From 0908e8f42c1be0b77192cde21388678f47a4fe06 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 20 Oct 2014 18:45:52 -0700 Subject: [PATCH] terraform: don't fail refresh if output doesn't exist [GH-483] --- terraform/context.go | 8 ++++ terraform/context_test.go | 44 +++++++++++++++++++ .../refresh-output-partial/main.tf | 7 +++ 3 files changed, 59 insertions(+) create mode 100644 terraform/test-fixtures/refresh-output-partial/main.tf diff --git a/terraform/context.go b/terraform/context.go index 29f9c1c73..47b9d1fae 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -531,6 +531,14 @@ func (c *walkContext) Walk() error { outputs := make(map[string]string) for _, o := range conf.Outputs { if err := c.computeVars(o.RawConfig, nil); err != nil { + // If we're refreshing, then we ignore output errors. This is + // properly not fully the correct behavior, but fixes a range + // of issues right now. As we expand test cases to find the + // correct behavior, this will likely be removed. + if c.Operation == walkRefresh { + continue + } + return err } vraw := o.RawConfig.Config()["value"] diff --git a/terraform/context_test.go b/terraform/context_test.go index 9dc34c07e..ef4b3f5b1 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -4131,6 +4131,46 @@ func TestContextRefresh_noState(t *testing.T) { } } +func TestContextRefresh_outputPartial(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "refresh-output-partial") + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }) + + p.RefreshFn = nil + p.RefreshReturn = nil + + s, err := ctx.Refresh() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(s.String()) + expected := strings.TrimSpace(testContextRefreshOutputPartialStr) + if actual != expected { + t.Fatalf("bad:\n\n%s\n\n%s", actual, expected) + } +} + func TestContextRefresh_state(t *testing.T) { p := testProvider("aws") m := testModule(t, "refresh-basic") @@ -4441,6 +4481,10 @@ module.child: ID = new ` +const testContextRefreshOutputPartialStr = ` + +` + const testContextRefreshTaintedStr = ` aws_instance.web: (1 tainted) ID = diff --git a/terraform/test-fixtures/refresh-output-partial/main.tf b/terraform/test-fixtures/refresh-output-partial/main.tf new file mode 100644 index 000000000..36ce289a3 --- /dev/null +++ b/terraform/test-fixtures/refresh-output-partial/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" {} + +resource "aws_instance" "web" {} + +output "foo" { + value = "${aws_instance.web.foo}" +}