github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/terraform/transform_root.go (about)

     1  package terraform
     2  
     3  import "github.com/hashicorp/terraform/dag"
     4  
     5  // RootTransformer is a GraphTransformer that adds a root to the graph.
     6  type RootTransformer struct{}
     7  
     8  func (t *RootTransformer) Transform(g *Graph) error {
     9  	// If we already have a good root, we're done
    10  	if _, err := g.Root(); err == nil {
    11  		return nil
    12  	}
    13  
    14  	// Add a root
    15  	var root graphNodeRoot
    16  	g.Add(root)
    17  
    18  	// Connect the root to all the edges that need it
    19  	for _, v := range g.Vertices() {
    20  		if v == root {
    21  			continue
    22  		}
    23  
    24  		if g.UpEdges(v).Len() == 0 {
    25  			g.Connect(dag.BasicEdge(root, v))
    26  		}
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  type graphNodeRoot struct{}
    33  
    34  func (n graphNodeRoot) Name() string {
    35  	return "root"
    36  }
    37  
    38  func (n graphNodeRoot) Flatten(p []string) (dag.Vertex, error) {
    39  	return n, nil
    40  }