github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/terraform/transform_root.go (about)

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