From cf432b3ceb3d7e661f2201718a1511e40cdd7c7a Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Mon, 29 Jun 2015 13:19:37 -0500 Subject: [PATCH] core: move targets transform after flatten Allows target dependencies to be properly calculated across module boundaries, fixing scenarios where a target depends on something inside a module, but the contents of the module are not included in the targeted resources. fixes #1858 --- terraform/context_test.go | 42 +++++++++++++++++++ terraform/graph_builder.go | 8 ++-- .../apply-targeted-module-dep/child/main.tf | 5 +++ .../apply-targeted-module-dep/main.tf | 7 ++++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 terraform/test-fixtures/apply-targeted-module-dep/child/main.tf create mode 100644 terraform/test-fixtures/apply-targeted-module-dep/main.tf diff --git a/terraform/context_test.go b/terraform/context_test.go index 0cb256168..999730bd6 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -6593,6 +6593,48 @@ module.child: `) } +// GH-1858 +func TestContext2Apply_targetedModuleDep(t *testing.T) { + m := testModule(t, "apply-targeted-module-dep") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Targets: []string{"aws_instance.foo"}, + }) + + 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 + foo = foo + type = aws_instance + + Dependencies: + module.child + +module.child: + aws_instance.mod: + ID = foo + + Outputs: + + output = foo + `) +} + func TestContext2Apply_targetedModuleResource(t *testing.T) { m := testModule(t, "apply-targeted-module-resource") p := testProvider("aws") diff --git a/terraform/graph_builder.go b/terraform/graph_builder.go index 1fbeb7399..a4475372e 100644 --- a/terraform/graph_builder.go +++ b/terraform/graph_builder.go @@ -140,10 +140,6 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer { // Make sure all the connections that are proxies are connected through &ProxyTransformer{}, - // Optionally reduces the graph to a user-specified list of targets and - // their dependencies. - &TargetsTransformer{Targets: b.Targets, Destroy: b.Destroy}, - // Make sure we have a single root &RootTransformer{}, } @@ -152,6 +148,10 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer { // We don't do the following for modules. if len(path) <= 1 { steps = append(steps, + // Optionally reduces the graph to a user-specified list of targets and + // their dependencies. + &TargetsTransformer{Targets: b.Targets, Destroy: b.Destroy}, + // Prune the providers and provisioners. This must happen // only once because flattened modules might depend on empty // providers. diff --git a/terraform/test-fixtures/apply-targeted-module-dep/child/main.tf b/terraform/test-fixtures/apply-targeted-module-dep/child/main.tf new file mode 100644 index 000000000..90a7c407b --- /dev/null +++ b/terraform/test-fixtures/apply-targeted-module-dep/child/main.tf @@ -0,0 +1,5 @@ +resource "aws_instance" "mod" { } + +output "output" { + value = "${aws_instance.mod.id}" +} diff --git a/terraform/test-fixtures/apply-targeted-module-dep/main.tf b/terraform/test-fixtures/apply-targeted-module-dep/main.tf new file mode 100644 index 000000000..754219c3e --- /dev/null +++ b/terraform/test-fixtures/apply-targeted-module-dep/main.tf @@ -0,0 +1,7 @@ +module "child" { + source = "./child" +} + +resource "aws_instance" "foo" { + foo = "${module.child.output}" +}