github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/terraform/graph_builder_destroy_plan.go (about) 1 package terraform 2 3 import ( 4 "github.com/iaas-resource-provision/iaas-rpc/internal/addrs" 5 "github.com/iaas-resource-provision/iaas-rpc/internal/configs" 6 "github.com/iaas-resource-provision/iaas-rpc/internal/dag" 7 "github.com/iaas-resource-provision/iaas-rpc/internal/states" 8 "github.com/iaas-resource-provision/iaas-rpc/internal/tfdiags" 9 ) 10 11 // DestroyPlanGraphBuilder implements GraphBuilder and is responsible for 12 // planning a pure-destroy. 13 // 14 // Planning a pure destroy operation is simple because we can ignore most 15 // ordering configuration and simply reverse the state. This graph mainly 16 // exists for targeting, because we need to walk the destroy dependencies to 17 // ensure we plan the required resources. Without the requirement for 18 // targeting, the plan could theoretically be created directly from the state. 19 type DestroyPlanGraphBuilder struct { 20 // Config is the configuration tree to build the plan from. 21 Config *configs.Config 22 23 // State is the current state 24 State *states.State 25 26 // Components is a factory for the plug-in components (providers and 27 // provisioners) available for use. 28 Components contextComponentFactory 29 30 // Schemas is the repository of schemas we will draw from to analyse 31 // the configuration. 32 Schemas *Schemas 33 34 // Targets are resources to target 35 Targets []addrs.Targetable 36 37 // Validate will do structural validation of the graph. 38 Validate bool 39 40 // If set, skipRefresh will cause us stop skip refreshing any existing 41 // resource instances as part of our planning. This will cause us to fail 42 // to detect if an object has already been deleted outside of Terraform. 43 skipRefresh bool 44 } 45 46 // See GraphBuilder 47 func (b *DestroyPlanGraphBuilder) Build(path addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics) { 48 return (&BasicGraphBuilder{ 49 Steps: b.Steps(), 50 Validate: b.Validate, 51 Name: "DestroyPlanGraphBuilder", 52 }).Build(path) 53 } 54 55 // See GraphBuilder 56 func (b *DestroyPlanGraphBuilder) Steps() []GraphTransformer { 57 concreteResourceInstance := func(a *NodeAbstractResourceInstance) dag.Vertex { 58 return &NodePlanDestroyableResourceInstance{ 59 NodeAbstractResourceInstance: a, 60 skipRefresh: b.skipRefresh, 61 } 62 } 63 concreteResourceInstanceDeposed := func(a *NodeAbstractResourceInstance, key states.DeposedKey) dag.Vertex { 64 return &NodePlanDeposedResourceInstanceObject{ 65 NodeAbstractResourceInstance: a, 66 DeposedKey: key, 67 skipRefresh: b.skipRefresh, 68 } 69 } 70 71 concreteProvider := func(a *NodeAbstractProvider) dag.Vertex { 72 return &NodeApplyableProvider{ 73 NodeAbstractProvider: a, 74 } 75 } 76 77 steps := []GraphTransformer{ 78 // Creates nodes for the resource instances tracked in the state. 79 &StateTransformer{ 80 ConcreteCurrent: concreteResourceInstance, 81 ConcreteDeposed: concreteResourceInstanceDeposed, 82 State: b.State, 83 }, 84 85 // Create the delete changes for root module outputs. 86 &OutputTransformer{ 87 Config: b.Config, 88 Destroy: true, 89 }, 90 91 // Attach the state 92 &AttachStateTransformer{State: b.State}, 93 94 // Attach the configuration to any resources 95 &AttachResourceConfigTransformer{Config: b.Config}, 96 97 TransformProviders(b.Components.ResourceProviders(), concreteProvider, b.Config), 98 99 // Destruction ordering. We require this only so that 100 // targeting below will prune the correct things. 101 &DestroyEdgeTransformer{ 102 Config: b.Config, 103 State: b.State, 104 Schemas: b.Schemas, 105 }, 106 107 &TargetsTransformer{Targets: b.Targets}, 108 109 // Close opened plugin connections 110 &CloseProviderTransformer{}, 111 112 // Close the root module 113 &CloseRootModuleTransformer{}, 114 115 &TransitiveReductionTransformer{}, 116 } 117 118 return steps 119 }