Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always evaluate module outputs during destroy #33462

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 19 additions & 31 deletions internal/terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11381,33 +11381,7 @@ locals {
// Ensure that we can destroy when a provider references a resource that will
// also be destroyed
func TestContext2Apply_destroyProviderReference(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
provider "null" {
value = ""
}

module "mod" {
source = "./mod"
}

provider "test" {
value = module.mod.output
}

resource "test_instance" "bar" {
}
`,
"mod/main.tf": `
data "null_data_source" "foo" {
count = 1
}


output "output" {
value = data.null_data_source.foo[0].output
}
`})
m, snap := testModuleWithSnapshot(t, "apply-destroy-provisider-refs")

schemaFn := func(name string) *ProviderSchema {
return &ProviderSchema{
Expand Down Expand Up @@ -11506,18 +11480,32 @@ output "output" {
t.Fatalf("apply errors: %s", diags.Err())
}

providers := map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(testP),
addrs.NewDefaultProvider("null"): testProviderFuncFixed(nullP),
}
ctx = testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(testP),
addrs.NewDefaultProvider("null"): testProviderFuncFixed(nullP),
},
Providers: providers,
})

plan, diags = ctx.Plan(m, state, &PlanOpts{
Mode: plans.DestroyMode,
})
assertNoErrors(t, diags)

// We'll marshal and unmarshal the plan here, to ensure that we have
// a clean new context as would be created if we separately ran
// terraform plan -out=tfplan && terraform apply tfplan
ctxOpts, m, plan, err := contextOptsForPlanViaFile(t, snap, plan)
if err != nil {
t.Fatal(err)
}
ctxOpts.Providers = providers
ctx, diags = NewContext(ctxOpts)

if diags.HasErrors() {
t.Fatalf("failed to create context for plan: %s", diags.Err())
}
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
t.Fatalf("destroy apply errors: %s", diags.Err())
}
Expand Down
4 changes: 0 additions & 4 deletions internal/terraform/node_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ func (n *nodeExpandOutput) DynamicExpand(ctx EvalContext) (*Graph, error) {
Planning: n.Planning,
}

case n.Destroying:
// nothing is done here for non-root outputs
continue

default:
node = &NodeApplyableOutput{
Addr: absAddr,
Expand Down
15 changes: 15 additions & 0 deletions internal/terraform/testdata/apply-destroy-provisider-refs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
provider "null" {
value = ""
}

module "mod" {
source = "./mod"
}

provider "test" {
value = module.mod.output
}

resource "test_instance" "bar" {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "null_data_source" "foo" {
count = 1
}


output "output" {
value = data.null_data_source.foo[0].output
}