Skip to content

Commit

Permalink
feat(pkger): add export support for notification rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 20, 2019
1 parent abd8ce0 commit 279b4e6
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 17 deletions.
12 changes: 12 additions & 0 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ spec:
}

resWithNewName := []pkger.ResourceToClone{
{
Kind: pkger.KindNotificationRule,
Name: "new rule name",
ID: influxdb.ID(rule.ID),
},
{
Kind: pkger.KindVariable,
Name: "new name",
Expand Down Expand Up @@ -457,6 +462,13 @@ spec:
assert.Equal(t, endpoints[0].NotificationEndpoint.GetDescription(), newEndpoints[0].NotificationEndpoint.GetDescription())
hasLabelAssociations(t, newEndpoints[0].LabelAssociations, 1, "label_1")

require.Len(t, newSum.NotificationRules, 1)
newRule := newSum.NotificationRules[0]
assert.Equal(t, "new rule name", newRule.Name)
assert.Zero(t, newRule.EndpointID)
assert.Equal(t, rule.EndpointName, newRule.EndpointName)
hasLabelAssociations(t, newRule.LabelAssociations, 1, "label_1")

require.Len(t, newSum.TelegrafConfigs, 1)
assert.Equal(t, teles[0].TelegrafConfig.Name, newSum.TelegrafConfigs[0].TelegrafConfig.Name)
assert.Equal(t, teles[0].TelegrafConfig.Description, newSum.TelegrafConfigs[0].TelegrafConfig.Description)
Expand Down
94 changes: 83 additions & 11 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/influxdata/influxdb/notification"
icheck "github.com/influxdata/influxdb/notification/check"
"github.com/influxdata/influxdb/notification/endpoint"
"github.com/influxdata/influxdb/notification/rule"
)

// ResourceToClone is a resource that will be cloned.
Expand Down Expand Up @@ -79,18 +80,14 @@ func checkToResource(ch influxdb.Check, name string) Resource {
}
assignNonZeroStrings(r, map[string]string{fieldDescription: ch.GetDescription()})

assignFluxDur := func(field string, dur *notification.Duration) {
if dur == nil {
return
}
r[field] = dur.TimeDuration().String()
}

assignBase := func(base icheck.Base) {
r[fieldQuery] = base.Query.Text
r[fieldCheckStatusMessageTemplate] = base.StatusMessageTemplate
assignFluxDur(fieldEvery, base.Every)
assignFluxDur(fieldOffset, base.Offset)
assignNonZeroFluxDurs(r, map[string]*notification.Duration{
fieldEvery: base.Every,
fieldOffset: base.Offset,
})

var tags []Resource
for _, t := range base.Tags {
if t.Valid() != nil {
Expand All @@ -110,8 +107,10 @@ func checkToResource(ch influxdb.Check, name string) Resource {
case *icheck.Deadman:
r[fieldKind] = KindCheckDeadman.title()
assignBase(cT.Base)
assignFluxDur(fieldCheckTimeSince, cT.TimeSince)
assignFluxDur(fieldCheckStaleTime, cT.StaleTime)
assignNonZeroFluxDurs(r, map[string]*notification.Duration{
fieldCheckTimeSince: cT.TimeSince,
fieldCheckStaleTime: cT.StaleTime,
})
r[fieldLevel] = cT.Level.String()
assignNonZeroBools(r, map[string]bool{fieldCheckReportZero: cT.ReportZero})
case *icheck.Threshold:
Expand Down Expand Up @@ -433,6 +432,67 @@ func endpointToResource(e influxdb.NotificationEndpoint, name string) Resource {
return r
}

func ruleToResource(iRule influxdb.NotificationRule, endpointName, name string) Resource {
if name == "" {
name = iRule.GetName()
}
r := Resource{
fieldKind: KindNotificationRule.title(),
fieldName: name,
fieldNotificationRuleEndpointName: endpointName,
}
assignNonZeroStrings(r, map[string]string{
fieldDescription: iRule.GetDescription(),
})

assignBase := func(base rule.Base) {
assignNonZeroFluxDurs(r, map[string]*notification.Duration{
fieldEvery: base.Every,
fieldOffset: base.Offset,
})

var tagRes []Resource
for _, tRule := range base.TagRules {
tagRes = append(tagRes, Resource{
fieldKey: tRule.Key,
fieldValue: tRule.Value,
fieldOperator: tRule.Operator.String(),
})
}
if len(tagRes) > 0 {
r[fieldNotificationRuleTagRules] = tagRes
}

var statusRuleRes []Resource
for _, sRule := range base.StatusRules {
sRes := Resource{
fieldNotificationRuleCurrentLevel: sRule.CurrentLevel.String(),
}
if sRule.PreviousLevel != nil {
sRes[fieldNotificationRulePreviousLevel] = sRule.PreviousLevel.String()
}
statusRuleRes = append(statusRuleRes, sRes)
}
if len(statusRuleRes) > 0 {
r[fieldNotificationRuleStatusRules] = statusRuleRes
}
}

switch t := iRule.(type) {
case *rule.HTTP:
assignBase(t.Base)
case *rule.PagerDuty:
assignBase(t.Base)
r[fieldNotificationRuleMessageTemplate] = t.MessageTemplate
case *rule.Slack:
assignBase(t.Base)
r[fieldNotificationRuleMessageTemplate] = t.MessageTemplate
assignNonZeroStrings(r, map[string]string{fieldNotificationRuleChannel: t.Channel})
}

return r
}

func telegrafToResource(t influxdb.TelegrafConfig, name string) Resource {
if name == "" {
name = t.Name
Expand Down Expand Up @@ -487,6 +547,18 @@ func variableToResource(v influxdb.Variable, name string) Resource {
return r
}

func assignNonZeroFluxDurs(r Resource, m map[string]*notification.Duration) {
for field, dur := range m {
if dur == nil {
continue
}
if dur.TimeDuration() == 0 {
continue
}
r[field] = dur.TimeDuration().String()
}
}

func assignNonZeroBools(r Resource, m map[string]bool) {
for k, v := range m {
if v {
Expand Down
34 changes: 28 additions & 6 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ func (s *Service) resourceCloneToResource(ctx context.Context, r ResourceToClone
e = ierrors.Wrap(e, "cloning resource")
}
}()
var newResource Resource

var (
newResource Resource
sidecarResources []Resource
)
switch {
case r.Kind.is(KindBucket):
bkt, err := s.bucketSVC.FindBucketByID(ctx, r.ID)
Expand Down Expand Up @@ -495,6 +499,12 @@ func (s *Service) resourceCloneToResource(ctx context.Context, r ResourceToClone
return nil, err
}
newResource = endpointToResource(e, r.Name)
case r.Kind.is(KindNotificationRule):
ruleRes, endpointRes, err := s.exportNotificationRule(ctx, r)
if err != nil {
return nil, err
}
newResource, sidecarResources = ruleRes, append(sidecarResources, endpointRes)
case r.Kind.is(KindTelegraf):
t, err := s.teleSVC.FindTelegrafConfigByID(ctx, r.ID)
if err != nil {
Expand All @@ -519,7 +529,21 @@ func (s *Service) resourceCloneToResource(ctx context.Context, r ResourceToClone
newResource[fieldAssociations] = ass.associations
}

return append([]Resource{newResource}, ass.newLableResources...), nil
return append(ass.newLableResources, append(sidecarResources, newResource)...), nil
}

func (s *Service) exportNotificationRule(ctx context.Context, r ResourceToClone) (Resource, Resource, error) {
rule, err := s.ruleSVC.FindNotificationRuleByID(ctx, r.ID)
if err != nil {
return nil, nil, err
}

ruleEndpoint, err := s.endpointSVC.FindNotificationEndpointByID(ctx, rule.GetEndpointID())
if err != nil {
return nil, nil, err
}

return ruleToResource(rule, ruleEndpoint.GetName(), r.Name), endpointToResource(ruleEndpoint, ""), nil
}

type (
Expand Down Expand Up @@ -1043,10 +1067,8 @@ func (s *Service) Apply(ctx context.Context, orgID, userID influxdb.ID, pkg *Pkg
}
}

// this is required after first primary run and before secondary run, b/c this has dependencies
// on the notification endpoints being live in the source of truth (store). Hence we break
// it up after the 1st group and before the 2nd. The 2nd group needs these done first so the
// label mappings are accurate.
// this has to be run after the above primary resources, because it relies on
// notification endpoints already being applied.
app, err := s.applyNotificationRulesGenerator(ctx, orgID, pkg.notificationRules())
if err != nil {
return Summary{}, err
Expand Down
Loading

0 comments on commit 279b4e6

Please sign in to comment.