Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pkger): update notification rule with unique constraints #17676

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,20 +832,45 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) error {
}

if rules := diff.NotificationRules; len(rules) > 0 {
headers := []string{"New", "Name", "Description", "Every", "Offset", "Endpoint Name", "Endpoint ID", "Endpoint Type"}
tablePrintFn("NOTIFICATION RULES", headers, len(rules), func(i int) []string {
v := rules[i]
printer := diffPrinterGen("Notification Rules", []string{
"Description",
"Every",
"Offset",
"Endpoint Name",
"Endpoint ID",
"Endpoint Type",
})

appendValues := func(id pkger.SafeID, pkgName string, v pkger.DiffNotificationRuleValues) []string {
return []string{
green(true),
pkgName,
id.String(),
v.Name,
v.Description,
v.Every,
v.Offset,
v.EndpointName,
v.EndpointID.String(),
v.EndpointType,
}
})
}

for _, e := range rules {
var oldRow []string
if e.Old != nil {
oldRow = appendValues(e.ID, e.PkgName, *e.Old)
}

newRow := appendValues(e.ID, e.PkgName, e.New)
switch {
case e.IsNew():
printer.AppendDiff(nil, newRow)
case e.Remove:
printer.AppendDiff(oldRow, nil)
default:
printer.AppendDiff(oldRow, newRow)
}
}
printer.Render()
}

if teles := diff.Telegrafs; len(teles) > 0 {
Expand Down
3 changes: 1 addition & 2 deletions cmd/influxd/launcher/launcher_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (tl *TestLauncher) ShutdownOrFail(tb testing.TB, ctx context.Context) {
}
}

