From 53d05cb81fdca8ebcc83d9ed9434f05472942474 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Oct 2014 16:21:17 -0700 Subject: [PATCH] terraform: counts can't be computed --- terraform/context_test.go | 17 +++++++++++++++++ terraform/graph.go | 10 +++++++++- .../test-fixtures/plan-count-computed/main.tf | 8 ++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/plan-count-computed/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 1313a6f27..b4ea97824 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -2428,6 +2428,23 @@ func TestContextPlan_count(t *testing.T) { } } +func TestContextPlan_countComputed(t *testing.T) { + m := testModule(t, "plan-count-computed") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + _, err := ctx.Plan(nil) + if err == nil { + t.Fatal("should error") + } +} + func TestContextPlan_countVar(t *testing.T) { m := testModule(t, "plan-count-var") p := testProvider("aws") diff --git a/terraform/graph.go b/terraform/graph.go index 26e5e6b58..ae34d6452 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -1595,7 +1595,15 @@ func (p *graphSharedProvider) MergeConfig( // Expand will expand this node into a subgraph if Expand is set. func (n *GraphNodeResource) Expand() ([]*depgraph.Noun, error) { - // Expand the count out, which should be interpolated at this point + // If the count configuration is empty then it means that the + // count is computed. In this case, we set the count to one + // but set a flag telling upstream that we're computing. + if len(n.Config.RawConfig.Config()) == 0 { + return nil, fmt.Errorf( + "%s: computed count attribute not allowed", n.Resource.Id) + } + + // Expand the count out, which should be interpolated at this point. count, err := n.Config.Count() if err != nil { return nil, err diff --git a/terraform/test-fixtures/plan-count-computed/main.tf b/terraform/test-fixtures/plan-count-computed/main.tf new file mode 100644 index 000000000..8a029236b --- /dev/null +++ b/terraform/test-fixtures/plan-count-computed/main.tf @@ -0,0 +1,8 @@ +resource "aws_instance" "foo" { + num = "2" + compute = "foo" +} + +resource "aws_instance" "bar" { + count = "${aws_instance.foo.foo}" +}