github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/terraform/transform_destroy.go (about) 1 package terraform 2 3 import ( 4 "github.com/hashicorp/terraform/dag" 5 ) 6 7 type GraphNodeDestroyMode byte 8 9 const ( 10 DestroyNone GraphNodeDestroyMode = 0 11 DestroyPrimary GraphNodeDestroyMode = 1 << iota 12 DestroyTainted 13 ) 14 15 // GraphNodeDestroyable is the interface that nodes that can be destroyed 16 // must implement. This is used to automatically handle the creation of 17 // destroy nodes in the graph and the dependency ordering of those destroys. 18 type GraphNodeDestroyable interface { 19 // DestroyNode returns the node used for the destroy with the given 20 // mode. If this returns nil, then a destroy node for that mode 21 // will not be added. 22 DestroyNode(GraphNodeDestroyMode) GraphNodeDestroy 23 } 24 25 // GraphNodeDestroy is the interface that must implemented by 26 // nodes that destroy. 27 type GraphNodeDestroy interface { 28 dag.Vertex 29 30 // CreateBeforeDestroy is called to check whether this node 31 // should be created before it is destroyed. The CreateBeforeDestroy 32 // transformer uses this information to setup the graph. 33 CreateBeforeDestroy() bool 34 35 // CreateNode returns the node used for the create side of this 36 // destroy. This must already exist within the graph. 37 CreateNode() dag.Vertex 38 } 39 40 // GraphNodeDestroyPrunable is the interface that can be implemented to 41 // signal that this node can be pruned depending on state. 42 type GraphNodeDestroyPrunable interface { 43 // DestroyInclude is called to check if this node should be included 44 // with the given state. The state and diff must NOT be modified. 45 DestroyInclude(*ModuleDiff, *ModuleState) bool 46 } 47 48 // GraphNodeEdgeInclude can be implemented to not include something 49 // as an edge within the destroy graph. This is usually done because it 50 // might cause unnecessary cycles. 51 type GraphNodeDestroyEdgeInclude interface { 52 DestroyEdgeInclude() bool 53 } 54 55 // DestroyTransformer is a GraphTransformer that creates the destruction 56 // nodes for things that _might_ be destroyed. 57 type DestroyTransformer struct{} 58 59 func (t *DestroyTransformer) Transform(g *Graph) error { 60 var connect, remove []dag.Edge 61 62 modes := []GraphNodeDestroyMode{DestroyPrimary, DestroyTainted} 63 for _, m := range modes { 64 connectMode, removeMode, err := t.transform(g, m) 65 if err != nil { 66 return err 67 } 68 69 connect = append(connect, connectMode...) 70 remove = append(remove, removeMode...) 71 } 72 73 // Atomatically add/remove the edges 74 for _, e := range connect { 75 g.Connect(e) 76 } 77 for _, e := range remove { 78 g.RemoveEdge(e) 79 } 80 81 return nil 82 } 83 84 func (t *DestroyTransformer) transform( 85 g *Graph, mode GraphNodeDestroyMode) ([]dag.Edge, []dag.Edge, error) { 86 var connect, remove []dag.Edge 87 nodeToCn := make(map[dag.Vertex]dag.Vertex, len(g.Vertices())) 88 nodeToDn := make(map[dag.Vertex]dag.Vertex, len(g.Vertices())) 89 for _, v := range g.Vertices() { 90 // If it is not a destroyable, we don't care 91 cn, ok := v.(GraphNodeDestroyable) 92 if !ok { 93 continue 94 } 95 96 // Grab the destroy side of the node and connect it through 97 n := cn.DestroyNode(mode) 98 if n == nil { 99 continue 100 } 101 102 // Store it 103 nodeToCn[n] = cn 104 nodeToDn[cn] = n 105 106 // Add it to the graph 107 g.Add(n) 108 109 // Inherit all the edges from the old node 110 downEdges := g.DownEdges(v).List() 111 for _, edgeRaw := range downEdges { 112 // If this thing specifically requests to not be depended on 113 // by destroy nodes, then don't. 114 if i, ok := edgeRaw.(GraphNodeDestroyEdgeInclude); ok && !i.DestroyEdgeInclude() { 115 continue 116 } 117 118 g.Connect(dag.BasicEdge(n, edgeRaw.(dag.Vertex))) 119 } 120 121 // Add a new edge to connect the node to be created to 122 // the destroy node. 123 connect = append(connect, dag.BasicEdge(v, n)) 124 } 125 126 // Go through the nodes we added and determine if they depend 127 // on any nodes with a destroy node. If so, depend on that instead. 128 for n, _ := range nodeToCn { 129 for _, downRaw := range g.DownEdges(n).List() { 130 target := downRaw.(dag.Vertex) 131 cn2, ok := target.(GraphNodeDestroyable) 132 if !ok { 133 continue 134 } 135 136 newTarget := nodeToDn[cn2] 137 if newTarget == nil { 138 continue 139 } 140 141 // Make the new edge and transpose 142 connect = append(connect, dag.BasicEdge(newTarget, n)) 143 144 // Remove the old edge 145 remove = append(remove, dag.BasicEdge(n, target)) 146 } 147 } 148 149 return connect, remove, nil 150 } 151 152 // CreateBeforeDestroyTransformer is a GraphTransformer that modifies 153 // the destroys of some nodes so that the creation happens before the 154 // destroy. 155 type CreateBeforeDestroyTransformer struct{} 156 157 func (t *CreateBeforeDestroyTransformer) Transform(g *Graph) error { 158 // We "stage" the edge connections/destroys in these slices so that 159 // while we're doing the edge transformations (transpositions) in 160 // the graph, we're not affecting future edge transpositions. These 161 // slices let us stage ALL the changes that WILL happen so that all 162 // of the transformations happen atomically. 163 var connect, destroy []dag.Edge 164 165 for _, v := range g.Vertices() { 166 // We only care to use the destroy nodes 167 dn, ok := v.(GraphNodeDestroy) 168 if !ok { 169 continue 170 } 171 172 // If the node doesn't need to create before destroy, then continue 173 if !dn.CreateBeforeDestroy() { 174 continue 175 } 176 177 // Get the creation side of this node 178 cn := dn.CreateNode() 179 180 // Take all the things which depend on the creation node and 181 // make them dependencies on the destruction. Clarifying this 182 // with an example: if you have a web server and a load balancer 183 // and the load balancer depends on the web server, then when we 184 // do a create before destroy, we want to make sure the steps are: 185 // 186 // 1.) Create new web server 187 // 2.) Update load balancer 188 // 3.) Delete old web server 189 // 190 // This ensures that. 191 for _, sourceRaw := range g.UpEdges(cn).List() { 192 source := sourceRaw.(dag.Vertex) 193 connect = append(connect, dag.BasicEdge(dn, source)) 194 } 195 196 // Swap the edge so that the destroy depends on the creation 197 // happening... 198 connect = append(connect, dag.BasicEdge(dn, cn)) 199 destroy = append(destroy, dag.BasicEdge(cn, dn)) 200 } 201 202 for _, edge := range connect { 203 g.Connect(edge) 204 } 205 for _, edge := range destroy { 206 g.RemoveEdge(edge) 207 } 208 209 return nil 210 } 211 212 // PruneDestroyTransformer is a GraphTransformer that removes the destroy 213 // nodes that aren't in the diff. 214 type PruneDestroyTransformer struct { 215 Diff *Diff 216 State *State 217 } 218 219 func (t *PruneDestroyTransformer) Transform(g *Graph) error { 220 for _, v := range g.Vertices() { 221 // If it is not a destroyer, we don't care 222 dn, ok := v.(GraphNodeDestroyPrunable) 223 if !ok { 224 continue 225 } 226 227 path := g.Path 228 if pn, ok := v.(GraphNodeSubPath); ok { 229 path = pn.Path() 230 } 231 232 var modDiff *ModuleDiff 233 var modState *ModuleState 234 if t.Diff != nil { 235 modDiff = t.Diff.ModuleByPath(path) 236 } 237 if t.State != nil { 238 modState = t.State.ModuleByPath(path) 239 } 240 241 // Remove it if we should 242 if !dn.DestroyInclude(modDiff, modState) { 243 g.Remove(v) 244 } 245 } 246 247 return nil 248 }