Skip to content

Commit

Permalink
feat(pkger): add application functionality for the notification rules…
Browse files Browse the repository at this point in the history
… resource
  • Loading branch information
jsteenb2 committed Dec 20, 2019
1 parent a2b2d82 commit 61dceaa
Show file tree
Hide file tree
Showing 16 changed files with 609 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
1. [16212](https://github.com/influxdata/influxdb/pull/16212): Add new kv.ForwardCursor interface
1. [16297](https://github.com/influxdata/influxdb/pull/16297): Add support for notification rule to pkger parser
1. [16298](https://github.com/influxdata/influxdb/pull/16298): Add support for notification rule pkger dry run functionality
1. [16305](https://github.com/influxdata/influxdb/pull/16305): Add support for notification rule pkger apply functionality

### Bug Fixes

Expand Down
17 changes: 17 additions & 0 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,23 @@ func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) {
})
}

if rules := sum.NotificationRules; len(rules) > 0 {
headers := []string{"ID", "Name", "Description", "Every", "Offset", "Endpoint Name", "Endpoint ID", "Endpoint Type"}
tablePrintFn("NOTIFICATION RULES", headers, len(rules), func(i int) []string {
v := rules[i]
return []string{
v.ID.String(),
v.Name,
v.Description,
v.Every,
v.Offset,
v.EndpointName,
v.EndpointID.String(),
v.EndpointType,
}
})
}

if teles := sum.TelegrafConfigs; len(teles) > 0 {
headers := []string{"ID", "Name", "Description"}
tablePrintFn("TELEGRAF CONFIGS", headers, len(teles), func(i int) []string {
Expand Down
3 changes: 2 additions & 1 deletion cmd/influxd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,8 @@ func (m *Launcher) run(ctx context.Context) (err error) {
pkger.WithCheckSVC(authorizer.NewCheckService(b.CheckService, authedURMSVC, authedOrgSVC)),
pkger.WithDashboardSVC(authorizer.NewDashboardService(b.DashboardService)),
pkger.WithLabelSVC(authorizer.NewLabelService(b.LabelService)),
pkger.WithNoticationEndpointSVC(authorizer.NewNotificationEndpointService(b.NotificationEndpointService, authedURMSVC, authedOrgSVC)),
pkger.WithNotificationEndpointSVC(authorizer.NewNotificationEndpointService(b.NotificationEndpointService, authedURMSVC, authedOrgSVC)),
pkger.WithNotificationRuleSVC(authorizer.NewNotificationRuleStore(b.NotificationRuleStore, authedURMSVC, authedOrgSVC)),
pkger.WithSecretSVC(authorizer.NewSecretService(b.SecretService)),
pkger.WithTelegrafSVC(authorizer.NewTelegrafConfigService(b.TelegrafService, b.UserResourceMappingService)),
pkger.WithVariableSVC(authorizer.NewVariableService(b.VariableService)),
Expand Down
4 changes: 4 additions & 0 deletions cmd/influxd/launcher/launcher_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func (tl *TestLauncher) NotificationEndpointService(tb testing.TB) *http.Notific
return http.NewNotificationEndpointService(tl.HTTPClient(tb))
}

func (tl *TestLauncher) NotificationRuleService() platform.NotificationRuleStore {
return tl.kvService
}

func (tl *TestLauncher) PkgerService(tb testing.TB) pkger.SVC {
return &http.PkgerService{Client: tl.HTTPClient(tb)}
}
Expand Down
19 changes: 17 additions & 2 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func TestLauncher_Pkger(t *testing.T) {
LabelService: l.LabelService(t),
killCount: 2, // hits error on 3rd attempt at creating a mapping
}),
pkger.WithNoticationEndpointSVC(l.NotificationEndpointService(t)),
pkger.WithNotificationEndpointSVC(l.NotificationEndpointService(t)),
pkger.WithNotificationRuleSVC(l.NotificationRuleService()),
pkger.WithTelegrafSVC(l.TelegrafService(t)),
pkger.WithVariableSVC(l.VariableService(t)),
)
Expand Down Expand Up @@ -81,6 +82,12 @@ func TestLauncher_Pkger(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, endpoints)

rules, _, err := l.NotificationRuleService().FindNotificationRules(ctx, influxdb.NotificationRuleFilter{
OrgID: &l.Org.ID,
})
require.NoError(t, err)
assert.Empty(t, rules)

teles, _, err := l.TelegrafService(t).FindTelegrafConfigs(ctx, influxdb.TelegrafConfigFilter{
OrgID: &l.Org.ID,
})
Expand Down Expand Up @@ -238,6 +245,14 @@ func TestLauncher_Pkger(t *testing.T) {
assert.Equal(t, influxdb.TaskStatusInactive, string(endpoints[0].NotificationEndpoint.GetStatus()))
hasLabelAssociations(t, endpoints[0].LabelAssociations, 1, "label_1")

require.Len(t, sum1.NotificationRules, 1)
rule := sum1.NotificationRules[0]
assert.NotZero(t, rule.ID)
assert.Equal(t, "rule_0", rule.Name)
assert.Equal(t, pkger.SafeID(endpoints[0].NotificationEndpoint.GetID()), rule.EndpointID)
assert.Equal(t, "http_none_auth_notification_endpoint", rule.EndpointName)
assert.Equal(t, "http", rule.EndpointType)

teles := sum1.TelegrafConfigs
require.Len(t, teles, 1)
assert.NotZero(t, teles[0].TelegrafConfig.ID)
Expand Down Expand Up @@ -473,7 +488,7 @@ spec:
pkger.WithCheckSVC(l.CheckService()),
pkger.WithDashboardSVC(l.DashboardService(t)),
pkger.WithLabelSVC(l.LabelService(t)),
pkger.WithNoticationEndpointSVC(l.NotificationEndpointService(t)),
pkger.WithNotificationEndpointSVC(l.NotificationEndpointService(t)),
pkger.WithTelegrafSVC(l.TelegrafService(t)),
pkger.WithVariableSVC(l.VariableService(t)),
)
Expand Down
57 changes: 52 additions & 5 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7221,7 +7221,7 @@ components:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
$ref: "#/components/schemas/PkgSummaryLabel"
labels:
type: array
items:
Expand All @@ -7242,7 +7242,7 @@ components:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
$ref: "#/components/schemas/PkgSummaryLabel"
charts:
type: array
items:
Expand Down Expand Up @@ -7272,7 +7272,54 @@ components:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
$ref: "#/components/schemas/PkgSummaryLabel"
notificationRules:
type: array
items:
type: object
properties:
name:
type: string
description:
type: string
endpointName:
type: string
endpointID:
type: string
endpointType:
type: string
every:
type: string
offset:
type: string
messageTemplate:
type: string
status:
type: string
statusRules:
type: array
items:
type: object
properties:
currentLevel:
type: string
previousLevel:
type: string
tagRules:
type: array
items:
type: object
properties:
key:
type: string
value:
type: string
operator:
type: string
labelAssociations:
type: array
items:
$ref: "#/components/schemas/PkgSummaryLabel"
telegrafConfigs:
type: array
items:
Expand All @@ -7283,7 +7330,7 @@ components:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
$ref: "#/components/schemas/PkgSummaryLabel"
variables:
type: array
items:
Expand All @@ -7302,7 +7349,7 @@ components:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
$ref: "#/components/schemas/PkgSummaryLabel"
diff:
type: object
properties:
Expand Down
54 changes: 46 additions & 8 deletions mock/notification_rule_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,83 @@ var _ influxdb.NotificationRuleStore = &NotificationRuleStore{}

// NotificationRuleStore represents a service for managing notification rule data.
type NotificationRuleStore struct {
OrganizationService
UserResourceMappingService
FindNotificationRuleByIDF func(ctx context.Context, id influxdb.ID) (influxdb.NotificationRule, error)
FindNotificationRulesF func(ctx context.Context, filter influxdb.NotificationRuleFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationRule, int, error)
CreateNotificationRuleF func(ctx context.Context, nr influxdb.NotificationRuleCreate, userID influxdb.ID) error
UpdateNotificationRuleF func(ctx context.Context, id influxdb.ID, nr influxdb.NotificationRuleCreate, userID influxdb.ID) (influxdb.NotificationRule, error)
PatchNotificationRuleF func(ctx context.Context, id influxdb.ID, upd influxdb.NotificationRuleUpdate) (influxdb.NotificationRule, error)
DeleteNotificationRuleF func(ctx context.Context, id influxdb.ID) error
*OrganizationService
*UserResourceMappingService
FindNotificationRuleByIDF func(ctx context.Context, id influxdb.ID) (influxdb.NotificationRule, error)
FindNotificationRuleByIDCalls SafeCount
FindNotificationRulesF func(ctx context.Context, filter influxdb.NotificationRuleFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationRule, int, error)
FindNotificationRulesCalls SafeCount
CreateNotificationRuleF func(ctx context.Context, nr influxdb.NotificationRuleCreate, userID influxdb.ID) error
CreateNotificationRuleCalls SafeCount
UpdateNotificationRuleF func(ctx context.Context, id influxdb.ID, nr influxdb.NotificationRuleCreate, userID influxdb.ID) (influxdb.NotificationRule, error)
UpdateNotificationRuleCalls SafeCount
PatchNotificationRuleF func(ctx context.Context, id influxdb.ID, upd influxdb.NotificationRuleUpdate) (influxdb.NotificationRule, error)
PatchNotificationRuleCalls SafeCount
DeleteNotificationRuleF func(ctx context.Context, id influxdb.ID) error
DeleteNotificationRuleCalls SafeCount
}

// NewNotificationRuleStore creats a fake notification rules tore.
func NewNotificationRuleStore() *NotificationRuleStore {
return &NotificationRuleStore{
OrganizationService: NewOrganizationService(),
UserResourceMappingService: NewUserResourceMappingService(),
FindNotificationRuleByIDF: func(ctx context.Context, id influxdb.ID) (influxdb.NotificationRule, error) {
return nil, nil
},
FindNotificationRulesF: func(ctx context.Context, f influxdb.NotificationRuleFilter, _ ...influxdb.FindOptions) ([]influxdb.NotificationRule, int, error) {
return nil, 0, nil
},
CreateNotificationRuleF: func(ctx context.Context, nr influxdb.NotificationRuleCreate, userID influxdb.ID) error {
return nil
},
UpdateNotificationRuleF: func(ctx context.Context, id influxdb.ID, nr influxdb.NotificationRuleCreate, userID influxdb.ID) (influxdb.NotificationRule, error) {
return nil, nil
},
PatchNotificationRuleF: func(ctx context.Context, id influxdb.ID, upd influxdb.NotificationRuleUpdate) (influxdb.NotificationRule, error) {
return nil, nil
},
DeleteNotificationRuleF: func(ctx context.Context, id influxdb.ID) error {
return nil
},
}
}

// FindNotificationRuleByID returns a single telegraf config by ID.
func (s *NotificationRuleStore) FindNotificationRuleByID(ctx context.Context, id influxdb.ID) (influxdb.NotificationRule, error) {
defer s.FindNotificationRuleByIDCalls.IncrFn()()
return s.FindNotificationRuleByIDF(ctx, id)
}

// FindNotificationRules returns a list of notification rules that match filter and the total count of matching notification rules.
// Additional options provide pagination & sorting.
func (s *NotificationRuleStore) FindNotificationRules(ctx context.Context, filter influxdb.NotificationRuleFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationRule, int, error) {
defer s.FindNotificationRulesCalls.IncrFn()()
return s.FindNotificationRulesF(ctx, filter, opt...)
}

// CreateNotificationRule creates a new notification rule and sets ID with the new identifier.
func (s *NotificationRuleStore) CreateNotificationRule(ctx context.Context, nr influxdb.NotificationRuleCreate, userID influxdb.ID) error {
defer s.CreateNotificationRuleCalls.IncrFn()()
return s.CreateNotificationRuleF(ctx, nr, userID)
}

// UpdateNotificationRule updates a single notification rule.
// Returns the new notification rule after update.
func (s *NotificationRuleStore) UpdateNotificationRule(ctx context.Context, id influxdb.ID, nr influxdb.NotificationRuleCreate, userID influxdb.ID) (influxdb.NotificationRule, error) {
defer s.UpdateNotificationRuleCalls.IncrFn()()
return s.UpdateNotificationRuleF(ctx, id, nr, userID)
}

// PatchNotificationRule updates a single notification rule with changeset.
// Returns the new notification rule after update.
func (s *NotificationRuleStore) PatchNotificationRule(ctx context.Context, id influxdb.ID, upd influxdb.NotificationRuleUpdate) (influxdb.NotificationRule, error) {
defer s.PatchNotificationRuleCalls.IncrFn()()
return s.PatchNotificationRuleF(ctx, id, upd)
}

// DeleteNotificationRule removes a notification rule by ID.
func (s *NotificationRuleStore) DeleteNotificationRule(ctx context.Context, id influxdb.ID) error {
defer s.DeleteNotificationRuleCalls.IncrFn()()
return s.DeleteNotificationRuleF(ctx, id)
}
8 changes: 3 additions & 5 deletions notification/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ var typeToRule = map[string](func() influxdb.NotificationRule){
"http": func() influxdb.NotificationRule { return &HTTP{} },
}

type rawRuleJSON struct {
Typ string `json:"type"`
}

// UnmarshalJSON will convert
func UnmarshalJSON(b []byte) (influxdb.NotificationRule, error) {
var raw rawRuleJSON
var raw struct {
Typ string `json:"type"`
}
if err := json.Unmarshal(b, &raw); err != nil {
return nil, &influxdb.Error{
Msg: "unable to detect the notification type from json",
Expand Down
Loading

0 comments on commit 61dceaa

Please sign in to comment.