From 82889ae9873eda7104236643481925f3e65d06a0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 1 Oct 2025 15:03:50 -0400 Subject: [PATCH] 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. --- internal/terraform/context_plan_query_test.go | 9 ++++++++- internal/terraform/node_resource_plan_instance.go | 11 +++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/internal/terraform/context_plan_query_test.go b/internal/terraform/context_plan_query_test.go index 9c2b9e1469..75b016003f 100644 --- a/internal/terraform/context_plan_query_test.go +++ b/internal/terraform/context_plan_query_test.go @@ -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": { diff --git a/internal/terraform/node_resource_plan_instance.go b/internal/terraform/node_resource_plan_instance.go index e4fd8e059d..4dec19172f 100644 --- a/internal/terraform/node_resource_plan_instance.go +++ b/internal/terraform/node_resource_plan_instance.go @@ -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 }