From cc0c32d5ca5f719ffd27e064863a7f28555f6eb4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 6 Jul 2014 14:09:44 -0700 Subject: [PATCH] terraform: test for single index access of counts --- terraform/context.go | 13 ++++++--- terraform/context_test.go | 28 +++++++++++++++++++ terraform/terraform_test.go | 23 +++++++++++++++ .../apply-output-multi-index/main.tf | 12 ++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 terraform/test-fixtures/apply-output-multi-index/main.tf diff --git a/terraform/context.go b/terraform/context.go index a0c35772e..a8b9e3365 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -260,7 +260,7 @@ func (c *Context) computeVars(raw *config.RawConfig) error { case *config.ResourceVariable: var attr string var err error - if v.Multi { + if v.Multi && v.Index == -1 { attr, err = c.computeResourceMultiVariable(v) } else { attr, err = c.computeResourceVariable(v) @@ -281,11 +281,16 @@ func (c *Context) computeVars(raw *config.RawConfig) error { func (c *Context) computeResourceVariable( v *config.ResourceVariable) (string, error) { - r, ok := c.state.Resources[v.ResourceId()] + id := v.ResourceId() + if v.Multi { + id = fmt.Sprintf("%s.%d", id, v.Index) + } + + r, ok := c.state.Resources[id] if !ok { return "", fmt.Errorf( "Resource '%s' not found for variable '%s'", - v.ResourceId(), + id, v.FullKey()) } @@ -294,7 +299,7 @@ func (c *Context) computeResourceVariable( return "", fmt.Errorf( "Resource '%s' does not have attribute '%s' "+ "for variable '%s'", - v.ResourceId(), + id, v.Field, v.FullKey()) } diff --git a/terraform/context_test.go b/terraform/context_test.go index e0da66d3e..b24335150 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -564,6 +564,34 @@ func TestContextApply_outputMulti(t *testing.T) { } } +func TestContextApply_outputMultiIndex(t *testing.T) { + c := testConfig(t, "apply-output-multi-index") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Config: c, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(nil); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(testTerraformApplyOutputMultiIndexStr) + if actual != expected { + t.Fatalf("bad: \n%s", actual) + } +} + func TestContextApply_unknownAttribute(t *testing.T) { c := testConfig(t, "apply-unknown") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index ce2b58733..1f5e792a1 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -144,6 +144,29 @@ Outputs: foo_num = bar,bar,bar ` +const testTerraformApplyOutputMultiIndexStr = ` +aws_instance.bar.0: + ID = foo + foo = bar + type = aws_instance +aws_instance.bar.1: + ID = foo + foo = bar + type = aws_instance +aws_instance.bar.2: + ID = foo + foo = bar + type = aws_instance +aws_instance.foo: + ID = foo + num = 2 + type = aws_instance + +Outputs: + +foo_num = bar +` + const testTerraformApplyUnknownAttrStr = ` aws_instance.foo: ID = foo diff --git a/terraform/test-fixtures/apply-output-multi-index/main.tf b/terraform/test-fixtures/apply-output-multi-index/main.tf new file mode 100644 index 000000000..7db99d62b --- /dev/null +++ b/terraform/test-fixtures/apply-output-multi-index/main.tf @@ -0,0 +1,12 @@ +resource "aws_instance" "foo" { + num = "2" +} + +resource "aws_instance" "bar" { + foo = "bar" + count = 3 +} + +output "foo_num" { + value = "${aws_instance.bar.0.foo}" +}