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.
pull/37713/head
James Bardin 8 months ago
parent 2274026c68
commit 82889ae987

@ -30,7 +30,10 @@ func TestContext2Plan_queryList(t *testing.T) {
instanceTypes := []string{"ami-123456", "ami-654321", "ami-789012"}
madeUp := []cty.Value{}
for i := range len(instanceTypes) {
madeUp = append(madeUp, cty.ObjectVal(map[string]cty.Value{"instance_type": cty.StringVal(instanceTypes[i])}))
madeUp = append(madeUp, cty.ObjectVal(map[string]cty.Value{
"instance_type": cty.StringVal(instanceTypes[i]),
"id": cty.StringVal(fmt.Sprint(i)),
}))
}
ids := []cty.Value{}
@ -1004,6 +1007,10 @@ func getListProviderSchemaResp() *providers.GetProviderSchemaResponse {
Computed: true,
Optional: true,
},
"id": {
Type: cty.String,
Computed: true,
},
},
},
"test_child_resource": {

@ -977,10 +977,17 @@ func (n *NodePlannableResourceInstance) generateResourceConfig(ctx EvalContext,
if diags.HasErrors() {
return cty.DynamicVal, diags
}
schema := providerSchema.SchemaForResourceAddr(n.Addr.Resource.Resource)
// the calling node may be a list resource, in which case we still need to
// lookup the schema for the corresponding managed resource for generating
// configuration.
managedAddr := n.Addr.Resource.Resource
managedAddr.Mode = addrs.ManagedResourceMode
schema := providerSchema.SchemaForResourceAddr(managedAddr)
if schema.Body == nil {
// Should be caught during validation, so we don't bother with a pretty error here
diags = diags.Append(fmt.Errorf("provider does not support resource type for %q", n.Addr))
diags = diags.Append(fmt.Errorf("provider does not support resource type for %q", managedAddr))
return cty.DynamicVal, diags
}

Loading…
Cancel
Save