terraform: use new dag API
This commit is contained in:
parent
2a910585a2
commit
9d593cdece
|
@ -43,31 +43,30 @@ func Graph2(mod *module.Tree) (*dag.Graph, error) {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
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
|
||||
// building the dep map based on the fullMap which contains the mapping
|
||||
// of var names to the actual node with that name.
|
||||
for _, n := range nodes {
|
||||
m := make(map[string]dag.Node)
|
||||
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
|
||||
g := &dag.Graph{Nodes: make([]dag.Node, 0, len(nodes))}
|
||||
for _, n := range nodes {
|
||||
g.Nodes = append(g.Nodes, n)
|
||||
}
|
||||
|
||||
return g, nil
|
||||
return &g, nil
|
||||
}
|
||||
|
||||
// varNameForVar returns the VarName value for an interpolated variable.
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
// configuration graph need to implement in order to build the variable
|
||||
// dependencies properly.
|
||||
type graphNodeConfig interface {
|
||||
dag.Node
|
||||
dag.Vertex
|
||||
|
||||
// Variables returns the full list of variables that this node
|
||||
// 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
|
||||
// `VarName` function.
|
||||
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.
|
||||
type GraphNodeConfigModule struct {
|
||||
graphNodeConfigBasicDepMap
|
||||
|
||||
Module *config.Module
|
||||
}
|
||||
|
||||
|
@ -86,8 +51,6 @@ func (n *GraphNodeConfigModule) VarName() string {
|
|||
// configuration graph. These are only immediately in the graph when an
|
||||
// explicit `provider` configuration block is in the configuration.
|
||||
type GraphNodeConfigProvider struct {
|
||||
graphNodeConfigBasicDepMap
|
||||
|
||||
Provider *config.ProviderConfig
|
||||
}
|
||||
|
||||
|
@ -111,8 +74,6 @@ func (n *GraphNodeConfigProvider) VarName() string {
|
|||
|
||||
// GraphNodeConfigResource represents a resource within the config graph.
|
||||
type GraphNodeConfigResource struct {
|
||||
graphNodeConfigBasicDepMap
|
||||
|
||||
Resource *config.Resource
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func TestGraphNodeConfigResource_impl(t *testing.T) {
|
||||
var _ dag.Node = new(GraphNodeConfigResource)
|
||||
var _ dag.NamedNode = new(GraphNodeConfigResource)
|
||||
var _ dag.Vertex = new(GraphNodeConfigResource)
|
||||
var _ dag.NamedVertex = new(GraphNodeConfigResource)
|
||||
var _ graphNodeConfig = new(GraphNodeConfigResource)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue