From eb1a1fa7c93062943c711647a3cae69c5dc812b6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 21 Jan 2015 14:53:50 -0800 Subject: [PATCH] terraform: put modules into the config graph --- terraform/graph_config.go | 6 ++++-- terraform/graph_config_node.go | 23 ++++++++++++++++++++++- terraform/graph_config_test.go | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/terraform/graph_config.go b/terraform/graph_config.go index 167bd8799..94e657e7f 100644 --- a/terraform/graph_config.go +++ b/terraform/graph_config.go @@ -25,7 +25,7 @@ func Graph2(mod *module.Tree) (*depgraph.Graph, error) { // Create the node list we'll use for the graph nodes := make([]graphNodeConfig, 0, - (len(config.Modules)+len(config.Resources))*2) + (len(config.ProviderConfigs)+len(config.Modules)+len(config.Resources))*2) // Write all the provider configs out for _, pc := range config.ProviderConfigs { @@ -38,7 +38,9 @@ func Graph2(mod *module.Tree) (*depgraph.Graph, error) { } // Write all the modules out - // TODO + for _, m := range config.Modules { + nodes = append(nodes, &GraphNodeConfigModule{Module: m}) + } // Build the full map of the var names to the nodes. fullMap := make(map[string]depgraph.Node) diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go index 6f33ed057..adb0566a3 100644 --- a/terraform/graph_config_node.go +++ b/terraform/graph_config_node.go @@ -56,7 +56,28 @@ func (n *graphNodeConfigBasicDepMap) setDepMap(m map[string]depgraph.Node) { n.DepMap = m } -// GraphNodeConfigProvider represents a resource within the config graph. +// GraphNodeConfigModule represents a module within the configuration graph. +type GraphNodeConfigModule struct { + graphNodeConfigBasicDepMap + + Module *config.Module +} + +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) VarName() string { + return n.Name() +} + +// GraphNodeConfigProvider represents a configured provider within the +// configuration graph. These are only immediately in the graph when an +// explicit `provider` configuration block is in the configuration. type GraphNodeConfigProvider struct { graphNodeConfigBasicDepMap diff --git a/terraform/graph_config_test.go b/terraform/graph_config_test.go index 6f3751d83..bb402cfbc 100644 --- a/terraform/graph_config_test.go +++ b/terraform/graph_config_test.go @@ -40,6 +40,19 @@ func TestGraph(t *testing.T) { } } +func TestGraph2_modules(t *testing.T) { + g, err := Graph2(testModule(t, "graph-modules")) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testGraphModulesStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + const testGraphBasicStr = ` aws_instance.web aws_security_group.firewall @@ -50,3 +63,13 @@ openstack_floating_ip.random provider.aws openstack_floating_ip.random ` + +const testGraphModulesStr = ` +aws_instance.web + aws_security_group.firewall + module.consul +aws_security_group.firewall +module.consul + aws_security_group.firewall +provider.aws +`