diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 95ac647b289e..28e42b02e893 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -7741,6 +7741,12 @@ func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) { Resources: map[string]*ResourceState{ "aws_instance.foo": resourceState("aws_instance", "1"), }, + Outputs: map[string]*OutputState{ + "value": { + Type: "string", + Value: "3", + }, + }, }, &ModuleState{ Path: []string{"root", "mod"}, @@ -7759,18 +7765,30 @@ func TestContext2Apply_destroyProvisionerWithOutput(t *testing.T) { }, }, Destroy: true, + + // targeting the source of the value used by all resources shoudl still + // destroy them all. + Targets: []string{"module.mod.aws_instance.baz"}, }) if _, err := ctx.Plan(); err != nil { t.Fatal(err) } - if _, err := ctx.Apply(); err != nil { + state, err := ctx.Apply() + if err != nil { t.Fatal(err) } if !pr.ApplyCalled { t.Fatal("provisioner not called") } + + // confirm all outputs were removed too + for _, mod := range state.Modules { + if len(mod.Outputs) > 0 { + t.Fatalf("output left in module state: %#v\n", mod) + } + } } func TestContext2Apply_targetedDestroyCountDeps(t *testing.T) { diff --git a/terraform/node_output.go b/terraform/node_output.go index be30911cf810..83e9925a1569 100644 --- a/terraform/node_output.go +++ b/terraform/node_output.go @@ -122,6 +122,12 @@ func (n *NodeDestroyableOutput) RemoveIfNotTargeted() bool { return true } +// This will keep the destroy node in the graph if its corresponding output +// node is also in the destroy graph. +func (n *NodeDestroyableOutput) TargetDownstream(targetedDeps, untargetedDeps *dag.Set) bool { + return true +} + // GraphNodeReferencer func (n *NodeDestroyableOutput) References() []string { var result []string diff --git a/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf b/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf index bb4e01db9ab2..9a5843aa3cc9 100644 --- a/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf +++ b/terraform/test-fixtures/apply-provisioner-destroy-outputs/main.tf @@ -17,3 +17,7 @@ module "mod2" { source = "./mod2" value = "${module.mod.value}" } + +output "value" { + value = "${local.value}" +}