Skip to content

Commit

Permalink
chore(pkger): handle edge cases for telegraf configs that manifest fr…
Browse files Browse the repository at this point in the history
…om user interaction

closes: #17434
  • Loading branch information
jsteenb2 committed May 6, 2020
1 parent 5a304cf commit 7c9b4d3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
42 changes: 42 additions & 0 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,42 @@ func TestLauncher_Pkger(t *testing.T) {
})
})
})

t.Run("when a user has deleted a telegraf config that was previously created by a stack", func(t *testing.T) {
testUserDeletedTelegraf := func(t *testing.T, actionFn func(t *testing.T, stackID influxdb.ID, initialObj pkger.Object, initialSum pkger.Summary)) {
t.Helper()

obj := newTelegrafObject("tele-1", "", "")
stackID, cleanup, initialSum := initializeStackPkg(t, newPkg(obj))
defer cleanup()

require.Len(t, initialSum.TelegrafConfigs, 1)
require.NotZero(t, initialSum.TelegrafConfigs[0].TelegrafConfig.ID)
resourceCheck.mustDeleteTelegrafConfig(t, initialSum.TelegrafConfigs[0].TelegrafConfig.ID)

actionFn(t, stackID, obj, initialSum)
}

t.Run("should create new resource when attempting to update", func(t *testing.T) {
testUserDeletedTelegraf(t, func(t *testing.T, stackID influxdb.ID, initialObj pkger.Object, initialSum pkger.Summary) {
pkg := newPkg(initialObj)
updateSum, _, err := svc.Apply(ctx, l.Org.ID, l.User.ID, pkg, pkger.ApplyWithStackID(stackID))
require.NoError(t, err)

require.Len(t, updateSum.TelegrafConfigs, 1)
initial, updated := initialSum.TelegrafConfigs[0].TelegrafConfig, updateSum.TelegrafConfigs[0].TelegrafConfig
assert.NotEqual(t, initial.ID, updated.ID)
initial.ID, updated.ID = 0, 0
assert.Equal(t, initial, updated)
})
})

t.Run("should not error when attempting to remove", func(t *testing.T) {
testUserDeletedTelegraf(t, func(t *testing.T, stackID influxdb.ID, _ pkger.Object, _ pkger.Summary) {
testValidRemoval(t, stackID)
})
})
})
})
})

Expand Down Expand Up @@ -3098,6 +3134,12 @@ func (r resourceChecker) mustGetTelegrafConfig(t *testing.T, getOpt getResourceO
return tele
}

func (r resourceChecker) mustDeleteTelegrafConfig(t *testing.T, id influxdb.ID) {
t.Helper()

require.NoError(t, r.tl.TelegrafService(t).DeleteTelegrafConfig(ctx, id))
}

func (r resourceChecker) getVariable(t *testing.T, getOpt getResourceOptFn) (influxdb.Variable, error) {
t.Helper()

Expand Down
13 changes: 10 additions & 3 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2340,13 +2340,16 @@ func (s *Service) applyTelegrafs(ctx context.Context, userID influxdb.ID, teles
}

func (s *Service) applyTelegrafConfig(ctx context.Context, userID influxdb.ID, t *stateTelegraf) (influxdb.TelegrafConfig, error) {
switch t.stateStatus {
case StateStatusRemove:
switch {
case IsRemoval(t.stateStatus):
if err := s.teleSVC.DeleteTelegrafConfig(ctx, t.ID()); err != nil {
if influxdb.ErrorCode(err) == influxdb.ENotFound {
return influxdb.TelegrafConfig{}, nil
}
return influxdb.TelegrafConfig{}, ierrors.Wrap(err, "failed to delete config")
}
return *t.existing, nil
case StateStatusExists:
case IsExisting(t.stateStatus) && t.existing != nil:
cfg := t.summarize().TelegrafConfig
updatedConfig, err := s.teleSVC.UpdateTelegrafConfig(ctx, t.ID(), &cfg, userID)
if err != nil {
Expand All @@ -2365,6 +2368,10 @@ func (s *Service) applyTelegrafConfig(ctx context.Context, userID influxdb.ID, t

func (s *Service) rollbackTelegrafConfigs(ctx context.Context, userID influxdb.ID, cfgs []*stateTelegraf) error {
rollbackFn := func(t *stateTelegraf) error {
if !IsNew(t.stateStatus) && t.existing == nil {
return nil
}

var err error
switch t.stateStatus {
case StateStatusRemove:
Expand Down

0 comments on commit 7c9b4d3

Please sign in to comment.