diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 032310aac..2f4d43296 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -894,6 +894,43 @@ func TestContext2Apply_moduleProviderAliasTargets(t *testing.T) { } } +func TestContext2Apply_moduleProviderCloseNested(t *testing.T) { + m := testModule(t, "apply-module-provider-close-nested") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + State: &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root", "child", "subchild"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + }, + }, + }, + Destroy: true, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + if _, err := ctx.Apply(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestContext2Apply_moduleVarResourceCount(t *testing.T) { m := testModule(t, "apply-module-var-resource-count") p := testProvider("aws") diff --git a/terraform/graph_config_node_resource.go b/terraform/graph_config_node_resource.go index 936398d2a..2235f4925 100644 --- a/terraform/graph_config_node_resource.go +++ b/terraform/graph_config_node_resource.go @@ -335,6 +335,13 @@ func (n *graphNodeResourceDestroyFlat) CreateNode() dag.Vertex { return n.FlatCreateNode } +func (n *graphNodeResourceDestroyFlat) ProvidedBy() []string { + prefix := modulePrefixStr(n.PathValue) + return modulePrefixList( + n.GraphNodeConfigResource.ProvidedBy(), + prefix) +} + // graphNodeResourceDestroy represents the logical destruction of a // resource. This node doesn't mean it will be destroyed for sure, but // instead that if a destroy were to happen, it must happen at this point. diff --git a/terraform/test-fixtures/apply-module-provider-close-nested/child/main.tf b/terraform/test-fixtures/apply-module-provider-close-nested/child/main.tf new file mode 100644 index 000000000..852bce8b9 --- /dev/null +++ b/terraform/test-fixtures/apply-module-provider-close-nested/child/main.tf @@ -0,0 +1,3 @@ +module "subchild" { + source = "./subchild" +} diff --git a/terraform/test-fixtures/apply-module-provider-close-nested/child/subchild/main.tf b/terraform/test-fixtures/apply-module-provider-close-nested/child/subchild/main.tf new file mode 100644 index 000000000..919f140bb --- /dev/null +++ b/terraform/test-fixtures/apply-module-provider-close-nested/child/subchild/main.tf @@ -0,0 +1 @@ +resource "aws_instance" "foo" {} diff --git a/terraform/test-fixtures/apply-module-provider-close-nested/main.tf b/terraform/test-fixtures/apply-module-provider-close-nested/main.tf new file mode 100644 index 000000000..0f6991c53 --- /dev/null +++ b/terraform/test-fixtures/apply-module-provider-close-nested/main.tf @@ -0,0 +1,3 @@ +module "child" { + source = "./child" +} diff --git a/terraform/transform_provider.go b/terraform/transform_provider.go index fb8857c96..8a6655182 100644 --- a/terraform/transform_provider.go +++ b/terraform/transform_provider.go @@ -130,7 +130,7 @@ func (t *CloseProviderTransformer) Transform(g *Graph) error { provider, ok := pm[p] if !ok { err = multierror.Append(err, fmt.Errorf( - "%s: provider %s couldn't be found", + "%s: provider %s couldn't be found for closing", dag.VertexName(v), p)) continue }