terraform: use new dag API

This commit is contained in:
Mitchell Hashimoto 2015-01-23 15:11:11 -08:00
parent 2a910585a2
commit 9d593cdece
3 changed files with 15 additions and 55 deletions

View File

@ -43,31 +43,30 @@ func Graph2(mod *module.Tree) (*dag.Graph, error) {
} }
// Build the full map of the var names to the nodes. // Build the full map of the var names to the nodes.
fullMap := make(map[string]dag.Node) fullMap := make(map[string]dag.Vertex)
for _, n := range nodes { for _, n := range nodes {
fullMap[n.VarName()] = n fullMap[n.VarName()] = n
} }
// Go through all the nodes and build up the actual dependency map. We // Build the graph vertices
var g dag.Graph
for _, n := range nodes {
g.Add(n)
}
// Go through all the nodes and build up the actual graph edges. We
// do this by getting the variables that each node depends on and then // do this by getting the variables that each node depends on and then
// building the dep map based on the fullMap which contains the mapping // building the dep map based on the fullMap which contains the mapping
// of var names to the actual node with that name. // of var names to the actual node with that name.
for _, n := range nodes { for _, n := range nodes {
m := make(map[string]dag.Node)
for _, id := range n.Variables() { for _, id := range n.Variables() {
m[id] = fullMap[id] if target, ok := fullMap[id]; ok {
g.Connect(dag.BasicEdge(n, target))
}
} }
n.setDepMap(m)
} }
// Build the graph and return it return &g, nil
g := &dag.Graph{Nodes: make([]dag.Node, 0, len(nodes))}
for _, n := range nodes {
g.Nodes = append(g.Nodes, n)
}
return g, nil
} }
// varNameForVar returns the VarName value for an interpolated variable. // varNameForVar returns the VarName value for an interpolated variable.

View File

@ -11,7 +11,7 @@ import (
// configuration graph need to implement in order to build the variable // configuration graph need to implement in order to build the variable
// dependencies properly. // dependencies properly.
type graphNodeConfig interface { type graphNodeConfig interface {
dag.Node dag.Vertex
// Variables returns the full list of variables that this node // Variables returns the full list of variables that this node
// depends on. The values within the slice should map to the VarName() // depends on. The values within the slice should map to the VarName()
@ -22,45 +22,10 @@ type graphNodeConfig interface {
// maps to this node. It should match the result of the // maps to this node. It should match the result of the
// `VarName` function. // `VarName` function.
VarName() string VarName() string
// depMap and setDepMap are used to get and set the dependency map
// for this node. This is used to modify the dependencies. The key of
// this map should be the VarName() of graphNodeConfig.
depMap() map[string]dag.Node
setDepMap(map[string]dag.Node)
}
// graphNodeConfigBasicDepMap is a struct that provides the Deps(),
// depMap(), and setDepMap() functions to help satisfy the graphNodeConfig
// interface. This struct is meant to be embedded into other nodes to get
// these features for free.
type graphNodeConfigBasicDepMap struct {
DepMap map[string]dag.Node
}
func (n *graphNodeConfigBasicDepMap) Deps() []dag.Node {
r := make([]dag.Node, 0, len(n.DepMap))
for _, v := range n.DepMap {
if v != nil {
r = append(r, v)
}
}
return r
}
func (n *graphNodeConfigBasicDepMap) depMap() map[string]dag.Node {
return n.DepMap
}
func (n *graphNodeConfigBasicDepMap) setDepMap(m map[string]dag.Node) {
n.DepMap = m
} }
// GraphNodeConfigModule represents a module within the configuration graph. // GraphNodeConfigModule represents a module within the configuration graph.
type GraphNodeConfigModule struct { type GraphNodeConfigModule struct {
graphNodeConfigBasicDepMap
Module *config.Module Module *config.Module
} }
@ -86,8 +51,6 @@ func (n *GraphNodeConfigModule) VarName() string {
// configuration graph. These are only immediately in the graph when an // configuration graph. These are only immediately in the graph when an
// explicit `provider` configuration block is in the configuration. // explicit `provider` configuration block is in the configuration.
type GraphNodeConfigProvider struct { type GraphNodeConfigProvider struct {
graphNodeConfigBasicDepMap
Provider *config.ProviderConfig Provider *config.ProviderConfig
} }
@ -111,8 +74,6 @@ func (n *GraphNodeConfigProvider) VarName() string {
// GraphNodeConfigResource represents a resource within the config graph. // GraphNodeConfigResource represents a resource within the config graph.
type GraphNodeConfigResource struct { type GraphNodeConfigResource struct {
graphNodeConfigBasicDepMap
Resource *config.Resource Resource *config.Resource
} }

View File

@ -7,7 +7,7 @@ import (
) )
func TestGraphNodeConfigResource_impl(t *testing.T) { func TestGraphNodeConfigResource_impl(t *testing.T) {
var _ dag.Node = new(GraphNodeConfigResource) var _ dag.Vertex = new(GraphNodeConfigResource)
var _ dag.NamedNode = new(GraphNodeConfigResource) var _ dag.NamedVertex = new(GraphNodeConfigResource)
var _ graphNodeConfig = new(GraphNodeConfigResource) var _ graphNodeConfig = new(GraphNodeConfigResource)
} }