opentofu/terraform/eval_check_prevent_destroy.go
Mitchell Hashimoto a332c121bc
terraform: prevent_destroy works for decreasing count
Fixes #5826

The `prevent_destroy` lifecycle configuration was not being checked when
the count was decreased for a resource with a count. It was only
checking when attributes changed on pre-existing resources.

This fixes that.
2016-10-28 21:31:47 -04:00

39 lines
1.1 KiB
Go

package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
)
// EvalPreventDestroy is an EvalNode implementation that returns an
// error if a resource has PreventDestroy configured and the diff
// would destroy the resource.
type EvalCheckPreventDestroy struct {
Resource *config.Resource
ResourceId string
Diff **InstanceDiff
}
func (n *EvalCheckPreventDestroy) Eval(ctx EvalContext) (interface{}, error) {
if n.Diff == nil || *n.Diff == nil || n.Resource == nil {
return nil, nil
}
diff := *n.Diff
preventDestroy := n.Resource.Lifecycle.PreventDestroy
if diff.GetDestroy() && preventDestroy {
resourceId := n.ResourceId
if resourceId == "" {
resourceId = n.Resource.Id()
}
return nil, fmt.Errorf(preventDestroyErrStr, resourceId)
}
return nil, nil
}
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.`