Add batch distributor notification config
This commit is contained in:
@@ -21,6 +21,14 @@ type DistributorTemplateValues struct {
|
||||
BundleID string
|
||||
}
|
||||
|
||||
type DistributorBatchTemplateValues struct {
|
||||
LocationID string
|
||||
Batch string
|
||||
BatchRunID string
|
||||
BatchStartedDate string
|
||||
BundleID string
|
||||
}
|
||||
|
||||
var distributorTemplateVariables = map[string]struct{}{
|
||||
"location_id": {},
|
||||
"report_id": {},
|
||||
@@ -52,6 +60,23 @@ var distributorIdempotencyTemplateVariables = map[string]struct{}{
|
||||
|
||||
var distributorPipelineTemplateVariables = distributorIdempotencyTemplateVariables
|
||||
|
||||
var distributorBatchTemplateVariables = map[string]struct{}{
|
||||
"location_id": {},
|
||||
"batch": {},
|
||||
"batch_run_id": {},
|
||||
"batch_started_date": {},
|
||||
}
|
||||
|
||||
var distributorBatchIdempotencyTemplateVariables = map[string]struct{}{
|
||||
"location_id": {},
|
||||
"batch": {},
|
||||
"batch_run_id": {},
|
||||
"batch_started_date": {},
|
||||
"bundle_id": {},
|
||||
}
|
||||
|
||||
var distributorBatchPipelineTemplateVariables = distributorBatchTemplateVariables
|
||||
|
||||
func RenderDistributorBundleID(template string, values DistributorTemplateValues) (string, error) {
|
||||
return renderDistributorTemplate("notify.distributor.bundle_id_template", template, values, distributorTemplateVariables)
|
||||
}
|
||||
@@ -71,6 +96,39 @@ func RenderDistributorIdempotencyKey(template string, values DistributorTemplate
|
||||
return renderDistributorTemplate("notify.distributor.idempotency_key_template", template, values, distributorIdempotencyTemplateVariables)
|
||||
}
|
||||
|
||||
func RenderDistributorBatchBundleID(template string, values DistributorBatchTemplateValues) (string, error) {
|
||||
rendered, err := renderDistributorBatchTemplate("notify.distributor.batch.bundle_id_template", template, values, distributorBatchTemplateVariables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(rendered) == "" {
|
||||
return "", fmt.Errorf("notify.distributor.batch.bundle_id_template renders an empty bundle id")
|
||||
}
|
||||
return rendered, nil
|
||||
}
|
||||
|
||||
func RenderDistributorBatchPipelineID(template string, values DistributorBatchTemplateValues) (string, error) {
|
||||
rendered, err := renderDistributorBatchTemplate("notify.distributor.batch.pipeline_id_template", template, values, distributorBatchPipelineTemplateVariables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(rendered) == "" {
|
||||
return "", fmt.Errorf("notify.distributor.batch.pipeline_id_template renders an empty pipeline id")
|
||||
}
|
||||
return rendered, nil
|
||||
}
|
||||
|
||||
func RenderDistributorBatchIdempotencyKey(template string, values DistributorBatchTemplateValues) (string, error) {
|
||||
rendered, err := renderDistributorBatchTemplate("notify.distributor.batch.idempotency_key_template", template, values, distributorBatchIdempotencyTemplateVariables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.TrimSpace(rendered) == "" {
|
||||
return "", fmt.Errorf("notify.distributor.batch.idempotency_key_template renders an empty idempotency key")
|
||||
}
|
||||
return rendered, nil
|
||||
}
|
||||
|
||||
func RenderDistributorReportPaths(templates []string, values DistributorTemplateValues) ([]string, error) {
|
||||
if len(templates) == 0 {
|
||||
return nil, fmt.Errorf("notify.distributor.report_path_templates must contain at least one entry")
|
||||
@@ -100,6 +158,11 @@ func validateDistributorTemplate(name, template string, allowed map[string]struc
|
||||
return err
|
||||
}
|
||||
|
||||
func validateDistributorBatchTemplate(name, template string, allowed map[string]struct{}) error {
|
||||
_, err := renderDistributorBatchTemplate(name, template, DistributorBatchTemplateValues{}, allowed)
|
||||
return err
|
||||
}
|
||||
|
||||
func renderDistributorTemplate(name, template string, values DistributorTemplateValues, allowed map[string]struct{}) (string, error) {
|
||||
var rendered strings.Builder
|
||||
for i := 0; i < len(template); {
|
||||
@@ -128,6 +191,34 @@ func renderDistributorTemplate(name, template string, values DistributorTemplate
|
||||
return rendered.String(), nil
|
||||
}
|
||||
|
||||
func renderDistributorBatchTemplate(name, template string, values DistributorBatchTemplateValues, allowed map[string]struct{}) (string, error) {
|
||||
var rendered strings.Builder
|
||||
for i := 0; i < len(template); {
|
||||
switch template[i] {
|
||||
case '{':
|
||||
end := strings.IndexByte(template[i+1:], '}')
|
||||
if end < 0 {
|
||||
return "", fmt.Errorf("%s contains an unclosed template variable", name)
|
||||
}
|
||||
variable := template[i+1 : i+1+end]
|
||||
if variable == "" {
|
||||
return "", fmt.Errorf("%s contains an empty template variable", name)
|
||||
}
|
||||
if _, ok := allowed[variable]; !ok {
|
||||
return "", fmt.Errorf("%s contains unknown template variable %q", name, variable)
|
||||
}
|
||||
rendered.WriteString(distributorBatchTemplateValue(variable, values))
|
||||
i += end + 2
|
||||
case '}':
|
||||
return "", fmt.Errorf("%s contains an unopened template variable", name)
|
||||
default:
|
||||
rendered.WriteByte(template[i])
|
||||
i++
|
||||
}
|
||||
}
|
||||
return rendered.String(), nil
|
||||
}
|
||||
|
||||
func distributorTemplateValue(variable string, values DistributorTemplateValues) string {
|
||||
switch variable {
|
||||
case "location_id":
|
||||
@@ -159,6 +250,23 @@ func distributorTemplateValue(variable string, values DistributorTemplateValues)
|
||||
}
|
||||
}
|
||||
|
||||
func distributorBatchTemplateValue(variable string, values DistributorBatchTemplateValues) string {
|
||||
switch variable {
|
||||
case "location_id":
|
||||
return values.LocationID
|
||||
case "batch":
|
||||
return values.Batch
|
||||
case "batch_run_id":
|
||||
return values.BatchRunID
|
||||
case "batch_started_date":
|
||||
return values.BatchStartedDate
|
||||
case "bundle_id":
|
||||
return values.BundleID
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateDistributorReportPath(name, path string) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("%s renders an empty path", name)
|
||||
|
||||
Reference in New Issue
Block a user