Skip to content

Commit

Permalink
feat(pkger): add support for exporting notification endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 17, 2019
1 parent 3ed4aba commit 9118b61
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 39 deletions.
3 changes: 3 additions & 0 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type cmdPkgBuilder struct {
resourceType string
buckets string
dashboards string
endpoints string
labels string
telegrafs string
variables string
Expand Down Expand Up @@ -231,6 +232,7 @@ func (b *cmdPkgBuilder) cmdPkgExport() *cobra.Command {
cmd.Flags().StringVar(&b.exportOpts.resourceType, "resource-type", "", "The resource type provided will be associated with all IDs via stdin.")
cmd.Flags().StringVar(&b.exportOpts.buckets, "buckets", "", "List of bucket ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.dashboards, "dashboards", "", "List of dashboard ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.endpoints, "endpoints", "", "List of notification endpoint ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.labels, "labels", "", "List of label ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.telegrafs, "telegraf-configs", "", "List of telegraf config ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.variables, "variables", "", "List of variable ids comma separated")
Expand All @@ -255,6 +257,7 @@ func (b *cmdPkgBuilder) pkgExportRunEFn() func(*cobra.Command, []string) error {
}{
{kind: pkger.KindBucket, idStrs: strings.Split(b.exportOpts.buckets, ",")},
{kind: pkger.KindDashboard, idStrs: strings.Split(b.exportOpts.dashboards, ",")},
{kind: pkger.KindNotificationEndpoint, idStrs: strings.Split(b.exportOpts.endpoints, ",")},
{kind: pkger.KindLabel, idStrs: strings.Split(b.exportOpts.labels, ",")},
{kind: pkger.KindTelegraf, idStrs: strings.Split(b.exportOpts.telegrafs, ",")},
{kind: pkger.KindVariable, idStrs: strings.Split(b.exportOpts.variables, ",")},
Expand Down
19 changes: 15 additions & 4 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointPagerDuty
- kind: Notification_Endpoint_Pager_Duty
name: pager_duty_notification_endpoint
url: http://localhost:8080/orgs/7167eb6719fa34e5/alert-history
routingKey: secret-sauce
Expand All @@ -310,7 +310,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointPagerDuty
- kind: Notification_Endpoint_Pager_Duty
name: pager_duty_notification_endpoint
url: http://localhost:8080/orgs/7167eb6719fa34e5/alert-history
routingKey:
Expand Down Expand Up @@ -342,6 +342,10 @@ spec:
Kind: pkger.KindLabel,
ID: influxdb.ID(labels[0].ID),
},
{
Kind: pkger.KindNotificationEndpoint,
ID: endpoints[0].NotificationEndpoint.GetID(),
},
{
Kind: pkger.KindTelegraf,
ID: teles[0].TelegrafConfig.ID,
Expand Down Expand Up @@ -392,9 +396,16 @@ spec:
require.Len(t, dashs[0].Charts, 1)
assert.Equal(t, influxdb.ViewPropertyTypeSingleStat, dashs[0].Charts[0].Properties.GetType())

newEndpoints := newSum.NotificationEndpoints
require.Len(t, newEndpoints, 1)
assert.Equal(t, endpoints[0].NotificationEndpoint.GetName(), newEndpoints[0].NotificationEndpoint.GetName())
assert.Equal(t, endpoints[0].NotificationEndpoint.GetDescription(), newEndpoints[0].NotificationEndpoint.GetDescription())
hasLabelAssociations(t, newEndpoints[0].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)
hasLabelAssociations(t, newSum.TelegrafConfigs[0].LabelAssociations, 1, "label_1")

vars := newSum.Variables
require.Len(t, vars, 1)
Expand Down Expand Up @@ -528,7 +539,7 @@ spec:
bucket = "rucket_3"
[[inputs.cpu]]
percpu = true
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: http_none_auth_notification_endpoint
type: none
description: http none auth desc
Expand Down Expand Up @@ -567,7 +578,7 @@ spec:
associations:
- kind: Label
name: label_1
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: http_none_auth_notification_endpoint
type: none
description: new desc
Expand Down
1 change: 1 addition & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7131,6 +7131,7 @@ components:
- bucket
- dashboard
- label
- notification_endpoint
- variable
name:
type: string
Expand Down
54 changes: 54 additions & 0 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/notification/endpoint"
)

// ResourceToClone is a resource that will be cloned.
Expand Down Expand Up @@ -301,6 +302,46 @@ func labelToResource(l influxdb.Label, name string) Resource {
return r
}

func endpointToResource(e influxdb.NotificationEndpoint, name string) Resource {
if name == "" {
name = e.GetName()
}
r := Resource{
fieldName: name,
}
assignNonZeroStrings(r, map[string]string{
fieldDescription: e.GetDescription(),
fieldStatus: string(e.GetStatus()),
})

switch actual := e.(type) {
case *endpoint.HTTP:
r[fieldKind] = KindNotificationEndpointHTTP.title()
r[fieldNotificationEndpointHTTPMethod] = actual.Method
r[fieldNotificationEndpointURL] = actual.URL
r[fieldType] = actual.AuthMethod
assignNonZeroSecrets(r, map[string]influxdb.SecretField{
fieldNotificationEndpointPassword: actual.Password,
fieldNotificationEndpointToken: actual.Token,
fieldNotificationEndpointUsername: actual.Username,
})
case *endpoint.PagerDuty:
r[fieldKind] = KindNotificationEndpointPagerDuty.title()
r[fieldNotificationEndpointURL] = actual.ClientURL
assignNonZeroSecrets(r, map[string]influxdb.SecretField{
fieldNotificationEndpointRoutingKey: actual.RoutingKey,
})
case *endpoint.Slack:
r[fieldKind] = KindNotificationEndpointSlack.title()
r[fieldNotificationEndpointURL] = actual.URL
assignNonZeroSecrets(r, map[string]influxdb.SecretField{
fieldNotificationEndpointToken: actual.Token,
})
}

return r
}

func telegrafToResource(t influxdb.TelegrafConfig, name string) Resource {
if name == "" {
name = t.Name
Expand Down Expand Up @@ -379,6 +420,19 @@ func assignNonZeroStrings(r Resource, m map[string]string) {
}
}

func assignNonZeroSecrets(r Resource, m map[string]influxdb.SecretField) {
for field, secret := range m {
if secret.Key == "" {
continue
}
r[field] = Resource{
fieldReferencesSecret: Resource{
fieldKey: secret.Key,
},
}
}
}

func stringsToColors(clrs []string) colors {
newColors := make(colors, 0)
for _, x := range clrs {
Expand Down
9 changes: 5 additions & 4 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const (
KindBucket Kind = "bucket"
KindDashboard Kind = "dashboard"
KindLabel Kind = "label"
KindNotificationEndpoint Kind = "notificationendpoint"
KindNotificationEndpointPagerDuty Kind = "notificationendpointpagerduty"
KindNotificationEndpointHTTP Kind = "notificationendpointhttp"
KindNotificationEndpointSlack Kind = "notificationendpointslack"
KindNotificationEndpoint Kind = "notification_endpoint"
KindNotificationEndpointPagerDuty Kind = "notification_endpoint_pager_duty"
KindNotificationEndpointHTTP Kind = "notification_endpoint_http"
KindNotificationEndpointSlack Kind = "notification_endpoint_slack"
KindPackage Kind = "package"
KindTelegraf Kind = "telegraf"
KindVariable Kind = "variable"
Expand All @@ -33,6 +33,7 @@ var kinds = map[Kind]bool{
KindBucket: true,
KindDashboard: true,
KindLabel: true,
KindNotificationEndpoint: true,
KindNotificationEndpointHTTP: true,
KindNotificationEndpointPagerDuty: true,
KindNotificationEndpointSlack: true,
Expand Down
28 changes: 14 additions & 14 deletions pkger/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2807,7 +2807,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointSlack
- kind: Notification_Endpoint_Slack
name: name1
`,
},
Expand All @@ -2826,7 +2826,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointPagerDuty
- kind: Notification_Endpoint_Pager_Duty
name: name1
`,
},
Expand All @@ -2845,7 +2845,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
method: GET
`,
Expand All @@ -2865,7 +2865,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: none
method: POST
Expand All @@ -2887,7 +2887,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: none
url: http://example.com
Expand All @@ -2908,7 +2908,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: none
method: GUT
Expand All @@ -2930,7 +2930,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: basic
url: example.com
Expand All @@ -2953,7 +2953,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: basic
method: POST
Expand All @@ -2976,7 +2976,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: basic
method: POST
Expand All @@ -2998,7 +2998,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: bearer
method: GET
Expand All @@ -3020,7 +3020,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointHTTP
- kind: Notification_Endpoint_HTTP
name: name1
type: threeve
method: GET
Expand All @@ -3042,10 +3042,10 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointSlack
- kind: Notification_Endpoint_Slack
name: dupe
url: example.com
- kind: NotificationEndpointSlack
- kind: Notification_Endpoint_Slack
name: dupe
url: example.com
`,
Expand All @@ -3065,7 +3065,7 @@ meta:
description: pack description
spec:
resources:
- kind: NotificationEndpointSlack
- kind: Notification_Endpoint_Slack
name: dupe
url: example.com
status: rando bad status
Expand Down
36 changes: 34 additions & 2 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ func (s *Service) cloneOrgResources(ctx context.Context, orgID influxdb.ID) ([]R
resType: KindLabel.ResourceType(),
cloneFn: s.cloneOrgLabels,
},
{
resType: KindNotificationEndpoint.ResourceType(),
cloneFn: s.cloneOrgNotificationEndpoints,
},
{
resType: KindTelegraf.ResourceType(),
cloneFn: s.cloneTelegrafs,
cloneFn: s.cloneOrgTelegrafs,
},
{
resType: KindVariable.ResourceType(),
Expand Down Expand Up @@ -353,11 +357,30 @@ func (s *Service) cloneOrgLabels(ctx context.Context, orgID influxdb.ID) ([]Reso
return resources, nil
}

func (s *Service) cloneTelegrafs(ctx context.Context, orgID influxdb.ID) ([]ResourceToClone, error) {
func (s *Service) cloneOrgNotificationEndpoints(ctx context.Context, orgID influxdb.ID) ([]ResourceToClone, error) {
endpoints, _, err := s.endpointSVC.FindNotificationEndpoints(ctx, influxdb.NotificationEndpointFilter{
OrgID: &orgID,
})
if err != nil {
return nil, err
}

resources := make([]ResourceToClone, 0, len(endpoints))
for _, e := range endpoints {
resources = append(resources, ResourceToClone{
Kind: KindNotificationEndpoint,
ID: e.GetID(),
})
}
return resources, nil
}

func (s *Service) cloneOrgTelegrafs(ctx context.Context, orgID influxdb.ID) ([]ResourceToClone, error) {
teles, _, err := s.teleSVC.FindTelegrafConfigs(ctx, influxdb.TelegrafConfigFilter{OrgID: &orgID})
if err != nil {
return nil, err
}

resources := make([]ResourceToClone, 0, len(teles))
for _, t := range teles {
resources = append(resources, ResourceToClone{
Expand Down Expand Up @@ -413,6 +436,15 @@ func (s *Service) resourceCloneToResource(ctx context.Context, r ResourceToClone
return nil, err
}
newResource = labelToResource(*l, r.Name)
case r.Kind.is(KindNotificationEndpoint),
r.Kind.is(KindNotificationEndpointHTTP),
r.Kind.is(KindNotificationEndpointPagerDuty),
r.Kind.is(KindNotificationEndpointSlack):
e, err := s.endpointSVC.FindNotificationEndpointByID(ctx, r.ID)
if err != nil {
return nil, err
}
newResource = endpointToResource(e, r.Name)
case r.Kind.is(KindTelegraf):
t, err := s.teleSVC.FindTelegrafConfigByID(ctx, r.ID)
if err != nil {
Expand Down
Loading

0 comments on commit 9118b61

Please sign in to comment.