From cb6caa3bb3a9ddfde823e7876d27d82826a80b01 Mon Sep 17 00:00:00 2001 From: Ales Pour Date: Fri, 18 Nov 2022 15:21:46 +0100 Subject: [PATCH] fix: pkger finds existing notification rule also by name --- pkger/service.go | 24 ++++++++++++++++++++++++ pkger/service_models.go | 19 +++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pkger/service.go b/pkger/service.go index 838132b0390..6998c1d10f6 100644 --- a/pkger/service.go +++ b/pkger/service.go @@ -1183,13 +1183,37 @@ func (s *Service) dryRunNotificationEndpoints(ctx context.Context, orgID platfor } func (s *Service) dryRunNotificationRules(ctx context.Context, orgID platform.ID, rules map[string]*stateRule, endpoints map[string]*stateEndpoint) error { + existingRules, _, err := s.ruleSVC.FindNotificationRules(ctx, influxdb.NotificationRuleFilter{ + OrgID: &orgID, + }) + if err != nil { + return internalErr(err) + } + for _, rule := range rules { rule.orgID = orgID var existing influxdb.NotificationRule if rule.ID() != 0 { existing, _ = s.ruleSVC.FindNotificationRuleByID(ctx, rule.ID()) + } else { + for _, existingRule := range existingRules { + if rule.parserRule.Name() == existingRule.GetName() { + existing = existingRule + break + } + } + } + if IsNew(rule.stateStatus) && existing != nil { + rule.stateStatus = StateStatusExists } rule.existing = existing + if existing != nil { + existingEndpoint, err := s.endpointSVC.FindNotificationEndpointByID(ctx, existing.GetEndpointID()) + if err != nil { + return internalErr(err) + } + rule.existingEndpoint = existingEndpoint + } } for _, r := range rules { diff --git a/pkger/service_models.go b/pkger/service_models.go index de134679abc..d8388e858dd 100644 --- a/pkger/service_models.go +++ b/pkger/service_models.go @@ -1180,8 +1180,9 @@ type stateRule struct { associatedEndpoint *stateEndpoint - parserRule *notificationRule - existing influxdb.NotificationRule + parserRule *notificationRule + existing influxdb.NotificationRule + existingEndpoint influxdb.NotificationEndpoint } func (r *stateRule) ID() platform.ID { @@ -1212,7 +1213,7 @@ func (r *stateRule) diffRule() DiffNotificationRule { New: DiffNotificationRuleValues{ Name: r.parserRule.Name(), Description: r.parserRule.description, - EndpointName: r.endpointTemplateName(), + EndpointName: r.endpointName(), EndpointID: SafeID(r.endpointID()), EndpointType: r.endpointType(), Every: r.parserRule.every.String(), @@ -1230,11 +1231,14 @@ func (r *stateRule) diffRule() DiffNotificationRule { sum.Old = &DiffNotificationRuleValues{ Name: r.existing.GetName(), Description: r.existing.GetDescription(), - EndpointName: r.existing.GetName(), EndpointID: SafeID(r.existing.GetEndpointID()), EndpointType: r.existing.Type(), } + if r.existingEndpoint != nil { + sum.Old.EndpointName = r.existingEndpoint.GetName() + } + assignBase := func(b rule.Base) { if b.Every != nil { sum.Old.Every = b.Every.TimeDuration().String() @@ -1286,6 +1290,13 @@ func (r *stateRule) endpointTemplateName() string { return "" } +func (r *stateRule) endpointName() string { + if r.associatedEndpoint != nil && r.associatedEndpoint.parserEndpoint != nil { + return r.associatedEndpoint.parserEndpoint.Name() + } + return "" +} + func (r *stateRule) endpointType() string { if r.associatedEndpoint != nil { return r.associatedEndpoint.parserEndpoint.kind.String()