Skip to content

Commit

Permalink
terraform: multi-variables work with count = 1 [GH-115]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 5, 2014
1 parent b19a976 commit 87f4b49
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
<no state>
`

9 changes: 9 additions & 0 deletions terraform/test-fixtures/plan-var-multi-count-one/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_instance" "foo" {
num = "2"

count = 1
}

resource "aws_instance" "bar" {
foo = "${aws_instance.foo.*.num}"
}

0 comments on commit 87f4b49

Please sign in to comment.