@@ -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+ }
0 commit comments