From 039531e9cae1120db41868926619066aaf38060a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Oct 2014 13:54:04 -0700 Subject: [PATCH] terraform: dependencies in the graph from count properly show up --- terraform/graph.go | 6 +++- terraform/graph_test.go | 28 +++++++++++++++++++ .../graph-count-var-resource/main.tf | 9 ++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/graph-count-var-resource/main.tf diff --git a/terraform/graph.go b/terraform/graph.go index ab82b7afd..26e5e6b58 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1046,8 +1046,12 @@ func graphAddVariableDeps(g *depgraph.Graph) { case *GraphNodeResource: if m.Config != nil { + // Handle the count variables + vars := m.Config.RawCount.Variables + nounAddVariableDeps(g, n, vars, false) + // Handle the resource variables - vars := m.Config.RawConfig.Variables + vars = m.Config.RawConfig.Variables nounAddVariableDeps(g, n, vars, false) } diff --git a/terraform/graph_test.go b/terraform/graph_test.go index 7fdd7b044..1eaff78f4 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -43,6 +43,21 @@ func TestGraph_count(t *testing.T) { } } +func TestGraph_varResource(t *testing.T) { + m := testModule(t, "graph-count-var-resource") + + g, err := Graph(&GraphOpts{Module: m}) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.String()) + expected := strings.TrimSpace(testTerraformGraphCountVarResourceStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} + func TestGraph_cycle(t *testing.T) { m := testModule(t, "graph-cycle") @@ -964,6 +979,19 @@ root root -> aws_load_balancer.weblb ` +const testTerraformGraphCountVarResourceStr = ` +root: root +aws_instance.foo +aws_instance.web + aws_instance.web -> aws_instance.foo +aws_load_balancer.weblb + aws_load_balancer.weblb -> aws_instance.web +root + root -> aws_instance.foo + root -> aws_instance.web + root -> aws_load_balancer.weblb +` + const testTerraformGraphDependsStr = ` root: root aws_instance.db diff --git a/terraform/test-fixtures/graph-count-var-resource/main.tf b/terraform/test-fixtures/graph-count-var-resource/main.tf new file mode 100644 index 000000000..9c7407fa5 --- /dev/null +++ b/terraform/test-fixtures/graph-count-var-resource/main.tf @@ -0,0 +1,9 @@ +resource "aws_instance" "foo" {} + +resource "aws_instance" "web" { + count = "${aws_instance.foo.bar}" +} + +resource "aws_load_balancer" "weblb" { + members = "${aws_instance.web.*.id}" +}