2015-04-16 17:57:18 -05:00
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 {
2016-10-28 20:31:47 -05:00
Resource * config . Resource
ResourceId string
Diff * * InstanceDiff
2015-04-16 17:57:18 -05:00
}
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
2016-07-29 12:17:48 -05:00
if diff . GetDestroy ( ) && preventDestroy {
2016-10-28 20:31:47 -05:00
resourceId := n . ResourceId
if resourceId == "" {
resourceId = n . Resource . Id ( )
}
return nil , fmt . Errorf ( preventDestroyErrStr , resourceId )
2015-04-16 17:57:18 -05:00
}
return nil , nil
}
2015-08-12 18:41:12 -05:00
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. `