Skip to content

Commit 221fc59

Browse files
authored
Merge pull request #37713 from hashicorp/jbardin/list-gen-config
get correct schema when generating query config
2 parents 2274026 + 9c2b2c5 commit 221fc59

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

internal/command/query_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func TestQuery_JSON(t *testing.T) {
536536
"ami": "ami-12345",
537537
"id": "test-instance-1",
538538
},
539-
"config": "resource \"test_instance\" \"example_0\" {\n provider = test\n ami = \"ami-12345\"\n id = \"test-instance-1\"\n}",
539+
"config": "resource \"test_instance\" \"example_0\" {\n provider = test\n ami = \"ami-12345\"\n}",
540540
"import_config": "import {\n to = test_instance.example_0\n provider = test\n identity = {\n id = \"test-instance-1\"\n }\n}",
541541
},
542542
"type": "list_resource_found",
@@ -556,7 +556,7 @@ func TestQuery_JSON(t *testing.T) {
556556
"ami": "ami-67890",
557557
"id": "test-instance-2",
558558
},
559-
"config": "resource \"test_instance\" \"example_1\" {\n provider = test\n ami = \"ami-67890\"\n id = \"test-instance-2\"\n}",
559+
"config": "resource \"test_instance\" \"example_1\" {\n provider = test\n ami = \"ami-67890\"\n}",
560560
"import_config": "import {\n to = test_instance.example_1\n provider = test\n identity = {\n id = \"test-instance-2\"\n }\n}",
561561
},
562562
"type": "list_resource_found",

internal/terraform/context_plan_query_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func TestContext2Plan_queryList(t *testing.T) {
3030
instanceTypes := []string{"ami-123456", "ami-654321", "ami-789012"}
3131
madeUp := []cty.Value{}
3232
for i := range len(instanceTypes) {
33-
madeUp = append(madeUp, cty.ObjectVal(map[string]cty.Value{"instance_type": cty.StringVal(instanceTypes[i])}))
33+
madeUp = append(madeUp, cty.ObjectVal(map[string]cty.Value{
34+
"instance_type": cty.StringVal(instanceTypes[i]),
35+
"id": cty.StringVal(fmt.Sprint(i)),
36+
}))
3437
}
3538

3639
ids := []cty.Value{}
@@ -1004,6 +1007,10 @@ func getListProviderSchemaResp() *providers.GetProviderSchemaResponse {
10041007
Computed: true,
10051008
Optional: true,
10061009
},
1010+
"id": {
1011+
Type: cty.String,
1012+
Computed: true,
1013+
},
10071014
},
10081015
},
10091016
"test_child_resource": {

internal/terraform/node_resource_plan_instance.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,10 +977,17 @@ func (n *NodePlannableResourceInstance) generateResourceConfig(ctx EvalContext,
977977
if diags.HasErrors() {
978978
return cty.DynamicVal, diags
979979
}
980-
schema := providerSchema.SchemaForResourceAddr(n.Addr.Resource.Resource)
980+
981+
// the calling node may be a list resource, in which case we still need to
982+
// lookup the schema for the corresponding managed resource for generating
983+
// configuration.
984+
managedAddr := n.Addr.Resource.Resource
985+
managedAddr.Mode = addrs.ManagedResourceMode
986+
987+
schema := providerSchema.SchemaForResourceAddr(managedAddr)
981988
if schema.Body == nil {
982989
// Should be caught during validation, so we don't bother with a pretty error here
983-
diags = diags.Append(fmt.Errorf("provider does not support resource type for %q", n.Addr))
990+
diags = diags.Append(fmt.Errorf("provider does not support resource type for %q", managedAddr))
984991
return cty.DynamicVal, diags
985992
}
986993

0 commit comments

Comments
 (0)