2018-09-20 14:30:52 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConcreteResourceInstanceDeposedNodeFunc is a callback type used to convert
|
|
|
|
// an abstract resource instance to a concrete one of some type that has
|
|
|
|
// an associated deposed object key.
|
|
|
|
type ConcreteResourceInstanceDeposedNodeFunc func(*NodeAbstractResourceInstance, states.DeposedKey) dag.Vertex
|
|
|
|
|
2018-09-21 19:08:52 -05:00
|
|
|
type GraphNodeDeposedResourceInstanceObject interface {
|
|
|
|
DeposedInstanceObjectKey() states.DeposedKey
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:30:52 -05:00
|
|
|
// NodePlanDeposedResourceInstanceObject represents deposed resource
|
|
|
|
// instance objects during plan. These are distinct from the primary object
|
|
|
|
// for each resource instance since the only valid operation to do with them
|
|
|
|
// is to destroy them.
|
|
|
|
//
|
|
|
|
// This node type is also used during the refresh walk to ensure that the
|
|
|
|
// record of a deposed object is up-to-date before we plan to destroy it.
|
|
|
|
type NodePlanDeposedResourceInstanceObject struct {
|
|
|
|
*NodeAbstractResourceInstance
|
|
|
|
DeposedKey states.DeposedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeDeposedResourceInstanceObject = (*NodePlanDeposedResourceInstanceObject)(nil)
|
2020-03-15 10:32:06 -05:00
|
|
|
_ GraphNodeConfigResource = (*NodePlanDeposedResourceInstanceObject)(nil)
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeResourceInstance = (*NodePlanDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*NodePlanDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeReferencer = (*NodePlanDeposedResourceInstanceObject)(nil)
|
2020-09-29 09:58:35 -05:00
|
|
|
_ GraphNodeExecutable = (*NodePlanDeposedResourceInstanceObject)(nil)
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeProviderConsumer = (*NodePlanDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeProvisionerConsumer = (*NodePlanDeposedResourceInstanceObject)(nil)
|
2018-09-20 14:30:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (n *NodePlanDeposedResourceInstanceObject) Name() string {
|
2018-09-27 19:55:14 -05:00
|
|
|
return fmt.Sprintf("%s (deposed %s)", n.ResourceInstanceAddr().String(), n.DeposedKey)
|
2018-09-20 14:30:52 -05:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:08:52 -05:00
|
|
|
func (n *NodePlanDeposedResourceInstanceObject) DeposedInstanceObjectKey() states.DeposedKey {
|
|
|
|
return n.DeposedKey
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:30:52 -05:00
|
|
|
// GraphNodeReferenceable implementation, overriding the one from NodeAbstractResourceInstance
|
|
|
|
func (n *NodePlanDeposedResourceInstanceObject) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
// Deposed objects don't participate in references.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer implementation, overriding the one from NodeAbstractResourceInstance
|
|
|
|
func (n *NodePlanDeposedResourceInstanceObject) References() []*addrs.Reference {
|
|
|
|
// We don't evaluate configuration for deposed objects, so they effectively
|
|
|
|
// make no references.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
2020-09-29 09:58:35 -05:00
|
|
|
func (n *NodePlanDeposedResourceInstanceObject) Execute(ctx EvalContext, op walkOperation) error {
|
2018-09-20 14:30:52 -05:00
|
|
|
addr := n.ResourceInstanceAddr()
|
|
|
|
|
2020-09-29 09:58:35 -05:00
|
|
|
provider, providerSchema, err := GetProvider(ctx, n.ResolvedProvider)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-20 14:30:52 -05:00
|
|
|
|
|
|
|
// During the plan walk we always produce a planned destroy change, because
|
|
|
|
// destroying is the only supported action for deposed objects.
|
|
|
|
var change *plans.ResourceInstanceChange
|
2020-09-29 09:58:35 -05:00
|
|
|
var state *states.ResourceInstanceObject
|
|
|
|
|
2020-10-06 16:14:53 -05:00
|
|
|
readStateDeposed := &EvalReadStateDeposed{
|
|
|
|
Addr: addr.Resource,
|
|
|
|
Output: &state,
|
|
|
|
Key: n.DeposedKey,
|
|
|
|
Provider: &provider,
|
|
|
|
ProviderSchema: &providerSchema,
|
2020-09-29 09:58:35 -05:00
|
|
|
}
|
2020-10-06 16:14:53 -05:00
|
|
|
_, err = readStateDeposed.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
diffDestroy := &EvalDiffDestroy{
|
|
|
|
Addr: addr.Resource,
|
|
|
|
ProviderAddr: n.ResolvedProvider,
|
|
|
|
DeposedKey: n.DeposedKey,
|
|
|
|
State: &state,
|
|
|
|
Output: &change,
|
|
|
|
}
|
|
|
|
_, err = diffDestroy.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writeDiff := &EvalWriteDiff{
|
|
|
|
Addr: addr.Resource,
|
|
|
|
DeposedKey: n.DeposedKey,
|
|
|
|
ProviderSchema: &providerSchema,
|
|
|
|
Change: &change,
|
|
|
|
}
|
|
|
|
_, err = writeDiff.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-29 09:58:35 -05:00
|
|
|
return nil
|
2018-09-20 14:30:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeDestroyDeposedResourceInstanceObject represents deposed resource
|
|
|
|
// instance objects during apply. Nodes of this type are inserted by
|
|
|
|
// DiffTransformer when the planned changeset contains "delete" changes for
|
|
|
|
// deposed instance objects, and its only supported operation is to destroy
|
|
|
|
// and then forget the associated object.
|
|
|
|
type NodeDestroyDeposedResourceInstanceObject struct {
|
|
|
|
*NodeAbstractResourceInstance
|
|
|
|
DeposedKey states.DeposedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeDeposedResourceInstanceObject = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
2020-03-15 10:32:06 -05:00
|
|
|
_ GraphNodeConfigResource = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeResourceInstance = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeDestroyer = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeDestroyerCBD = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeReferencer = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
2020-09-29 09:58:35 -05:00
|
|
|
_ GraphNodeExecutable = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
2018-09-21 19:08:52 -05:00
|
|
|
_ GraphNodeProviderConsumer = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
|
|
|
_ GraphNodeProvisionerConsumer = (*NodeDestroyDeposedResourceInstanceObject)(nil)
|
2018-09-20 14:30:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) Name() string {
|
2019-11-19 16:30:27 -06:00
|
|
|
return fmt.Sprintf("%s (destroy deposed %s)", n.ResourceInstanceAddr(), n.DeposedKey)
|
2018-09-20 14:30:52 -05:00
|
|
|
}
|
|
|
|
|
2018-09-21 19:08:52 -05:00
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) DeposedInstanceObjectKey() states.DeposedKey {
|
|
|
|
return n.DeposedKey
|
|
|
|
}
|
|
|
|
|
2018-09-20 14:30:52 -05:00
|
|
|
// GraphNodeReferenceable implementation, overriding the one from NodeAbstractResourceInstance
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
// Deposed objects don't participate in references.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer implementation, overriding the one from NodeAbstractResourceInstance
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) References() []*addrs.Reference {
|
|
|
|
// We don't evaluate configuration for deposed objects, so they effectively
|
|
|
|
// make no references.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDestroyer
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) DestroyAddr() *addrs.AbsResourceInstance {
|
|
|
|
addr := n.ResourceInstanceAddr()
|
|
|
|
return &addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDestroyerCBD
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) CreateBeforeDestroy() bool {
|
|
|
|
// A deposed instance is always CreateBeforeDestroy by definition, since
|
|
|
|
// we use deposed only to handle create-before-destroy.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDestroyerCBD
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) ModifyCreateBeforeDestroy(v bool) error {
|
|
|
|
if !v {
|
|
|
|
// Should never happen: deposed instances are _always_ create_before_destroy.
|
|
|
|
return fmt.Errorf("can't deactivate create_before_destroy for a deposed instance")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 09:58:35 -05:00
|
|
|
// GraphNodeExecutable impl.
|
|
|
|
func (n *NodeDestroyDeposedResourceInstanceObject) Execute(ctx EvalContext, op walkOperation) error {
|
|
|
|
addr := n.ResourceInstanceAddr().Resource
|
2018-09-20 14:30:52 -05:00
|
|
|
|
|
|
|
var state *states.ResourceInstanceObject
|
|
|
|
var change *plans.ResourceInstanceChange
|
2020-09-29 09:58:35 -05:00
|
|
|
var applyError error
|
|
|
|
|
|
|
|
provider, providerSchema, err := GetProvider(ctx, n.ResolvedProvider)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-20 14:30:52 -05:00
|
|
|
}
|
2020-09-29 09:58:35 -05:00
|
|
|
|
|
|
|
readStateDeposed := &EvalReadStateDeposed{
|
|
|
|
Addr: addr,
|
|
|
|
Output: &state,
|
|
|
|
Key: n.DeposedKey,
|
|
|
|
Provider: &provider,
|
|
|
|
ProviderSchema: &providerSchema,
|
|
|
|
}
|
|
|
|
_, err = readStateDeposed.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
diffDestroy := &EvalDiffDestroy{
|
|
|
|
Addr: addr,
|
|
|
|
ProviderAddr: n.ResolvedProvider,
|
|
|
|
State: &state,
|
|
|
|
Output: &change,
|
|
|
|
}
|
|
|
|
_, err = diffDestroy.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call pre-apply hook
|
|
|
|
applyPre := &EvalApplyPre{
|
|
|
|
Addr: addr,
|
|
|
|
State: &state,
|
|
|
|
Change: &change,
|
|
|
|
}
|
|
|
|
_, err = applyPre.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apply := &EvalApply{
|
|
|
|
Addr: addr,
|
|
|
|
Config: nil, // No configuration because we are destroying
|
|
|
|
State: &state,
|
|
|
|
Change: &change,
|
|
|
|
Provider: &provider,
|
|
|
|
ProviderAddr: n.ResolvedProvider,
|
|
|
|
ProviderSchema: &providerSchema,
|
|
|
|
Output: &state,
|
|
|
|
Error: &applyError,
|
|
|
|
}
|
|
|
|
_, err = apply.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always write the resource back to the state deposed. If it
|
|
|
|
// was successfully destroyed it will be pruned. If it was not, it will
|
|
|
|
// be caught on the next run.
|
|
|
|
writeStateDeposed := &EvalWriteStateDeposed{
|
|
|
|
Addr: addr,
|
|
|
|
Key: n.DeposedKey,
|
|
|
|
ProviderAddr: n.ResolvedProvider,
|
|
|
|
ProviderSchema: &providerSchema,
|
|
|
|
State: &state,
|
|
|
|
}
|
|
|
|
_, err = writeStateDeposed.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
applyPost := &EvalApplyPost{
|
|
|
|
Addr: addr,
|
|
|
|
State: &state,
|
|
|
|
Error: &applyError,
|
|
|
|
}
|
|
|
|
_, err = applyPost.Eval(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if applyError != nil {
|
|
|
|
return applyError
|
|
|
|
}
|
|
|
|
UpdateStateHook(ctx)
|
|
|
|
return nil
|
2018-09-20 14:30:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDeposer is an optional interface implemented by graph nodes that
|
|
|
|
// might create a single new deposed object for a specific associated resource
|
|
|
|
// instance, allowing a caller to optionally pre-allocate a DeposedKey for
|
|
|
|
// it.
|
|
|
|
type GraphNodeDeposer interface {
|
|
|
|
// SetPreallocatedDeposedKey will be called during graph construction
|
|
|
|
// if a particular node must use a pre-allocated deposed key if/when it
|
|
|
|
// "deposes" the current object of its associated resource instance.
|
|
|
|
SetPreallocatedDeposedKey(key states.DeposedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// graphNodeDeposer is an embeddable implementation of GraphNodeDeposer.
|
|
|
|
// Embed it in a node type to get automatic support for it, and then access
|
|
|
|
// the field PreallocatedDeposedKey to access any pre-allocated key.
|
|
|
|
type graphNodeDeposer struct {
|
|
|
|
PreallocatedDeposedKey states.DeposedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeDeposer) SetPreallocatedDeposedKey(key states.DeposedKey) {
|
|
|
|
n.PreallocatedDeposedKey = key
|
|
|
|
}
|