Skip to content

Commit

Permalink
chore(pkger): handle edge cases for labels that manifest from user in…
Browse files Browse the repository at this point in the history
…teraction

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

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

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

require.Len(t, initialSum.Labels, 1)
require.NotZero(t, initialSum.Labels[0].ID)
resourceCheck.mustDeleteLabel(t, influxdb.ID(initialSum.Labels[0].ID))

actionFn(t, stackID, obj, initialSum)
}

t.Run("should create new resource when attempting to update", func(t *testing.T) {
testUserDeletedLabel(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.Labels, 1)
initial, updated := initialSum.Labels[0], updateSum.Labels[0]
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) {
testUserDeletedLabel(t, func(t *testing.T, stackID influxdb.ID, initialObj pkger.Object, initialSum pkger.Summary) {
testValidRemoval(t, stackID)
})
})
})
})
})

Expand Down Expand Up @@ -2800,6 +2836,11 @@ func (r resourceChecker) mustGetLabel(t *testing.T, getOpt getResourceOptFn) inf
return l
}

func (r resourceChecker) mustDeleteLabel(t *testing.T, id influxdb.ID) {
t.Helper()
require.NoError(t, r.tl.LabelService(t).DeleteLabel(ctx, id))
}

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

Expand Down
19 changes: 13 additions & 6 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,11 +1714,15 @@ func (s *Service) applyLabels(ctx context.Context, labels []*stateLabel) applier

func (s *Service) rollbackLabels(ctx context.Context, labels []*stateLabel) error {
rollbackFn := func(l *stateLabel) error {
if !IsNew(l.stateStatus) && l.existing == nil {
return nil
}

var err error
switch l.stateStatus {
case StateStatusRemove:
switch {
case IsRemoval(l.stateStatus):
err = s.labelSVC.CreateLabel(ctx, l.existing)
case StateStatusExists:
case IsExisting(l.stateStatus):
_, err = s.labelSVC.UpdateLabel(ctx, l.ID(), influxdb.LabelUpdate{
Name: l.parserLabel.Name(),
Properties: l.existing.Properties,
Expand Down Expand Up @@ -1748,10 +1752,10 @@ func (s *Service) applyLabel(ctx context.Context, l *stateLabel) (influxdb.Label
influxLabel *influxdb.Label
err error
)
switch l.stateStatus {
case StateStatusRemove:
switch {
case IsRemoval(l.stateStatus):
influxLabel, err = l.existing, s.labelSVC.DeleteLabel(ctx, l.ID())
case StateStatusExists:
case IsExisting(l.stateStatus) && l.existing != nil:
influxLabel, err = s.labelSVC.UpdateLabel(ctx, l.ID(), influxdb.LabelUpdate{
Name: l.parserLabel.Name(),
Properties: l.properties(),
Expand All @@ -1762,6 +1766,9 @@ func (s *Service) applyLabel(ctx context.Context, l *stateLabel) (influxdb.Label
influxLabel = &creatLabel
err = ierrors.Wrap(s.labelSVC.CreateLabel(ctx, &creatLabel), "creating")
}
if influxdb.ErrorCode(err) == influxdb.ENotFound {
return influxdb.Label{}, nil
}
if err != nil || influxLabel == nil {
return influxdb.Label{}, err
}
Expand Down

0 comments on commit e4afcec

Please sign in to comment.