terraform: config graph respects depends_on
This commit is contained in:
parent
eb1a1fa7c9
commit
012dcca7d5
|
@ -54,8 +54,7 @@ func Graph2(mod *module.Tree) (*depgraph.Graph, error) {
|
|||
// of var names to the actual node with that name.
|
||||
for _, n := range nodes {
|
||||
m := make(map[string]depgraph.Node)
|
||||
for _, v := range n.Variables() {
|
||||
id := varNameForVar(v)
|
||||
for _, id := range n.Variables() {
|
||||
m[id] = fullMap[id]
|
||||
}
|
||||
|
||||
|
|
|
@ -14,8 +14,9 @@ type graphNodeConfig interface {
|
|||
depgraph.Node
|
||||
|
||||
// Variables returns the full list of variables that this node
|
||||
// depends on.
|
||||
Variables() map[string]config.InterpolatedVariable
|
||||
// depends on. The values within the slice should map to the VarName()
|
||||
// values that are returned by any nodes.
|
||||
Variables() []string
|
||||
|
||||
// VarName returns the name that is used to identify a variable
|
||||
// maps to this node. It should match the result of the
|
||||
|
@ -67,8 +68,14 @@ func (n *GraphNodeConfigModule) Name() string {
|
|||
return fmt.Sprintf("module.%s", n.Module.Name)
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigModule) Variables() map[string]config.InterpolatedVariable {
|
||||
return n.Module.RawConfig.Variables
|
||||
func (n *GraphNodeConfigModule) Variables() []string {
|
||||
vars := n.Module.RawConfig.Variables
|
||||
result := make([]string, 0, len(vars))
|
||||
for _, v := range vars {
|
||||
result = append(result, varNameForVar(v))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigModule) VarName() string {
|
||||
|
@ -88,8 +95,14 @@ func (n *GraphNodeConfigProvider) Name() string {
|
|||
return fmt.Sprintf("provider.%s", n.Provider.Name)
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigProvider) Variables() map[string]config.InterpolatedVariable {
|
||||
return n.Provider.RawConfig.Variables
|
||||
func (n *GraphNodeConfigProvider) Variables() []string {
|
||||
vars := n.Provider.RawConfig.Variables
|
||||
result := make([]string, 0, len(vars))
|
||||
for _, v := range vars {
|
||||
result = append(result, varNameForVar(v))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigProvider) VarName() string {
|
||||
|
@ -107,19 +120,20 @@ func (n *GraphNodeConfigResource) Name() string {
|
|||
return n.Resource.Id()
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigResource) Variables() map[string]config.InterpolatedVariable {
|
||||
var m map[string]config.InterpolatedVariable
|
||||
if n.Resource != nil {
|
||||
m = make(map[string]config.InterpolatedVariable)
|
||||
for k, v := range n.Resource.RawCount.Variables {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range n.Resource.RawConfig.Variables {
|
||||
m[k] = v
|
||||
}
|
||||
func (n *GraphNodeConfigResource) Variables() []string {
|
||||
result := make([]string, len(n.Resource.DependsOn),
|
||||
len(n.Resource.RawCount.Variables)+
|
||||
len(n.Resource.RawConfig.Variables)+
|
||||
len(n.Resource.DependsOn))
|
||||
copy(result, n.Resource.DependsOn)
|
||||
for _, v := range n.Resource.RawCount.Variables {
|
||||
result = append(result, varNameForVar(v))
|
||||
}
|
||||
for _, v := range n.Resource.RawConfig.Variables {
|
||||
result = append(result, varNameForVar(v))
|
||||
}
|
||||
|
||||
return m
|
||||
return result
|
||||
}
|
||||
|
||||
func (n *GraphNodeConfigResource) VarName() string {
|
||||
|
|
|
@ -40,6 +40,19 @@ func TestGraph(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGraph2_dependsOn(t *testing.T) {
|
||||
g, err := Graph2(testModule(t, "graph-depends-on"))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testGraphDependsOnStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGraph2_modules(t *testing.T) {
|
||||
g, err := Graph2(testModule(t, "graph-modules"))
|
||||
if err != nil {
|
||||
|
@ -64,6 +77,12 @@ provider.aws
|
|||
openstack_floating_ip.random
|
||||
`
|
||||
|
||||
const testGraphDependsOnStr = `
|
||||
aws_instance.db
|
||||
aws_instance.web
|
||||
aws_instance.web
|
||||
`
|
||||
|
||||
const testGraphModulesStr = `
|
||||
aws_instance.web
|
||||
aws_security_group.firewall
|
||||
|
|
Loading…
Reference in New Issue