Skip to content

Commit 88a9b79

Browse files
committed
import for_each from data source
1 parent 1c259f5 commit 88a9b79

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

internal/terraform/context_plan_import_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,3 +1281,103 @@ import {
12811281
}
12821282
}
12831283
}
1284+
1285+
func TestContext2Plan_importForEachFromData(t *testing.T) {
1286+
m := testModuleInline(t, map[string]string{
1287+
"main.tf": `
1288+
data "test_object" "d" {
1289+
}
1290+
1291+
resource "test_object" "a" {
1292+
count = 2
1293+
test_string = "foo"
1294+
}
1295+
1296+
import {
1297+
for_each = data.test_object.d.objects
1298+
to = test_object.a[each.key]
1299+
id = each.value
1300+
}
1301+
`,
1302+
})
1303+
1304+
p := simpleMockProvider()
1305+
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
1306+
Provider: providers.Schema{Block: simpleTestSchema()},
1307+
ResourceTypes: map[string]providers.Schema{
1308+
"test_object": providers.Schema{Block: simpleTestSchema()},
1309+
},
1310+
DataSources: map[string]providers.Schema{
1311+
"test_object": providers.Schema{
1312+
Block: &configschema.Block{
1313+
Attributes: map[string]*configschema.Attribute{
1314+
"objects": {
1315+
Type: cty.List(cty.String),
1316+
Computed: true,
1317+
},
1318+
},
1319+
},
1320+
},
1321+
},
1322+
}
1323+
1324+
ctx := testContext2(t, &ContextOpts{
1325+
Providers: map[addrs.Provider]providers.Factory{
1326+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
1327+
},
1328+
})
1329+
1330+
p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
1331+
State: cty.ObjectVal(map[string]cty.Value{
1332+
"objects": cty.ListVal([]cty.Value{
1333+
cty.StringVal("first_id"), cty.StringVal("second_id"),
1334+
}),
1335+
}),
1336+
}
1337+
1338+
p.ReadResourceResponse = &providers.ReadResourceResponse{
1339+
NewState: cty.ObjectVal(map[string]cty.Value{
1340+
"test_string": cty.StringVal("foo"),
1341+
}),
1342+
}
1343+
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
1344+
ImportedResources: []providers.ImportedResource{
1345+
{
1346+
TypeName: "test_object",
1347+
State: cty.ObjectVal(map[string]cty.Value{
1348+
"test_string": cty.StringVal("foo"),
1349+
}),
1350+
},
1351+
},
1352+
}
1353+
1354+
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
1355+
if diags.HasErrors() {
1356+
t.Fatalf("unexpected errors\n%s", diags.Err().Error())
1357+
}
1358+
1359+
firstAddr := mustResourceInstanceAddr(`test_object.a[0]`)
1360+
secondAddr := mustResourceInstanceAddr(`test_object.a[1]`)
1361+
1362+
for _, instPlan := range plan.Changes.Resources {
1363+
switch {
1364+
case instPlan.Addr.Equal(firstAddr):
1365+
if instPlan.Importing.ID != "first_id" {
1366+
t.Errorf("expected import ID of \"first_id\", got %q", instPlan.Importing.ID)
1367+
}
1368+
case instPlan.Addr.Equal(secondAddr):
1369+
if instPlan.Importing.ID != "second_id" {
1370+
t.Errorf("expected import ID of \"second_id\", got %q", instPlan.Importing.ID)
1371+
}
1372+
default:
1373+
t.Errorf("unexpected change for %s", instPlan.Addr)
1374+
}
1375+
1376+
if got, want := instPlan.Action, plans.NoOp; got != want {
1377+
t.Errorf("wrong planned action\ngot: %s\nwant: %s", got, want)
1378+
}
1379+
if got, want := instPlan.ActionReason, plans.ResourceInstanceChangeNoReason; got != want {
1380+
t.Errorf("wrong action reason\ngot: %s\nwant: %s", got, want)
1381+
}
1382+
}
1383+
}

0 commit comments

Comments
 (0)