Skip to content

Commit 82889ae

Browse files
committed
get correct schema when generating query config
During the latest refactor, the wrong schema was fetched when generating configuration from a list resource. Having the list resource schema instead of the managed resource schema prevented the config generation from matching the computed attributes within the state, causing them to show up in the configuration. It turns out the tests for this didn't have computed attributes, so the accidental swap during refactoring didn't cause the tests to fail. A small addition to the tests adding an `id` attribute picks the regression.
1 parent 2274026 commit 82889ae

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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)