github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/transform_destroy_edge.go (about) 1 package terraform 2 3 import ( 4 "log" 5 6 "github.com/hashicorp/terraform/config/module" 7 "github.com/hashicorp/terraform/dag" 8 ) 9 10 // GraphNodeDestroyer must be implemented by nodes that destroy resources. 11 type GraphNodeDestroyer interface { 12 dag.Vertex 13 14 // ResourceAddr is the address of the resource that is being 15 // destroyed by this node. If this returns nil, then this node 16 // is not destroying anything. 17 DestroyAddr() *ResourceAddress 18 } 19 20 // GraphNodeCreator must be implemented by nodes that create OR update resources. 21 type GraphNodeCreator interface { 22 // ResourceAddr is the address of the resource being created or updated 23 CreateAddr() *ResourceAddress 24 } 25 26 // DestroyEdgeTransformer is a GraphTransformer that creates the proper 27 // references for destroy resources. Destroy resources are more complex 28 // in that they must be depend on the destruction of resources that 29 // in turn depend on the CREATION of the node being destroy. 30 // 31 // That is complicated. Visually: 32 // 33 // B_d -> A_d -> A -> B 34 // 35 // Notice that A destroy depends on B destroy, while B create depends on 36 // A create. They're inverted. This must be done for example because often 37 // dependent resources will block parent resources from deleting. Concrete 38 // example: VPC with subnets, the VPC can't be deleted while there are 39 // still subnets. 40 type DestroyEdgeTransformer struct { 41 // Module and State are only needed to look up dependencies in 42 // any way possible. Either can be nil if not availabile. 43 Module *module.Tree 44 State *State 45 } 46 47 func (t *DestroyEdgeTransformer) Transform(g *Graph) error { 48 log.Printf("[TRACE] DestroyEdgeTransformer: Beginning destroy edge transformation...") 49 50 // Build a map of what is being destroyed (by address string) to 51 // the list of destroyers. In general there will only be one destroyer 52 // but to make it more robust we support multiple. 53 destroyers := make(map[string][]GraphNodeDestroyer) 54 for _, v := range g.Vertices() { 55 dn, ok := v.(GraphNodeDestroyer) 56 if !ok { 57 continue 58 } 59 60 addr := dn.DestroyAddr() 61 if addr == nil { 62 continue 63 } 64 65 key := addr.String() 66 log.Printf( 67 "[TRACE] DestroyEdgeTransformer: %s destroying %q", 68 dag.VertexName(dn), key) 69 destroyers[key] = append(destroyers[key], dn) 70 } 71 72 // If we aren't destroying anything, there will be no edges to make 73 // so just exit early and avoid future work. 74 if len(destroyers) == 0 { 75 return nil 76 } 77 78 // Go through and connect creators to destroyers. Going along with 79 // our example, this makes: A_d => A 80 for _, v := range g.Vertices() { 81 cn, ok := v.(GraphNodeCreator) 82 if !ok { 83 continue 84 } 85 86 addr := cn.CreateAddr() 87 if addr == nil { 88 continue 89 } 90 91 key := addr.String() 92 ds := destroyers[key] 93 if len(ds) == 0 { 94 continue 95 } 96 97 for _, d := range ds { 98 // For illustrating our example 99 a_d := d.(dag.Vertex) 100 a := v 101 102 log.Printf( 103 "[TRACE] DestroyEdgeTransformer: connecting creator/destroyer: %s, %s", 104 dag.VertexName(a), dag.VertexName(a_d)) 105 106 g.Connect(&DestroyEdge{S: a, T: a_d}) 107 } 108 } 109 110 // This is strange but is the easiest way to get the dependencies 111 // of a node that is being destroyed. We use another graph to make sure 112 // the resource is in the graph and ask for references. We have to do this 113 // because the node that is being destroyed may NOT be in the graph. 114 // 115 // Example: resource A is force new, then destroy A AND create A are 116 // in the graph. BUT if resource A is just pure destroy, then only 117 // destroy A is in the graph, and create A is not. 118 steps := []GraphTransformer{ 119 &AttachResourceConfigTransformer{Module: t.Module}, 120 &AttachStateTransformer{State: t.State}, 121 } 122 123 // Go through the all destroyers and find what they're destroying. 124 // Use this to find the dependencies, look up if any of them are being 125 // destroyed, and to make the proper edge. 126 for d, dns := range destroyers { 127 // d is what is being destroyed. We parse the resource address 128 // which it came from it is a panic if this fails. 129 addr, err := ParseResourceAddress(d) 130 if err != nil { 131 panic(err) 132 } 133 134 // This part is a little bit weird but is the best way to 135 // find the dependencies we need to: build a graph and use the 136 // attach config and state transformers then ask for references. 137 node := &NodeAbstractResource{Addr: addr} 138 { 139 var g Graph 140 g.Add(node) 141 for _, s := range steps { 142 if err := s.Transform(&g); err != nil { 143 return err 144 } 145 } 146 } 147 148 // Get the references of the creation node. If it has none, 149 // then there are no edges to make here. 150 prefix := modulePrefixStr(normalizeModulePath(addr.Path)) 151 deps := modulePrefixList(node.References(), prefix) 152 log.Printf( 153 "[TRACE] DestroyEdgeTransformer: creation of %q depends on %#v", 154 d, deps) 155 if len(deps) == 0 { 156 continue 157 } 158 159 // We have dependencies, check if any are being destroyed 160 // to build the list of things that we must depend on! 161 // 162 // In the example of the struct, if we have: 163 // 164 // B_d => A_d => A => B 165 // 166 // Then at this point in the algorithm we started with A_d, 167 // we built A (to get dependencies), and we found B. We're now looking 168 // to see if B_d exists. 169 var depDestroyers []dag.Vertex 170 for _, d := range deps { 171 if ds, ok := destroyers[d]; ok { 172 for _, d := range ds { 173 depDestroyers = append(depDestroyers, d.(dag.Vertex)) 174 log.Printf( 175 "[TRACE] DestroyEdgeTransformer: destruction of %q depends on %s", 176 addr.String(), dag.VertexName(d)) 177 } 178 } 179 } 180 181 // Go through and make the connections. Use the variable 182 // names "a_d" and "b_d" to reference our example. 183 for _, a_d := range dns { 184 for _, b_d := range depDestroyers { 185 if b_d != a_d { 186 g.Connect(dag.BasicEdge(b_d, a_d)) 187 } 188 } 189 } 190 } 191 192 return nil 193 }