From 22f09b487b9e48d363f515cbc65241f65132565d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 20 Oct 2014 23:08:17 -0700 Subject: [PATCH] terraform: refresh ignores variables with no values [GH-478] --- terraform/context.go | 20 ++++++++++++------- terraform/context_test.go | 16 +++++++++++++++ .../child/main.tf | 9 +++++++++ .../main.tf | 8 ++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf create mode 100644 terraform/test-fixtures/refresh-module-input-computed-output/main.tf diff --git a/terraform/context.go b/terraform/context.go index 47b9d1fae..e2d8bbd33 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1611,19 +1611,25 @@ func (c *walkContext) computeResourceVariable( // Get the relevant module module := c.Context.state.ModuleByPath(c.Path) - r, ok := module.Resources[id] - if !ok { - if v.Multi && v.Index == 0 { + var r *ResourceState + if module != nil { + var ok bool + r, ok = module.Resources[id] + if !ok && v.Multi && v.Index == 0 { r, ok = module.Resources[v.ResourceId()] } if !ok { - return "", fmt.Errorf( - "Resource '%s' not found for variable '%s'", - id, - v.FullKey()) + r = nil } } + if r == nil { + return "", fmt.Errorf( + "Resource '%s' not found for variable '%s'", + id, + v.FullKey()) + } + if r.Primary == nil { goto MISSING } diff --git a/terraform/context_test.go b/terraform/context_test.go index ef4b3f5b1..5ad260763 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4110,6 +4110,22 @@ func TestContextRefresh_modules(t *testing.T) { } } +func TestContextRefresh_moduleInputComputedOutput(t *testing.T) { + m := testModule(t, "refresh-module-input-computed-output") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Refresh(); err != nil { + t.Fatalf("err: %s", err) + } +} + // GH-70 func TestContextRefresh_noState(t *testing.T) { p := testProvider("aws") diff --git a/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf new file mode 100644 index 000000000..fff03a60f --- /dev/null +++ b/terraform/test-fixtures/refresh-module-input-computed-output/child/main.tf @@ -0,0 +1,9 @@ +variable "input" {} + +resource "aws_instance" "foo" { + foo = "${var.input}" +} + +output "foo" { + value = "${aws_instance.foo.foo}" +} diff --git a/terraform/test-fixtures/refresh-module-input-computed-output/main.tf b/terraform/test-fixtures/refresh-module-input-computed-output/main.tf new file mode 100644 index 000000000..3a0576434 --- /dev/null +++ b/terraform/test-fixtures/refresh-module-input-computed-output/main.tf @@ -0,0 +1,8 @@ +module "child" { + input = "${aws_instance.bar.foo}" + source = "./child" +} + +resource "aws_instance" "bar" { + compute = "foo" +}