From 0a342e8dc2186cf6dc16690185b1f622ce6a12bc Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 1 Sep 2017 09:35:49 -0700 Subject: [PATCH] config: allow local value interpolations in count There is some additional, early validation on the "count" meta-argument that verifies that only suitable variable types are used, and adding local values to this whitelist was missed in the initial implementation. --- config/config.go | 1 + config/config_test.go | 7 ++++ .../validate-local-value-count/main.tf | 8 +++++ terraform/context_plan_test.go | 33 +++++++++++++++++++ .../plan-local-value-count/main.tf | 8 +++++ 5 files changed, 57 insertions(+) create mode 100644 config/test-fixtures/validate-local-value-count/main.tf create mode 100644 terraform/test-fixtures/plan-local-value-count/main.tf diff --git a/config/config.go b/config/config.go index 9549d0a50..949ad06d0 100644 --- a/config/config.go +++ b/config/config.go @@ -557,6 +557,7 @@ func (c *Config) Validate() error { case *ResourceVariable: case *TerraformVariable: case *UserVariable: + case *LocalVariable: default: errs = append(errs, fmt.Errorf( diff --git a/config/config_test.go b/config/config_test.go index 49c4d9ac3..50301f21f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -312,6 +312,13 @@ func TestConfigValidate_countUserVar(t *testing.T) { } } +func TestConfigValidate_countLocalValue(t *testing.T) { + c := testConfig(t, "validate-local-value-count") + if err := c.Validate(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestConfigValidate_countVar(t *testing.T) { c := testConfig(t, "validate-count-var") if err := c.Validate(); err != nil { diff --git a/config/test-fixtures/validate-local-value-count/main.tf b/config/test-fixtures/validate-local-value-count/main.tf new file mode 100644 index 000000000..b50c4b360 --- /dev/null +++ b/config/test-fixtures/validate-local-value-count/main.tf @@ -0,0 +1,8 @@ + +locals { + count = 3 +} + +resource "null_resource" "foo" { + count = "${local.count}" +} diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 39baa426d..0f6fdf284 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -1294,6 +1294,39 @@ func TestContext2Plan_computedDataCountResource(t *testing.T) { } } +func TestContext2Plan_localValueCount(t *testing.T) { + m := testModule(t, "plan-local-value-count") + p := testProvider("test") + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + ProviderResolver: ResourceProviderResolverFixed( + map[string]ResourceProviderFactory{ + "test": testProviderFuncFixed(p), + }, + ), + }) + + plan, err := ctx.Plan() + if err != nil { + t.Fatalf("err: %s", err) + } + + if got := len(plan.Diff.Modules); got != 1 { + t.Fatalf("got %d modules; want 1", got) + } + + moduleDiff := plan.Diff.Modules[0] + + // make sure we created 3 "bar"s + for i := 0; i < 3; i++ { + resource := fmt.Sprintf("test_resource.foo.%d", i) + if _, ok := moduleDiff.Resources[resource]; !ok { + t.Fatalf("missing diff for %s", resource) + } + } +} + // Higher level test at TestResource_dataSourceListPlanPanic func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) { m := testModule(t, "plan-data-source-type-mismatch") diff --git a/terraform/test-fixtures/plan-local-value-count/main.tf b/terraform/test-fixtures/plan-local-value-count/main.tf new file mode 100644 index 000000000..34aad96ad --- /dev/null +++ b/terraform/test-fixtures/plan-local-value-count/main.tf @@ -0,0 +1,8 @@ + +locals { + count = 3 +} + +resource "test_resource" "foo" { + count = "${local.count}" +}