Skip to content

Commit

Permalink
Merge pull request hashicorp#35262 from hashicorp/jbardin/destroy-import
Browse files Browse the repository at this point in the history
Don't attempt to import during a destroy operation
  • Loading branch information
jbardin authored May 31, 2024
2 parents a73e4e6 + a5443a8 commit 611210d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions internal/terraform/context_plan_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1564,3 +1564,67 @@ import {
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
}
}
func TestContext2Plan_importDuringDestroy(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_object" "a" {
test_string = "foo"
}
import {
to = test_object.a
id = "missing"
}
resource "test_object" "b" {
test_string = "foo"
}
`,
})

p := simpleMockProvider()
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
// this resource has already been deleted, so return nothing during refresh
if req.PriorState.GetAttr("test_string").AsString() == "missing" {
resp.NewState = cty.NullVal(req.PriorState.Type())
return resp
}

resp.NewState = req.PriorState
return resp
}

p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
{
TypeName: "test_object",
State: cty.ObjectVal(map[string]cty.Value{
"test_string": cty.StringVal("missing"),
}),
},
},
}

state := states.NewState()
root := state.EnsureModule(addrs.RootModuleInstance)
root.SetResourceInstanceCurrent(
mustResourceInstanceAddr("test_object.b").Resource,
&states.ResourceInstanceObjectSrc{
Status: states.ObjectReady,
AttrsJSON: []byte(`{"test_string":"foo"}`),
},
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
)

_, diags := ctx.Plan(m, state, &PlanOpts{
Mode: plans.DestroyMode,
})
if diags.HasErrors() {
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
}
}
2 changes: 1 addition & 1 deletion internal/terraform/node_resource_plan_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
}
}

importing := n.importTarget.IDString != ""
importing := n.importTarget.IDString != "" && !n.preDestroyRefresh
importId := n.importTarget.IDString

var deferred *providers.Deferred
Expand Down

0 comments on commit 611210d

Please sign in to comment.