Skip to content

Commit

Permalink
fix: pkger finds existing notification rule also by name
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour committed Nov 18, 2022
1 parent 1179f19 commit cb6caa3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
24 changes: 24 additions & 0 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions pkger/service_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit cb6caa3

Please sign in to comment.