opentofu/terraform/eval_check_prevent_destroy.go
Paul Hinze afe4abb637 core: add prevent_destroy lifecycle flag
When the `prevent_destroy` flag is set on a resource, any plan that
would destroy that resource instead returns an error. This has the
effect of preventing the resource from being unexpectedly destroyed by
Terraform until the flag is removed from the config.
2015-04-17 10:40:04 -05:00

33 lines
908 B
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
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.Destroy && preventDestroy {
return nil, fmt.Errorf(preventDestroyErrStr, n.Resource.Id())
}
return nil, nil
}
const preventDestroyErrStr = `%s: plan would destroy, but resource has prevent_destroy set. To avoid this error, either disable prevent_destroy, or change your config so the plan does not destroy this resource.`