Skip to content

Commit

Permalink
feat(pkger): add apply functionality for notification endpoints kind
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 11, 2019
1 parent 69d7eb4 commit 3daaa4d
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 91 deletions.
26 changes: 26 additions & 0 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ func newPkgerSVC(cliReqOpts httpClientOpts, opts ...pkger.ServiceSetterFn) (pkge
pkger.WithBucketSVC(&ihttp.BucketService{Client: httpClient}),
pkger.WithDashboardSVC(&ihttp.DashboardService{Client: httpClient}),
pkger.WithLabelSVC(&ihttp.LabelService{Client: httpClient}),
pkger.WithNoticationEndpointSVC(ihttp.NewNotificationEndpointService(httpClient)),
pkger.WithTelegrafSVC(ihttp.NewTelegrafService(httpClient)),
pkger.WithVariableSVC(&ihttp.VariableService{Client: httpClient}),
)...,
Expand Down Expand Up @@ -654,6 +655,18 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) {
})
}

if endpoints := diff.NotificationEndpoints; len(endpoints) > 0 {
headers := []string{"New", "ID", "Name"}
tablePrintFn("NOTIFICATION ENDPOINTS", headers, len(endpoints), func(i int) []string {
v := endpoints[i]
return []string{
boolDiff(v.IsNew()),
v.ID.String(),
v.Name,
}
})
}

if teles := diff.Telegrafs; len(diff.Telegrafs) > 0 {
headers := []string{"New", "Name", "Description"}
tablePrintFn("TELEGRAF CONFIGS", headers, len(teles), func(i int) []string {
Expand Down Expand Up @@ -733,6 +746,19 @@ func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) {
})
}

if endpoints := sum.NotificationEndpoints; len(endpoints) > 0 {
headers := []string{"ID", "Name", "Description", "Status"}
tablePrintFn("NOTIFICATION ENDPOINTS", headers, len(endpoints), func(i int) []string {
v := endpoints[i]
return []string{
v.GetID().String(),
v.GetName(),
v.GetDescription(),
string(v.GetStatus()),
}
})
}

