terraform: multi-variables work with count = 1 [GH-115]

This commit is contained in:
Mitchell Hashimoto 2014-08-05 10:12:35 -07:00
parent b19a976696
commit 87f4b49f85
5 changed files with 56 additions and 0 deletions

View File

@ -13,6 +13,7 @@ IMPROVEMENTS:
BUG FIXES: BUG FIXES:
* core: Default variable file "terraform.tfvars" is auto-loaded. [GH-59] * 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 * providers/cloudflare: Include the proper bins so the cloudflare
provider is compiled provider is compiled
* providers/aws: Engine version for RDS now properly set [GH-118] * providers/aws: Engine version for RDS now properly set [GH-118]

View File

@ -406,6 +406,13 @@ func (c *Context) computeResourceMultiVariable(
var values []string var values []string
for i := 0; i < cr.Count; i++ { for i := 0; i < cr.Count; i++ {
id := fmt.Sprintf("%s.%d", v.ResourceId(), 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] r, ok := c.state.Resources[id]
if !ok { if !ok {
continue continue

View File

@ -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) { func TestContextRefresh(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
c := testConfig(t, "refresh-basic") c := testConfig(t, "refresh-basic")

View File

@ -478,3 +478,19 @@ aws_instance.foo:
ID = bar ID = bar
num = 2 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>
`

View File

@ -0,0 +1,9 @@
resource "aws_instance" "foo" {
num = "2"
count = 1
}
resource "aws_instance" "bar" {
foo = "${aws_instance.foo.*.num}"
}