From fe5f51981718a474f867960755fcdd5cad92d45f Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 16 Mar 2017 10:30:14 -0400 Subject: [PATCH 1/2] Add failing test for invalid interpolation Adding the submodule causes the count variable interpolation to fail. --- terraform/context_input_test.go | 27 +++++++++++++++++++ .../input-submodule-count/main.tf | 4 +++ .../input-submodule-count/mod/main.tf | 11 ++++++++ .../input-submodule-count/mod/submod/main.tf | 7 +++++ 4 files changed, 49 insertions(+) create mode 100644 terraform/test-fixtures/input-submodule-count/main.tf create mode 100644 terraform/test-fixtures/input-submodule-count/mod/main.tf create mode 100644 terraform/test-fixtures/input-submodule-count/mod/submod/main.tf diff --git a/terraform/context_input_test.go b/terraform/context_input_test.go index f5e0f47a2..5e3434bd8 100644 --- a/terraform/context_input_test.go +++ b/terraform/context_input_test.go @@ -658,3 +658,30 @@ func TestContext2Input_hcl(t *testing.T) { t.Fatalf("bad: \n%s", actualStr) } } + +// adding a list interpolation in fails to interpolate the count variable +func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) { + input := new(MockUIInput) + m := testModule(t, "input-submodule-count") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + UIInput: input, + }) + + p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) { + return c, nil + } + p.ConfigureFn = func(c *ResourceConfig) error { + return nil + } + + if err := ctx.Input(InputModeStd); err != nil { + t.Fatalf("err: %s", err) + } +} diff --git a/terraform/test-fixtures/input-submodule-count/main.tf b/terraform/test-fixtures/input-submodule-count/main.tf new file mode 100644 index 000000000..1cbfc3450 --- /dev/null +++ b/terraform/test-fixtures/input-submodule-count/main.tf @@ -0,0 +1,4 @@ +module "mod" { + source = "./mod" + count = 2 +} diff --git a/terraform/test-fixtures/input-submodule-count/mod/main.tf b/terraform/test-fixtures/input-submodule-count/mod/main.tf new file mode 100644 index 000000000..995abe256 --- /dev/null +++ b/terraform/test-fixtures/input-submodule-count/mod/main.tf @@ -0,0 +1,11 @@ +variable "count" { +} + +resource "aws_instance" "foo" { + count = "${var.count}" +} + +module "submod" { + source = "./submod" + list = ["${aws_instance.foo.*.id}"] +} diff --git a/terraform/test-fixtures/input-submodule-count/mod/submod/main.tf b/terraform/test-fixtures/input-submodule-count/mod/submod/main.tf new file mode 100644 index 000000000..c0c8d15af --- /dev/null +++ b/terraform/test-fixtures/input-submodule-count/mod/submod/main.tf @@ -0,0 +1,7 @@ +variable "list" { + type = "list" +} + +resource "aws_instance" "bar" { + count = "${var.list[0]}" +} From 9e8ddaed47b1994d388a2e516a4427a3165d84d6 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 16 Mar 2017 13:41:55 -0400 Subject: [PATCH 2/2] Don't interpolate multivariables during walkInput We don't need these expanded for input, and if the multivar's count is a variable, it may be known but not available during the input walk. --- terraform/interpolate.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/terraform/interpolate.go b/terraform/interpolate.go index 993317e9e..7a3270fc9 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -540,6 +540,13 @@ func (i *Interpolater) computeResourceMultiVariable( unknownVariable := unknownVariable() + // If we're only looking for input, we don't need to expand a + // multi-variable. This prevents us from encountering things that should be + // known but aren't because the state has yet to be refreshed. + if i.Operation == walkInput { + return &unknownVariable, nil + } + // Get the information about this resource variable, and verify // that it exists and such. module, cr, err := i.resourceVariableInfo(scope, v)