From 70a41c5e1507670d51bbef6dc3d4c813e5c0626b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Nov 2016 18:16:04 -0800 Subject: [PATCH] terraform: output nodes reference `depends_on` values --- terraform/context_apply_test.go | 71 +++++++++++++++++++ terraform/node_output.go | 1 + .../apply-output-depends-on/main.tf | 7 ++ 3 files changed, 79 insertions(+) create mode 100644 terraform/test-fixtures/apply-output-depends-on/main.tf diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index b6ac3b277..0d0ab0004 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -2243,6 +2243,77 @@ func TestContext2Apply_nilDiff(t *testing.T) { } } +func TestContext2Apply_outputDependsOn(t *testing.T) { + m := testModule(t, "apply-output-depends-on") + p := testProvider("aws") + p.DiffFn = testDiffFn + + { + // Create a custom apply function that sleeps a bit (to allow parallel + // graph execution) and then returns an error to force a partial state + // return. We then verify the output is NOT there. + p.ApplyFn = func( + info *InstanceInfo, + is *InstanceState, + id *InstanceDiff) (*InstanceState, error) { + + // Sleep to allow parallel execution + time.Sleep(50 * time.Millisecond) + + // Return error to force partial state + return nil, fmt.Errorf("abcd") + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err == nil || !strings.Contains(err.Error(), "abcd") { + t.Fatalf("err: %s", err) + } + + checkStateString(t, state, ``) + } + + { + // Create the standard apply function and verify we get the output + p.ApplyFn = testApplyFn + + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + checkStateString(t, state, ` +aws_instance.foo: + ID = foo + +Outputs: + +value = result + `) + } +} + func TestContext2Apply_outputOrphan(t *testing.T) { m := testModule(t, "apply-output-orphan") p := testProvider("aws") diff --git a/terraform/node_output.go b/terraform/node_output.go index 3e6001618..41962c2cb 100644 --- a/terraform/node_output.go +++ b/terraform/node_output.go @@ -37,6 +37,7 @@ func (n *NodeApplyableOutput) ReferenceableName() []string { // GraphNodeReferencer func (n *NodeApplyableOutput) References() []string { var result []string + result = append(result, n.Config.DependsOn...) result = append(result, ReferencesFromConfig(n.Config.RawConfig)...) for _, v := range result { split := strings.Split(v, "/") diff --git a/terraform/test-fixtures/apply-output-depends-on/main.tf b/terraform/test-fixtures/apply-output-depends-on/main.tf new file mode 100644 index 000000000..4923a6f58 --- /dev/null +++ b/terraform/test-fixtures/apply-output-depends-on/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "foo" {} + +output "value" { + value = "result" + + depends_on = ["aws_instance.foo"] +}