github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/eval_check_prevent_destroy.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  )
     8  
     9  // EvalPreventDestroy is an EvalNode implementation that returns an
    10  // error if a resource has PreventDestroy configured and the diff
    11  // would destroy the resource.
    12  type EvalCheckPreventDestroy struct {
    13  	Resource   *config.Resource
    14  	ResourceId string
    15  	Diff       **InstanceDiff
    16  }
    17  
    18  func (n *EvalCheckPreventDestroy) Eval(ctx EvalContext) (interface{}, error) {
    19  	if n.Diff == nil || *n.Diff == nil || n.Resource == nil {
    20  		return nil, nil
    21  	}
    22  
    23  	diff := *n.Diff
    24  	preventDestroy := n.Resource.Lifecycle.PreventDestroy
    25  
    26  	if diff.GetDestroy() && preventDestroy {
    27  		resourceId := n.ResourceId
    28  		if resourceId == "" {
    29  			resourceId = n.Resource.Id()
    30  		}
    31  
    32  		return nil, fmt.Errorf(preventDestroyErrStr, resourceId)
    33  	}
    34  
    35  	return nil, nil
    36  }
    37  
    38  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.`