github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/terraform/transform_root.go (about)

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