// SetupOrFail creates a new user, bucket, org, and auth token.
// Setup creates a new user, bucket, org, and auth token.
func (tl *TestLauncher) Setup() error {
results, err := tl.OnBoard(&platform.OnboardingRequest{
User: "USER",
Expand All @@ -117,7 +117,6 @@ func (tl *TestLauncher) Setup() error {
}

tl.User = results.User
fmt.Println(tl.User)
tl.Org = results.Org
tl.Bucket = results.Bucket
tl.Auth = results.Auth
Expand Down
3 changes: 2 additions & 1 deletion cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func TestLauncher_Pkger(t *testing.T) {

require.Len(t, diff.NotificationRules, 1)
// the pkg being run here has a relationship with the rule and the endpoint within the pkg.
assert.Equal(t, "http", diff.NotificationRules[0].EndpointType)
assert.Equal(t, "http", diff.NotificationRules[0].New.EndpointType)

require.Len(t, diff.Dashboards, 1)
require.Len(t, diff.NotificationEndpoints, 1)
Expand Down Expand Up @@ -1089,6 +1089,7 @@ spec:
pkger.WithDashboardSVC(l.DashboardService(t)),
pkger.WithLabelSVC(l.LabelService(t)),
pkger.WithNotificationEndpointSVC(l.NotificationEndpointService(t)),
pkger.WithNotificationRuleSVC(l.NotificationRuleService()),
pkger.WithTaskSVC(l.TaskServiceKV()),
pkger.WithTelegrafSVC(l.TelegrafService(t)),
pkger.WithVariableSVC(l.VariableService(t)),
Expand Down
8 changes: 5 additions & 3 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (ex *resourceExporter) resourceCloneToKind(ctx context.Context, r ResourceT
}
endpointObjectName := object.Name()

mapResource(rule.GetOrgID(), rule.GetID(), KindNotificationRule, ruleToObject(rule, endpointObjectName, r.Name))
mapResource(rule.GetOrgID(), rule.GetID(), KindNotificationRule, NotificationRuleToObject(r.Name, endpointObjectName, rule))
case r.Kind.is(KindTask):
t, err := ex.taskSVC.FindTaskByID(ctx, r.ID)
if err != nil {
Expand Down Expand Up @@ -806,6 +806,7 @@ func LabelToObject(name string, l influxdb.Label) Object {
return o
}

// NotificationEndpointToObject converts an notification endpoint into a pkger Object.
func NotificationEndpointToObject(name string, e influxdb.NotificationEndpoint) Object {
if name == "" {
name = e.GetName()
Expand Down Expand Up @@ -845,13 +846,14 @@ func NotificationEndpointToObject(name string, e influxdb.NotificationEndpoint)
return o
}

func ruleToObject(iRule influxdb.NotificationRule, endpointName, name string) Object {
// NotificationRuleToObject converts an notification rule into a pkger Object.
func NotificationRuleToObject(name, endpointPkgName string, iRule influxdb.NotificationRule) Object {
if name == "" {
name = iRule.GetName()
}

o := newObject(KindNotificationRule, name)
o.Spec[fieldNotificationRuleEndpointName] = endpointName
o.Spec[fieldNotificationRuleEndpointName] = endpointPkgName
assignNonZeroStrings(o.Spec, map[string]string{
fieldDescription: iRule.GetDescription(),
})
Expand Down
3 changes: 0 additions & 3 deletions pkger/http_remote_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pkger

import (
"context"
"fmt"
"net/http"

"github.com/influxdata/influxdb/v2"
Expand Down Expand Up @@ -43,14 +42,12 @@ func (s *HTTPRemoteService) InitStack(ctx context.Context, userID influxdb.ID, s

id, err := influxdb.IDFromString(respBody.ID)
if err != nil {
fmt.Println("IN HERE with id: ", respBody.ID)
return Stack{}, err
}
newStack.ID = *id

orgID, err := influxdb.IDFromString(respBody.OrgID)
if err != nil {
fmt.Println("IN HERE with orgID: ", respBody.OrgID)
return Stack{}, err
}
newStack.OrgID = *orgID
Expand Down
105 changes: 90 additions & 15 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ func (d DiffNotificationEndpoint) IsNew() bool {
return d.Old == nil
}

// DiffNotificationRule is a diff of an individual notification rule. This resource is always new.
type DiffNotificationRule struct {
type DiffNotificationRuleValues struct {
Name string `json:"name"`
Description string `json:"description"`

Expand All @@ -430,31 +429,96 @@ type DiffNotificationRule struct {
Every string `json:"every"`
Offset string `json:"offset"`
MessageTemplate string `json:"messageTemplate"`
Status influxdb.Status `json:"status"`
StatusRules []SummaryStatusRule `json:"statusRules"`
TagRules []SummaryTagRule `json:"tagRules"`
}

// DiffNotificationRule is a diff of an individual notification rule. This resource is always new.
type DiffNotificationRule struct {
ID SafeID `json:"id"`
Remove bool `json:"bool"`
PkgName string `json:"pkgName"`

New DiffNotificationRuleValues `json:"new"`
Old *DiffNotificationRuleValues `json:"old"`
}

func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.NotificationEndpoint) DiffNotificationRule {
sum := DiffNotificationRule{
Name: r.Name(),
Description: r.description,
EndpointName: r.endpointName.String(),
Every: r.every.String(),
Offset: r.offset.String(),
MessageTemplate: r.msgTemplate,
Status: r.Status(),
StatusRules: toSummaryStatusRules(r.statusRules),
TagRules: toSummaryTagRules(r.tagRules),
ID: SafeID(r.ID()),
Remove: r.shouldRemove,
PkgName: r.PkgName(),
New: DiffNotificationRuleValues{
Name: r.Name(),
Description: r.description,
EndpointName: r.endpointName.String(),
Every: r.every.String(),
Offset: r.offset.String(),
MessageTemplate: r.msgTemplate,
StatusRules: toSummaryStatusRules(r.statusRules),
TagRules: toSummaryTagRules(r.tagRules),
},
}
if iEndpoint != nil {
sum.EndpointID = SafeID(iEndpoint.GetID())
sum.EndpointType = iEndpoint.Type()
sum.New.EndpointID = SafeID(iEndpoint.GetID())
sum.New.EndpointType = iEndpoint.Type()
}

if r.existing == nil {
return sum
}

sum.Old = &DiffNotificationRuleValues{
Name: r.existing.rule.GetName(),
Description: r.existing.rule.GetDescription(),
EndpointName: r.existing.endpointName,
EndpointID: SafeID(r.existing.rule.GetEndpointID()),
EndpointType: r.existing.endpointType,
}

assignBase := func(b rule.Base) {
if b.Every != nil {
sum.Old.Every = b.Every.TimeDuration().String()
}
if b.Offset != nil {
sum.Old.Offset = b.Offset.TimeDuration().String()
}
for _, tr := range b.TagRules {
sum.Old.TagRules = append(sum.Old.TagRules, SummaryTagRule{
Key: tr.Key,
Value: tr.Value,
Operator: tr.Operator.String(),
})
}
for _, sr := range b.StatusRules {
sRule := SummaryStatusRule{CurrentLevel: sr.CurrentLevel.String()}
if sr.PreviousLevel != nil {
sRule.PreviousLevel = sr.PreviousLevel.String()
}
sum.Old.StatusRules = append(sum.Old.StatusRules, sRule)
}
}

switch p := r.existing.rule.(type) {
case *rule.HTTP:
assignBase(p.Base)
case *rule.Slack:
assignBase(p.Base)
sum.Old.MessageTemplate = p.MessageTemplate
case *rule.PagerDuty:
assignBase(p.Base)
sum.Old.MessageTemplate = p.MessageTemplate
}

return sum
}

// IsNew indicates if the resource will be new to the platform or if it edits
// an existing resource.
func (d DiffNotificationRule) IsNew() bool {
return d.Old != nil
}

// DiffTask is a diff of an individual task. This resource is always new.
type DiffTask struct {
Name string `json:"name"`
Expand Down Expand Up @@ -1667,14 +1731,25 @@ type notificationRule struct {
endpointName *references
endpointType string

existing *existingRule

labels sortedLabels
}

type existingRule struct {
rule influxdb.NotificationRule
endpointName string
endpointType string
}

func (r *notificationRule) Exists() bool {
return false
return r.existing != nil
}

func (r *notificationRule) ID() influxdb.ID {
if r.existing != nil {
return r.existing.rule.GetID()
}
return r.id
}

Expand Down
Loading