Skip to content

Commit 9da5015

Browse files
authored
Merge pull request #35272 from hashicorp/backport-35262
backport 35262
2 parents 22de83d + 8e6098d commit 9da5015

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

internal/terraform/context_plan_import_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,3 +1564,67 @@ import {
15641564
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
15651565
}
15661566
}
1567+
func TestContext2Plan_importDuringDestroy(t *testing.T) {
1568+
m := testModuleInline(t, map[string]string{
1569+
"main.tf": `
1570+
resource "test_object" "a" {
1571+
test_string = "foo"
1572+
}
1573+
1574+
import {
1575+
to = test_object.a
1576+
id = "missing"
1577+
}
1578+
1579+
resource "test_object" "b" {
1580+
test_string = "foo"
1581+
}
1582+
`,
1583+
})
1584+
1585+
p := simpleMockProvider()
1586+
ctx := testContext2(t, &ContextOpts{
1587+
Providers: map[addrs.Provider]providers.Factory{
1588+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
1589+
},
1590+
})
1591+
p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
1592+
// this resource has already been deleted, so return nothing during refresh
1593+
if req.PriorState.GetAttr("test_string").AsString() == "missing" {
1594+
resp.NewState = cty.NullVal(req.PriorState.Type())
1595+
return resp
1596+
}
1597+
1598+
resp.NewState = req.PriorState
1599+
return resp
1600+
}
1601+
1602+
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
1603+
ImportedResources: []providers.ImportedResource{
1604+
{
1605+
TypeName: "test_object",
1606+
State: cty.ObjectVal(map[string]cty.Value{
1607+
"test_string": cty.StringVal("missing"),
1608+
}),
1609+
},
1610+
},
1611+
}
1612+
1613+
state := states.NewState()
1614+
root := state.EnsureModule(addrs.RootModuleInstance)
1615+
root.SetResourceInstanceCurrent(
1616+
mustResourceInstanceAddr("test_object.b").Resource,
1617+
&states.ResourceInstanceObjectSrc{
1618+
Status: states.ObjectReady,
1619+
AttrsJSON: []byte(`{"test_string":"foo"}`),
1620+
},
1621+
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
1622+
)
1623+
1624+
_, diags := ctx.Plan(m, state, &PlanOpts{
1625+
Mode: plans.DestroyMode,
1626+
})
1627+
if diags.HasErrors() {
1628+
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
1629+
}
1630+
}

internal/terraform/node_resource_plan_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
161161
}
162162
}
163163

164-
importing := n.importTarget.IDString != ""
164+
importing := n.importTarget.IDString != "" && !n.preDestroyRefresh
165165
importId := n.importTarget.IDString
166166

167167
var deferred *providers.Deferred

0 commit comments

Comments
 (0)