6 "github.com/hashicorp/terraform/plans"
8 "github.com/hashicorp/hcl2/hcl"
10 "github.com/hashicorp/terraform/addrs"
11 "github.com/hashicorp/terraform/configs"
12 "github.com/hashicorp/terraform/tfdiags"
15 // EvalPreventDestroy is an EvalNode implementation that returns an
16 // error if a resource has PreventDestroy configured and the diff
17 // would destroy the resource.
18 type EvalCheckPreventDestroy struct {
19 Addr addrs.ResourceInstance
20 Config *configs.Resource
21 Change **plans.ResourceInstanceChange
24 func (n *EvalCheckPreventDestroy) Eval(ctx EvalContext) (interface{}, error) {
25 if n.Change == nil || *n.Change == nil || n.Config == nil || n.Config.Managed == nil {
30 preventDestroy := n.Config.Managed.PreventDestroy
32 if (change.Action == plans.Delete || change.Action.IsReplace()) && preventDestroy {
33 var diags tfdiags.Diagnostics
34 diags = diags.Append(&hcl.Diagnostic{
35 Severity: hcl.DiagError,
36 Summary: "Instance cannot be destroyed",
38 "Resource %s has lifecycle.prevent_destroy set, but the plan calls for this resource to be destroyed. To avoid this error and continue with the plan, either disable lifecycle.prevent_destroy or reduce the scope of the plan using the -target flag.",
39 n.Addr.Absolute(ctx.Path()).String(),
41 Subject: &n.Config.DeclRange,
43 return nil, diags.Err()
49 const preventDestroyErrStr = `%s: the plan would destroy this resource, but it currently has lifecycle.prevent_destroy set to true. To avoid this error and continue with the plan, either disable lifecycle.prevent_destroy or adjust the scope of the plan using the -target flag.`