github.com/markdia/terraform@v0.5.1-0.20150508012022-f1ae920aa970/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(dag.Vertex) 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  	FullDestroy bool
    59  }
    60  
    61  func (t *DestroyTransformer) Transform(g *Graph) error {
    62  	var connect, remove []dag.Edge
    63  
    64  	modes := []GraphNodeDestroyMode{DestroyPrimary, DestroyTainted}
    65  	for _, m := range modes {
    66  		connectMode, removeMode, err := t.transform(g, m)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		connect = append(connect, connectMode...)
    72  		remove = append(remove, removeMode...)
    73  	}
    74  
    75  	// Atomatically add/remove the edges
    76  	for _, e := range connect {
    77  		g.Connect(e)
    78  	}
    79  	for _, e := range remove {
    80  		g.RemoveEdge(e)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  func (t *DestroyTransformer) transform(
    87  	g *Graph, mode GraphNodeDestroyMode) ([]dag.Edge, []dag.Edge, error) {
    88  	var connect, remove []dag.Edge
    89  	nodeToCn := make(map[dag.Vertex]dag.Vertex, len(g.Vertices()))
    90  	nodeToDn := make(map[dag.Vertex]dag.Vertex, len(g.Vertices()))
    91  	for _, v := range g.Vertices() {
    92  		// If it is not a destroyable, we don't care
    93  		cn, ok := v.(GraphNodeDestroyable)
    94  		if !ok {
    95  			continue
    96  		}
    97  
    98  		// Grab the destroy side of the node and connect it through
    99  		n := cn.DestroyNode(mode)
   100  		if n == nil {
   101  			continue
   102  		}
   103  
   104  		// Store it
   105  		nodeToCn[n] = cn
   106  		nodeToDn[cn] = n
   107  
   108  		// Add it to the graph
   109  		g.Add(n)
   110  
   111  		// Inherit all the edges from the old node
   112  		downEdges := g.DownEdges(v).List()
   113  		for _, edgeRaw := range downEdges {
   114  			// If this thing specifically requests to not be depended on
   115  			// by destroy nodes, then don't.
   116  			if i, ok := edgeRaw.(GraphNodeDestroyEdgeInclude); ok &&
   117  				!i.DestroyEdgeInclude(v) {
   118  				continue
   119  			}
   120  
   121  			g.Connect(dag.BasicEdge(n, edgeRaw.(dag.Vertex)))
   122  		}
   123  
   124  		// Add a new edge to connect the node to be created to
   125  		// the destroy node.
   126  		connect = append(connect, dag.BasicEdge(v, n))
   127  	}
   128  
   129  	// Go through the nodes we added and determine if they depend
   130  	// on any nodes with a destroy node. If so, depend on that instead.
   131  	for n, _ := range nodeToCn {
   132  		for _, downRaw := range g.DownEdges(n).List() {
   133  			target := downRaw.(dag.Vertex)
   134  			cn2, ok := target.(GraphNodeDestroyable)
   135  			if !ok {
   136  				continue
   137  			}
   138  
   139  			newTarget := nodeToDn[cn2]
   140  			if newTarget == nil {
   141  				continue
   142  			}
   143  
   144  			// Make the new edge and transpose
   145  			connect = append(connect, dag.BasicEdge(newTarget, n))
   146  
   147  			// Remove the old edge
   148  			remove = append(remove, dag.BasicEdge(n, target))
   149  		}
   150  	}
   151  
   152  	return connect, remove, nil
   153  }
   154  
   155  // CreateBeforeDestroyTransformer is a GraphTransformer that modifies
   156  // the destroys of some nodes so that the creation happens before the
   157  // destroy.
   158  type CreateBeforeDestroyTransformer struct{}
   159  
   160  func (t *CreateBeforeDestroyTransformer) Transform(g *Graph) error {
   161  	// We "stage" the edge connections/destroys in these slices so that
   162  	// while we're doing the edge transformations (transpositions) in
   163  	// the graph, we're not affecting future edge transpositions. These
   164  	// slices let us stage ALL the changes that WILL happen so that all
   165  	// of the transformations happen atomically.
   166  	var connect, destroy []dag.Edge
   167  
   168  	for _, v := range g.Vertices() {
   169  		// We only care to use the destroy nodes
   170  		dn, ok := v.(GraphNodeDestroy)
   171  		if !ok {
   172  			continue
   173  		}
   174  
   175  		// If the node doesn't need to create before destroy, then continue
   176  		if !dn.CreateBeforeDestroy() {
   177  			continue
   178  		}
   179  
   180  		// Get the creation side of this node
   181  		cn := dn.CreateNode()
   182  
   183  		// Take all the things which depend on the creation node and
   184  		// make them dependencies on the destruction. Clarifying this
   185  		// with an example: if you have a web server and a load balancer
   186  		// and the load balancer depends on the web server, then when we
   187  		// do a create before destroy, we want to make sure the steps are:
   188  		//
   189  		// 1.) Create new web server
   190  		// 2.) Update load balancer
   191  		// 3.) Delete old web server
   192  		//
   193  		// This ensures that.
   194  		for _, sourceRaw := range g.UpEdges(cn).List() {
   195  			source := sourceRaw.(dag.Vertex)
   196  			connect = append(connect, dag.BasicEdge(dn, source))
   197  		}
   198  
   199  		// Swap the edge so that the destroy depends on the creation
   200  		// happening...
   201  		connect = append(connect, dag.BasicEdge(dn, cn))
   202  		destroy = append(destroy, dag.BasicEdge(cn, dn))
   203  	}
   204  
   205  	for _, edge := range connect {
   206  		g.Connect(edge)
   207  	}
   208  	for _, edge := range destroy {
   209  		g.RemoveEdge(edge)
   210  	}
   211  
   212  	return nil
   213  }
   214  
   215  // PruneDestroyTransformer is a GraphTransformer that removes the destroy
   216  // nodes that aren't in the diff.
   217  type PruneDestroyTransformer struct {
   218  	Diff  *Diff
   219  	State *State
   220  }
   221  
   222  func (t *PruneDestroyTransformer) Transform(g *Graph) error {
   223  	for _, v := range g.Vertices() {
   224  		// If it is not a destroyer, we don't care
   225  		dn, ok := v.(GraphNodeDestroyPrunable)
   226  		if !ok {
   227  			continue
   228  		}
   229  
   230  		path := g.Path
   231  		if pn, ok := v.(GraphNodeSubPath); ok {
   232  			path = pn.Path()
   233  		}
   234  
   235  		var modDiff *ModuleDiff
   236  		var modState *ModuleState
   237  		if t.Diff != nil {
   238  			modDiff = t.Diff.ModuleByPath(path)
   239  		}
   240  		if t.State != nil {
   241  			modState = t.State.ModuleByPath(path)
   242  		}
   243  
   244  		// Remove it if we should
   245  		if !dn.DestroyInclude(modDiff, modState) {
   246  			g.Remove(v)
   247  		}
   248  	}
   249  
   250  	return nil
   251  }