if teles := sum.TelegrafConfigs; len(teles) > 0 {
headers := []string{"ID", "Name", "Description"}
tablePrintFn("TELEGRAF CONFIGS", headers, len(teles), func(i int) []string {
Expand Down
28 changes: 19 additions & 9 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ func TestLauncher_Pkger(t *testing.T) {
assert.Equal(t, "desc1", dashs[0].Description)
hasLabelAssociations(t, dashs[0].LabelAssociations, 1, "label_1")

endpoints := sum1.NotificationEndpoints
require.Len(t, endpoints, 1)
assert.NotZero(t, endpoints[0].GetID())
assert.Equal(t, "http_none_auth_notification_endpoint", endpoints[0].GetName())
assert.Equal(t, "http none auth desc", endpoints[0].GetDescription())
assert.Equal(t, influxdb.TaskStatusInactive, string(endpoints[0].GetStatus()))
hasLabelAssociations(t, endpoints[0].LabelAssociations, 1, "label_1")

teles := sum1.TelegrafConfigs
require.Len(t, teles, 1)
assert.NotZero(t, teles[0].ID)
assert.Equal(t, l.Org.ID, teles[0].OrgID)
assert.Equal(t, "first_tele_config", teles[0].Name)
assert.Equal(t, "desc", teles[0].Description)
assert.Len(t, teles[0].Plugins, 2)

vars := sum1.Variables
require.Len(t, vars, 1)
assert.NotZero(t, vars[0].ID)
Expand All @@ -223,14 +239,6 @@ func TestLauncher_Pkger(t *testing.T) {
Language: "flux",
}, varArgs.Values)

teles := sum1.TelegrafConfigs
require.Len(t, teles, 1)
assert.NotZero(t, teles[0].ID)
assert.Equal(t, l.Org.ID, teles[0].OrgID)
assert.Equal(t, "first_tele_config", teles[0].Name)
assert.Equal(t, "desc", teles[0].Description)
assert.Len(t, teles[0].Plugins, 2)

newSumMapping := func(id influxdb.ID, name string, rt influxdb.ResourceType) pkger.SummaryLabelMapping {
return pkger.SummaryLabelMapping{
ResourceName: name,
Expand All @@ -244,7 +252,7 @@ func TestLauncher_Pkger(t *testing.T) {
}

mappings := sum1.LabelMappings
require.Len(t, mappings, 4)
require.Len(t, mappings, 5)
hasMapping(t, mappings, newSumMapping(bkts[0].ID, bkts[0].Name, influxdb.BucketsResourceType))
hasMapping(t, mappings, newSumMapping(influxdb.ID(dashs[0].ID), dashs[0].Name, influxdb.DashboardsResourceType))
hasMapping(t, mappings, newSumMapping(vars[0].ID, vars[0].Name, influxdb.VariablesResourceType))
Expand All @@ -258,6 +266,7 @@ func TestLauncher_Pkger(t *testing.T) {

require.Equal(t, sum1.Buckets, sum2.Buckets)
require.Equal(t, sum1.Labels, sum2.Labels)
require.Equal(t, sum1.NotificationEndpoints, sum2.NotificationEndpoints)
require.Equal(t, sum1.Variables, sum2.Variables)

// dashboards should be new
Expand Down Expand Up @@ -462,6 +471,7 @@ spec:
name: http_none_auth_notification_endpoint
type: none
description: http none auth desc
method: GET
url: https://www.example.com/endpoint/noneauth
status: inactive
associations:
Expand Down
51 changes: 46 additions & 5 deletions http/notification_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (s *NotificationEndpointService) CreateNotificationEndpoint(ctx context.Con
// the token/auth. its a nothing burger here
var resp notificationEndpointDecoder
err := s.Client.
Post(httpc.BodyJSON(ne), prefixNotificationEndpoints).
Post(httpc.BodyJSON(&notificationEndpointEncoder{ne: ne}), prefixNotificationEndpoints).
DecodeJSON(&resp).
Do(ctx)
if err != nil {
Expand All @@ -675,10 +675,11 @@ func (s *NotificationEndpointService) CreateNotificationEndpoint(ctx context.Con

// UpdateNotificationEndpoint updates a single notification endpoint.
// Returns the new notification endpoint after update.
func (s *NotificationEndpointService) UpdateNotificationEndpoint(ctx context.Context, id influxdb.ID, nr influxdb.NotificationEndpoint, userID influxdb.ID) (influxdb.NotificationEndpoint, error) {
func (s *NotificationEndpointService) UpdateNotificationEndpoint(ctx context.Context, id influxdb.ID, ne influxdb.NotificationEndpoint, userID influxdb.ID) (influxdb.NotificationEndpoint, error) {
// userID is ignored since userID is grabbed off the http auth set on the client
var resp notificationEndpointDecoder
err := s.Client.
Put(httpc.BodyJSON(nr), prefixNotificationEndpoints, id.String()).
Put(httpc.BodyJSON(&notificationEndpointEncoder{ne: ne}), prefixNotificationEndpoints, id.String()).
DecodeJSON(&resp).
Do(ctx)
if err != nil {
Expand Down Expand Up @@ -706,8 +707,48 @@ func (s *NotificationEndpointService) PatchNotificationEndpoint(ctx context.Cont
}

// DeleteNotificationEndpoint removes a notification endpoint by ID, returns secret fields, orgID for further deletion.
func (s *NotificationEndpointService) DeleteNotificationEndpoint(ctx context.Context, id influxdb.ID) (flds []influxdb.SecretField, orgID influxdb.ID, err error) {
panic("not implemented")
// TODO: axe this delete design, makes little sense in how its currently being done. Right now, as an http client,
// I am forced to know how the store handles this and then figure out what the server does in between me and that store,
// then see what falls out :flushed... for now returning nothing for secrets, orgID, and only returning an error. This makes
// the code/design smell super obvious imo
func (s *NotificationEndpointService) DeleteNotificationEndpoint(ctx context.Context, id influxdb.ID) ([]influxdb.SecretField, influxdb.ID, error) {
err := s.Client.
Delete(prefixNotificationEndpoints, id.String()).
Do(ctx)
return nil, 0, err
}

type notificationEndpointEncoder struct {
ne influxdb.NotificationEndpoint
}

func (n *notificationEndpointEncoder) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(n.ne)
if err != nil {
return nil, err
}

ughhh := make(map[string]interface{})
if err := json.Unmarshal(b, &ughhh); err != nil {
return nil, err
}
n.ne.BackfillSecretKeys()

// this makes me queezy and altogether sad
fieldMap := map[string]string{
"-password": "password",
"-routing-key": "routingKey",
"-token": "token",
"-username": "username",
}
for _, sec := range n.ne.SecretFields() {
var v string
if sec.Value != nil {
v = *sec.Value
}
ughhh[fieldMap[sec.Key]] = v
}
return json.Marshal(ughhh)
}

type notificationEndpointDecoder struct {
Expand Down
25 changes: 25 additions & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7217,6 +7217,17 @@ components:
type: string
labelID:
type: string
notificationEndpoints:
type: array
items:
allOf:
- $ref: "#/components/schemas/NotificationEndpointDiscrimator"
- type: object
properties:
labelAssociations:
type: array
items:
$ref: "#/components/schemas/Label"
telegrafConfigs:
type: array
items:
Expand Down Expand Up @@ -7318,6 +7329,19 @@ components:
type: string
labelName:
type: string
notificationEndpoints:
type: array
items:
type: object
properties:
id:
type: string
name:
type: string
new:
$ref: "#/components/schemas/NotificationEndpointDiscrimator"
old:
$ref: "#/components/schemas/NotificationEndpointDiscrimator"
telegrafConfigs:
type: array
items:
Expand Down Expand Up @@ -10296,6 +10320,7 @@ components:
type: string
NotificationEndpointUpdate:
type: object

properties:
name:
type: string
Expand Down
24 changes: 18 additions & 6 deletions mock/notification_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ var _ influxdb.NotificationEndpointService = &NotificationEndpointService{}
type NotificationEndpointService struct {
*OrganizationService
*UserResourceMappingService
FindNotificationEndpointByIDF func(ctx context.Context, id influxdb.ID) (influxdb.NotificationEndpoint, error)
FindNotificationEndpointsF func(ctx context.Context, filter influxdb.NotificationEndpointFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error)
CreateNotificationEndpointF func(ctx context.Context, nr influxdb.NotificationEndpoint, userID influxdb.ID) error
UpdateNotificationEndpointF func(ctx context.Context, id influxdb.ID, nr influxdb.NotificationEndpoint, userID influxdb.ID) (influxdb.NotificationEndpoint, error)
PatchNotificationEndpointF func(ctx context.Context, id influxdb.ID, upd influxdb.NotificationEndpointUpdate) (influxdb.NotificationEndpoint, error)
DeleteNotificationEndpointF func(ctx context.Context, id influxdb.ID) ([]influxdb.SecretField, influxdb.ID, error)
FindNotificationEndpointByIDF func(ctx context.Context, id influxdb.ID) (influxdb.NotificationEndpoint, error)
FindNotificationEndpointByIDCalls SafeCount
FindNotificationEndpointsF func(ctx context.Context, filter influxdb.NotificationEndpointFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error)
FindNotificationEndpointsCalls SafeCount
CreateNotificationEndpointF func(ctx context.Context, nr influxdb.NotificationEndpoint, userID influxdb.ID) error
CreateNotificationEndpointCalls SafeCount
UpdateNotificationEndpointF func(ctx context.Context, id influxdb.ID, nr influxdb.NotificationEndpoint, userID influxdb.ID) (influxdb.NotificationEndpoint, error)
UpdateNotificationEndpointCalls SafeCount
PatchNotificationEndpointF func(ctx context.Context, id influxdb.ID, upd influxdb.NotificationEndpointUpdate) (influxdb.NotificationEndpoint, error)
PatchNotificationEndpointCalls SafeCount
DeleteNotificationEndpointF func(ctx context.Context, id influxdb.ID) ([]influxdb.SecretField, influxdb.ID, error)
DeleteNotificationEndpointCalls SafeCount
}

func NewNotificationEndpointService() *NotificationEndpointService {
Expand Down Expand Up @@ -47,33 +53,39 @@ func NewNotificationEndpointService() *NotificationEndpointService {

// FindNotificationEndpointByID returns a single telegraf config by ID.
func (s *NotificationEndpointService) FindNotificationEndpointByID(ctx context.Context, id influxdb.ID) (influxdb.NotificationEndpoint, error) {
defer s.FindNotificationEndpointByIDCalls.IncrFn()()
return s.FindNotificationEndpointByIDF(ctx, id)
}

// FindNotificationEndpoints returns a list of notification rules that match filter and the total count of matching notification rules.
// Additional options provide pagination & sorting.
func (s *NotificationEndpointService) FindNotificationEndpoints(ctx context.Context, filter influxdb.NotificationEndpointFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error) {
defer s.FindNotificationEndpointsCalls.IncrFn()()
return s.FindNotificationEndpointsF(ctx, filter, opt...)
}

// CreateNotificationEndpoint creates a new notification rule and sets ID with the new identifier.
func (s *NotificationEndpointService) CreateNotificationEndpoint(ctx context.Context, nr influxdb.NotificationEndpoint, userID influxdb.ID) error {
defer s.CreateNotificationEndpointCalls.IncrFn()()
return s.CreateNotificationEndpointF(ctx, nr, userID)
}

// UpdateNotificationEndpoint updates a single notification rule.
// Returns the new notification rule after update.
func (s *NotificationEndpointService) UpdateNotificationEndpoint(ctx context.Context, id influxdb.ID, nr influxdb.NotificationEndpoint, userID influxdb.ID) (influxdb.NotificationEndpoint, error) {
defer s.UpdateNotificationEndpointCalls.IncrFn()()
return s.UpdateNotificationEndpointF(ctx, id, nr, userID)
}

// PatchNotificationEndpoint updates a single notification rule with changeset.
// Returns the new notification rule after update.
func (s *NotificationEndpointService) PatchNotificationEndpoint(ctx context.Context, id influxdb.ID, upd influxdb.NotificationEndpointUpdate) (influxdb.NotificationEndpoint, error) {
defer s.PatchNotificationEndpointCalls.IncrFn()()
return s.PatchNotificationEndpointF(ctx, id, upd)
}

// DeleteNotificationEndpoint removes a notification rule by ID.
func (s *NotificationEndpointService) DeleteNotificationEndpoint(ctx context.Context, id influxdb.ID) ([]influxdb.SecretField, influxdb.ID, error) {
defer s.DeleteNotificationEndpointCalls.IncrFn()()
return s.DeleteNotificationEndpointF(ctx, id)
}
3 changes: 1 addition & 2 deletions notification/endpoint/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ func (s HTTP) Valid() error {
return nil
}

type httpAlias HTTP

// MarshalJSON implement json.Marshaler interface.
func (s HTTP) MarshalJSON() ([]byte, error) {
type httpAlias HTTP
return json.Marshal(
struct {
httpAlias
Expand Down
Loading

0 comments on commit 3daaa4d

Please sign in to comment.