github.com/mehmetalisavas/terraform@v0.7.10/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 all the nodes being destroyed and create a graph.
   124  	// The resulting graph is only of things being CREATED. For example,
   125  	// following our example, the resulting graph would be:
   126  	//
   127  	//   A, B (with no edges)
   128  	//
   129  	var tempG Graph
   130  	for d, _ := range destroyers {
   131  		// d is what is being destroyed. We parse the resource address
   132  		// which it came from it is a panic if this fails.
   133  		addr, err := ParseResourceAddress(d)
   134  		if err != nil {
   135  			panic(err)
   136  		}
   137  
   138  		// This part is a little bit weird but is the best way to
   139  		// find the dependencies we need to: build a graph and use the
   140  		// attach config and state transformers then ask for references.
   141  		node := &NodeAbstractResource{Addr: addr}
   142  		tempG.Add(node)
   143  	}
   144  
   145  	// Run the graph transforms so we have the information we need to
   146  	// build references.
   147  	for _, s := range steps {
   148  		if err := s.Transform(&tempG); err != nil {
   149  			return err
   150  		}
   151  	}
   152  
   153  	// Create a reference map for easy lookup
   154  	refMap := NewReferenceMap(tempG.Vertices())
   155  
   156  	// Go through all the nodes in the graph and determine what they
   157  	// depend on.
   158  	for _, v := range tempG.Vertices() {
   159  		// Find all the references
   160  		refs, _ := refMap.References(v)
   161  		log.Printf(
   162  			"[TRACE] DestroyEdgeTransformer: creation node %q references %v",
   163  			dag.VertexName(v), refs)
   164  
   165  		// If we have no references, then we won't need to do anything
   166  		if len(refs) == 0 {
   167  			continue
   168  		}
   169  
   170  		// Get the destroy node for this. In the example of our struct,
   171  		// we are currently at B and we're looking for B_d.
   172  		rn, ok := v.(GraphNodeResource)
   173  		if !ok {
   174  			continue
   175  		}
   176  
   177  		addr := rn.ResourceAddr()
   178  		if addr == nil {
   179  			continue
   180  		}
   181  
   182  		dns := destroyers[addr.String()]
   183  
   184  		// We have dependencies, check if any are being destroyed
   185  		// to build the list of things that we must depend on!
   186  		//
   187  		// In the example of the struct, if we have:
   188  		//
   189  		//   B_d => A_d => A => B
   190  		//
   191  		// Then at this point in the algorithm we started with B_d,
   192  		// we built B (to get dependencies), and we found A. We're now looking
   193  		// to see if A_d exists.
   194  		var depDestroyers []dag.Vertex
   195  		for _, v := range refs {
   196  			rn, ok := v.(GraphNodeResource)
   197  			if !ok {
   198  				continue
   199  			}
   200  
   201  			addr := rn.ResourceAddr()
   202  			if addr == nil {
   203  				continue
   204  			}
   205  
   206  			key := addr.String()
   207  			if ds, ok := destroyers[key]; ok {
   208  				for _, d := range ds {
   209  					depDestroyers = append(depDestroyers, d.(dag.Vertex))
   210  					log.Printf(
   211  						"[TRACE] DestroyEdgeTransformer: destruction of %q depends on %s",
   212  						key, dag.VertexName(d))
   213  				}
   214  			}
   215  		}
   216  
   217  		// Go through and make the connections. Use the variable
   218  		// names "a_d" and "b_d" to reference our example.
   219  		for _, a_d := range dns {
   220  			for _, b_d := range depDestroyers {
   221  				if b_d != a_d {
   222  					g.Connect(dag.BasicEdge(b_d, a_d))
   223  				}
   224  			}
   225  		}
   226  	}
   227  
   228  	return nil
   229  }