Skip to content

Commit fe565f5

Browse files
committed
do not use isDestroy flag and read prevent_destroy directly
1 parent 9118ceb commit fe565f5

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

bundle/phases/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func approvalForDeploy(ctx context.Context, b *bundle.Bundle) (bool, error) {
5252
return false, err
5353
}
5454

55-
err = checkForPreventDestroy(b, actions, false)
55+
err = checkForPreventDestroy(b, actions)
5656
if err != nil {
5757
return false, err
5858
}

bundle/phases/destroy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func approvalForDestroy(ctx context.Context, b *bundle.Bundle) (bool, error) {
6565
return false, err
6666
}
6767

68-
err = checkForPreventDestroy(b, deleteActions, true)
68+
err = checkForPreventDestroy(b, deleteActions)
6969
if err != nil {
7070
return false, err
7171
}

bundle/phases/plan.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,26 @@ func deployPrepare(ctx context.Context, b *bundle.Bundle) map[string][]libraries
5252

5353
// checkForPreventDestroy checks if the resource has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed.
5454
// If it does, it returns an error.
55-
func checkForPreventDestroy(b *bundle.Bundle, actions []deployplan.Action, isDestroy bool) error {
55+
func checkForPreventDestroy(b *bundle.Bundle, actions []deployplan.Action) error {
5656
root := b.Config.Value()
5757
for _, action := range actions {
58-
// If the action is not a recreate or a delete as part of destroy - skip checking for prevent destroy
59-
// We allow delete as part of deploy though (hence isDestroy check) because we mimic the behavior of terraform which allows such resources to be removed from config.
60-
if action.ActionType != deployplan.ActionTypeRecreate && (!isDestroy || action.ActionType != deployplan.ActionTypeDelete) {
58+
if action.ActionType != deployplan.ActionTypeRecreate && action.ActionType != deployplan.ActionTypeDelete {
6159
continue
6260
}
6361

64-
path := dyn.NewPath(dyn.Key("resources"), dyn.Key(action.Group), dyn.Key(action.Key), dyn.Key("lifecycle"))
65-
lifecycleV, err := dyn.GetByPath(root, path)
66-
// If there is no lifecycle, skip
62+
path := dyn.NewPath(dyn.Key("resources"), dyn.Key(action.Group), dyn.Key(action.Key), dyn.Key("lifecycle"), dyn.Key("prevent_destroy"))
63+
// If there is no prevent_destroy, skip
64+
preventDestroyV, err := dyn.GetByPath(root, path)
6765
if err != nil {
6866
return nil
6967
}
7068

71-
if lifecycleV.Kind() == dyn.KindMap {
72-
preventDestroyV := lifecycleV.Get("prevent_destroy")
73-
preventDestroy, ok := preventDestroyV.AsBool()
74-
if ok && preventDestroy {
75-
return fmt.Errorf("resource %s has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for %s.%s", action.Key, action.Group, action.Key)
76-
}
69+
preventDestroy, ok := preventDestroyV.AsBool()
70+
if !ok {
71+
return fmt.Errorf("internal error: prevent_destroy is not a boolean for %s.%s", action.Group, action.Key)
72+
}
73+
if preventDestroy {
74+
return fmt.Errorf("resource %s has lifecycle.prevent_destroy set, but the plan calls for this resource to be recreated or destroyed. To avoid this error, disable lifecycle.prevent_destroy for %s.%s", action.Key, action.Group, action.Key)
7775
}
7876
}
7977
return nil

bundle/phases/plan_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestCheckPreventDestroyForAllResources(t *testing.T) {
5454
},
5555
}
5656

57-
err := checkForPreventDestroy(b, actions, false)
57+
err := checkForPreventDestroy(b, actions)
5858
require.Error(t, err)
5959
require.Contains(t, err.Error(), "resource test_resource has lifecycle.prevent_destroy set")
6060
require.Contains(t, err.Error(), "but the plan calls for this resource to be recreated or destroyed")

0 commit comments

Comments
 (0)