From e8b3062629659f4f0c15e4d1d0fde60dee695506 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Mar 2017 15:29:10 -0700 Subject: [PATCH] terraform: unknown value for variables not set Fixes #12836 Realistically, these should be caught during validation anyways. In this case, this was causing 12386 because refresh with a data source will attempt to use module variables. I don't see any clear logic to prune those module variables or not add them so its easier to return unknown to cause the data to be computed and not run. --- terraform/context_refresh_test.go | 26 +++++++++++++++++++ terraform/interpolate.go | 2 +- .../refresh-data-module-var/child/main.tf | 6 +++++ .../refresh-data-module-var/main.tf | 8 ++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/refresh-data-module-var/child/main.tf create mode 100644 terraform/test-fixtures/refresh-data-module-var/main.tf diff --git a/terraform/context_refresh_test.go b/terraform/context_refresh_test.go index 7c00cf4f4..b29e63679 100644 --- a/terraform/context_refresh_test.go +++ b/terraform/context_refresh_test.go @@ -60,6 +60,32 @@ func TestContext2Refresh(t *testing.T) { } } +func TestContext2Refresh_dataComputedModuleVar(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "refresh-data-module-var") + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + p.RefreshFn = nil + p.RefreshReturn = &InstanceState{ + ID: "foo", + } + + s, err := ctx.Refresh() + if err != nil { + t.Fatalf("err: %s", err) + } + + checkStateString(t, s, ` + +module.child: + `) +} + func TestContext2Refresh_targeted(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-targeted") diff --git a/terraform/interpolate.go b/terraform/interpolate.go index 7a3270fc9..0c5acaa35 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -262,7 +262,7 @@ func (i *Interpolater) valueResourceVar( // If it truly is missing, we'll catch it on a later walk. // This applies only to graph nodes that interpolate during the // config walk, e.g. providers. - if i.Operation == walkInput { + if i.Operation == walkInput || i.Operation == walkRefresh { result[n] = unknownVariable() return nil } diff --git a/terraform/test-fixtures/refresh-data-module-var/child/main.tf b/terraform/test-fixtures/refresh-data-module-var/child/main.tf new file mode 100644 index 000000000..64d21beda --- /dev/null +++ b/terraform/test-fixtures/refresh-data-module-var/child/main.tf @@ -0,0 +1,6 @@ +variable "key" {} + +data "aws_data_source" "foo" { + id = "${var.key}" +} + diff --git a/terraform/test-fixtures/refresh-data-module-var/main.tf b/terraform/test-fixtures/refresh-data-module-var/main.tf new file mode 100644 index 000000000..06f18b1b5 --- /dev/null +++ b/terraform/test-fixtures/refresh-data-module-var/main.tf @@ -0,0 +1,8 @@ +resource "aws_instance" "A" { + foo = "bar" +} + +module "child" { + source = "child" + key = "${aws_instance.A.id}" +}