From 1d4e1ed2b7c7552fc7ad0e0abbcfdd3ed682626e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 10 Mar 2021 16:41:00 -0500 Subject: [PATCH] do not panic if there is no deposed state NodeDestroyDeposedResourceInstanceObject should not panic if the deposed state no longer exists. --- terraform/node_resource_destroy_deposed.go | 5 ++++ .../node_resource_destroy_deposed_test.go | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/terraform/node_resource_destroy_deposed.go b/terraform/node_resource_destroy_deposed.go index fc6137ddb..9c80c2b2a 100644 --- a/terraform/node_resource_destroy_deposed.go +++ b/terraform/node_resource_destroy_deposed.go @@ -159,6 +159,11 @@ func (n *NodeDestroyDeposedResourceInstanceObject) Execute(ctx EvalContext, op w return diags.Append(err) } + if state == nil { + diags = diags.Append(fmt.Errorf("missing deposed state for %s (%s)", n.Addr, n.DeposedKey)) + return diags + } + change, destroyPlanDiags := n.planDestroy(ctx, state, n.DeposedKey) diags = diags.Append(destroyPlanDiags) if diags.HasErrors() { diff --git a/terraform/node_resource_destroy_deposed_test.go b/terraform/node_resource_destroy_deposed_test.go index d65b512ef..1f3057bb7 100644 --- a/terraform/node_resource_destroy_deposed_test.go +++ b/terraform/node_resource_destroy_deposed_test.go @@ -176,3 +176,28 @@ aws_instance.foo: (1 deposed) Deposed ID 1 = i-abc123 `) } + +func TestNodeDestroyDeposedResourceInstanceObject_ExecuteMissingState(t *testing.T) { + p := simpleMockProvider() + ctx := &MockEvalContext{ + StateState: states.NewState().SyncWrapper(), + ProviderProvider: simpleMockProvider(), + ProviderSchemaSchema: p.ProviderSchema(), + ChangesChanges: plans.NewChanges().SyncWrapper(), + } + + node := NodeDestroyDeposedResourceInstanceObject{ + NodeAbstractResourceInstance: &NodeAbstractResourceInstance{ + Addr: mustResourceInstanceAddr("test_object.foo"), + NodeAbstractResource: NodeAbstractResource{ + ResolvedProvider: mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`), + }, + }, + DeposedKey: states.NewDeposedKey(), + } + err := node.Execute(ctx, walkApply) + + if err == nil { + t.Fatal("expected error") + } +}