From 87f4b49f853b92b4237915473bb1ab4d6608e3a3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 5 Aug 2014 10:12:35 -0700 Subject: [PATCH] terraform: multi-variables work with count = 1 [GH-115] --- CHANGELOG.md | 1 + terraform/context.go | 7 ++++++ terraform/context_test.go | 23 +++++++++++++++++++ terraform/terraform_test.go | 16 +++++++++++++ .../plan-var-multi-count-one/main.tf | 9 ++++++++ 5 files changed, 56 insertions(+) create mode 100644 terraform/test-fixtures/plan-var-multi-count-one/main.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d83a40cf2..f87164ade1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ IMPROVEMENTS: BUG FIXES: * core: Default variable file "terraform.tfvars" is auto-loaded. [GH-59] + * core: Multi-variables (`foo.*.bar`) work even when `count = 1`. [GH-115] * providers/cloudflare: Include the proper bins so the cloudflare provider is compiled * providers/aws: Engine version for RDS now properly set [GH-118] diff --git a/terraform/context.go b/terraform/context.go index 3b837c6d89..32d93cafe3 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -406,6 +406,13 @@ func (c *Context) computeResourceMultiVariable( var values []string for i := 0; i < cr.Count; i++ { id := fmt.Sprintf("%s.%d", v.ResourceId(), i) + + // If we're dealing with only a single resource, then the + // ID doesn't have a trailing index. + if cr.Count == 1 { + id = v.ResourceId() + } + r, ok := c.state.Resources[id] if !ok { continue diff --git a/terraform/context_test.go b/terraform/context_test.go index 1c658d4f28..2557273edd 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1781,6 +1781,29 @@ func TestContextPlan_taint(t *testing.T) { } } +func TestContextPlan_varMultiCountOne(t *testing.T) { + c := testConfig(t, "plan-var-multi-count-one") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanVarMultiCountOneStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestContextRefresh(t *testing.T) { p := testProvider("aws") c := testConfig(t, "refresh-basic") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index b852864cf0..a952f18e2f 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -478,3 +478,19 @@ aws_instance.foo: ID = bar num = 2 ` + +const testTerraformPlanVarMultiCountOneStr = ` +DIFF: + +CREATE: aws_instance.bar + foo: "" => "2" + type: "" => "aws_instance" +CREATE: aws_instance.foo + num: "" => "2" + type: "" => "aws_instance" + +STATE: + + +` + diff --git a/terraform/test-fixtures/plan-var-multi-count-one/main.tf b/terraform/test-fixtures/plan-var-multi-count-one/main.tf new file mode 100644 index 0000000000..6fd84932f1 --- /dev/null +++ b/terraform/test-fixtures/plan-var-multi-count-one/main.tf @@ -0,0 +1,9 @@ +resource "aws_instance" "foo" { + num = "2" + + count = 1 +} + +resource "aws_instance" "bar" { + foo = "${aws_instance.foo.*.num}" +}