From 9edb719aaa48dfcf0a81711099cb41de1a8b20eb Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 3 Dec 2019 17:13:08 -0500 Subject: [PATCH] run AttachStateTransformer in destroy plan The AttachStateTransformer was never run in the destroy plan. This means that resource without configuration that used a non-default provider would not be connected to the correct provider for the plan. The test that was attempting to catch this only worked because the temporary graph used in the DestroyEdgeTransformer would add the state and detect some issues. --- terraform/context_apply_test.go | 43 ++++++++----------------- terraform/graph_builder_destroy_plan.go | 3 ++ 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 8f316c5fa..0f544d1e3 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -10236,29 +10236,16 @@ func TestContext2Apply_destroyWithProviders(t *testing.T) { p.ApplyFn = testApplyFn p.DiffFn = testDiffFn - s := MustShimLegacyState(&State{ - Modules: []*ModuleState{ - &ModuleState{ - Path: rootModulePath, - }, - &ModuleState{ - Path: []string{"root", "child"}, - }, - &ModuleState{ - Path: []string{"root", "mod", "removed"}, - Resources: map[string]*ResourceState{ - "aws_instance.child": &ResourceState{ - Type: "aws_instance", - Primary: &InstanceState{ - ID: "bar", - }, - // this provider doesn't exist - Provider: "provider.aws.baz", - }, - }, - }, + state := states.NewState() + removed := state.EnsureModule(addrs.RootModuleInstance.Child("mod", addrs.NoKey).Child("removed", addrs.NoKey)) + removed.SetResourceInstanceCurrent( + mustResourceInstanceAddr("aws_instance.child").Resource, + &states.ResourceInstanceObjectSrc{ + Status: states.ObjectReady, + AttrsJSON: []byte(`{"id":"bar"}`), }, - }) + mustProviderConfig("provider.aws.baz"), + ) ctx := testContext2(t, &ContextOpts{ Config: m, @@ -10267,7 +10254,7 @@ func TestContext2Apply_destroyWithProviders(t *testing.T) { addrs.NewLegacyProvider("aws"): testProviderFuncFixed(p), }, ), - State: s, + State: state, Destroy: true, }) @@ -10277,12 +10264,10 @@ func TestContext2Apply_destroyWithProviders(t *testing.T) { } // correct the state - s.Modules["module.mod.module.removed"].Resources["aws_instance.child"].ProviderConfig = addrs.AbsProviderConfig{ - Provider: addrs.NewLegacyProvider("aws"), - Module: addrs.RootModuleInstance, - Alias: "bar", - } - + state.Modules["module.mod.module.removed"].Resources["aws_instance.child"].ProviderConfig = addrs.ProviderConfig{ + Type: addrs.NewLegacyProvider("aws"), + Alias: "bar", + }.Absolute(addrs.RootModuleInstance) if _, diags := ctx.Plan(); diags.HasErrors() { t.Fatal(diags.Err()) } diff --git a/terraform/graph_builder_destroy_plan.go b/terraform/graph_builder_destroy_plan.go index a6047a9b4..08ee4e63e 100644 --- a/terraform/graph_builder_destroy_plan.go +++ b/terraform/graph_builder_destroy_plan.go @@ -72,6 +72,9 @@ func (b *DestroyPlanGraphBuilder) Steps() []GraphTransformer { State: b.State, }, + // Attach the state + &AttachStateTransformer{State: b.State}, + // Attach the configuration to any resources &AttachResourceConfigTransformer{Config: b.Config},