From af47e0f4eb475abeed85cb24d8db4ceeeb083876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Thu, 12 Sep 2024 13:40:13 +0200 Subject: [PATCH] don't enrich id if it's not provided (#1845) --- pkg/provisioning/config/enrich.go | 13 ++++++++++--- pkg/provisioning/config/enrich_test.go | 6 +++--- pkg/provisioning/config/validate.go | 27 ++++++++++++++------------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/provisioning/config/enrich.go b/pkg/provisioning/config/enrich.go index 50319e8e6..253d4448d 100644 --- a/pkg/provisioning/config/enrich.go +++ b/pkg/provisioning/config/enrich.go @@ -65,8 +65,11 @@ func enrichConnectors(mp []Connector, pipelineID string) []Connector { if cfg.Settings == nil { cfg.Settings = make(map[string]string) } - // attach the pipelineID to the connectorID - cfg.ID = pipelineID + ":" + cfg.ID + if cfg.ID != "" { + // attach the pipelineID to the connectorID only if the connectorID + // is actually provided, otherwise the validation will miss it later + cfg.ID = pipelineID + ":" + cfg.ID + } cfg.Processors = enrichProcessors(cfg.Processors, cfg.ID) out[i] = cfg } @@ -86,7 +89,11 @@ func enrichProcessors(mp []Processor, parentID string) []Processor { if cfg.Settings == nil { cfg.Settings = make(map[string]string) } - cfg.ID = parentID + ":" + cfg.ID + if cfg.ID != "" { + // attach the parentID to the processorID only if the processorID + // is actually provided, otherwise the validation will miss it later + cfg.ID = parentID + ":" + cfg.ID + } out[i] = cfg } return out diff --git a/pkg/provisioning/config/enrich_test.go b/pkg/provisioning/config/enrich_test.go index a4bf88159..93eb853a0 100644 --- a/pkg/provisioning/config/enrich_test.go +++ b/pkg/provisioning/config/enrich_test.go @@ -132,7 +132,7 @@ func TestEnrich_DefaultValues(t *testing.T) { Status: "stopped", Description: "empty", Connectors: []Connector{ - {ID: "con1"}, + {ID: ""}, }, Processors: []Processor{ {ID: "proc1"}, @@ -151,8 +151,8 @@ func TestEnrich_DefaultValues(t *testing.T) { }, Connectors: []Connector{ { - ID: "pipeline3:con1", - Name: "con1", + ID: "", + Name: "", Settings: map[string]string{}, }, }, diff --git a/pkg/provisioning/config/validate.go b/pkg/provisioning/config/validate.go index fc357e9fb..661b22740 100644 --- a/pkg/provisioning/config/validate.go +++ b/pkg/provisioning/config/validate.go @@ -56,24 +56,24 @@ func Validate(cfg Pipeline) error { func validateConnectors(mp []Connector) []error { var errs []error ids := make(map[string]bool) - for _, cfg := range mp { + for i, cfg := range mp { if cfg.ID == "" { - errs = append(errs, cerrors.Errorf(`id is mandatory: %w`, ErrMandatoryField)) + errs = append(errs, cerrors.Errorf("connector %d: id is mandatory: %w", i+1, ErrMandatoryField)) } if len(cfg.ID) > connector.IDLengthLimit { - errs = append(errs, connector.ErrIDOverLimit) + errs = append(errs, cerrors.Errorf("connector %q: %w", cfg.ID, connector.ErrIDOverLimit)) } if len(cfg.Name) > connector.NameLengthLimit { - errs = append(errs, connector.ErrNameOverLimit) + errs = append(errs, cerrors.Errorf("connector %q: %w", cfg.ID, connector.ErrNameOverLimit)) } if cfg.Plugin == "" { - errs = append(errs, cerrors.Errorf("connector %q: \"plugin\" is mandatory: %w", cfg.ID, ErrMandatoryField)) + errs = append(errs, cerrors.Errorf(`connector %q: "plugin" is mandatory: %w`, cfg.ID, ErrMandatoryField)) } if cfg.Type == "" { - errs = append(errs, cerrors.Errorf("connector %q: \"type\" is mandatory: %w", cfg.ID, ErrMandatoryField)) + errs = append(errs, cerrors.Errorf(`connector %q: "type" is mandatory: %w`, cfg.ID, ErrMandatoryField)) } if cfg.Type != "" && cfg.Type != TypeSource && cfg.Type != TypeDestination { - errs = append(errs, cerrors.Errorf("connector %q: \"type\" is invalid: %w", cfg.ID, ErrInvalidField)) + errs = append(errs, cerrors.Errorf(`connector %q: "type" is invalid: %w`, cfg.ID, ErrInvalidField)) } pErrs := validateProcessors(cfg.Processors) @@ -82,7 +82,7 @@ func validateConnectors(mp []Connector) []error { } if ids[cfg.ID] { - errs = append(errs, cerrors.Errorf("connector %q: \"id\" must be unique: %w", cfg.ID, ErrDuplicateID)) + errs = append(errs, cerrors.Errorf(`connector %q: "id" must be unique: %w`, cfg.ID, ErrDuplicateID)) } ids[cfg.ID] = true } @@ -93,15 +93,18 @@ func validateConnectors(mp []Connector) []error { func validateProcessors(mp []Processor) []error { var errs []error ids := make(map[string]bool) - for _, cfg := range mp { + for i, cfg := range mp { + if cfg.ID == "" { + errs = append(errs, cerrors.Errorf("processor %d: id is mandatory: %w", i+1, ErrMandatoryField)) + } if cfg.Plugin == "" { - errs = append(errs, cerrors.Errorf("processor %q: \"plugin\" is mandatory: %w", cfg.ID, ErrMandatoryField)) + errs = append(errs, cerrors.Errorf(`processor %q: "plugin" is mandatory: %w`, cfg.ID, ErrMandatoryField)) } if cfg.Workers < 0 { - errs = append(errs, cerrors.Errorf("processor %q: \"workers\" can't be negative: %w", cfg.ID, ErrInvalidField)) + errs = append(errs, cerrors.Errorf(`processor %q: "workers" can't be negative: %w`, cfg.ID, ErrInvalidField)) } if ids[cfg.ID] { - errs = append(errs, cerrors.Errorf("processor %q: \"id\" must be unique: %w", cfg.ID, ErrDuplicateID)) + errs = append(errs, cerrors.Errorf(`processor %q: "id" must be unique: %w`, cfg.ID, ErrDuplicateID)) } ids[cfg.ID] = true }