Skip to content

Commit b2c4a8f

Browse files
authored
Merge pull request #37867 from hashicorp/dbanck/backport/list-identity-fix
backport: Fix list resource diagnostics check (#37863)
2 parents 314372b + 72e2548 commit b2c4a8f

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'query: improve error handling for missing identity schemas'
3+
time: 2025-11-04T12:23:22.096828+01:00
4+
custom:
5+
Issue: "37863"

internal/plugin/grpc_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13211321

13221322
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
13231323
if !ok || resourceSchema.Identity == nil {
1324-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
1324+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Identity schema not found for resource type %s; this is a bug in the provider - please report it there", r.TypeName))
13251325
return resp
13261326
}
13271327

internal/plugin6/grpc_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13171317

13181318
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
13191319
if !ok || resourceSchema.Identity == nil {
1320-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
1320+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Identity schema not found for resource type %s; this is a bug in the provider - please report it there", r.TypeName))
13211321
return resp
13221322
}
13231323

internal/terraform/context_plan_query_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,10 @@ func TestContext2Plan_queryList(t *testing.T) {
683683
if tc.transformSchema != nil {
684684
tc.transformSchema(provider.GetProviderSchemaResponse)
685685
}
686-
var requestConfigs = make(map[string]cty.Value)
687686
provider.ListResourceFn = func(request providers.ListResourceRequest) providers.ListResourceResponse {
688687
if request.Config.IsNull() || request.Config.GetAttr("config").IsNull() {
689688
t.Fatalf("config should never be null, got null for %s", request.TypeName)
690689
}
691-
requestConfigs[request.TypeName] = request.Config
692690
return listResourceFn(request)
693691
}
694692

@@ -948,6 +946,55 @@ func TestContext2Plan_queryListArgs(t *testing.T) {
948946
}
949947
}
950948

949+
func TestContext2Plan_queryListDiags(t *testing.T) {
950+
configFiles := map[string]string{}
951+
configFiles["main.tf"] = `
952+
terraform {
953+
required_providers {
954+
test = {
955+
source = "hashicorp/test"
956+
version = "1.0.0"
957+
}
958+
}
959+
}`
960+
configFiles["main.tfquery.hcl"] = `
961+
list "test_resource" "test1" {
962+
provider = test
963+
}`
964+
mod := testModuleInline(t, configFiles, configs.MatchQueryFiles())
965+
966+
providerAddr := addrs.NewDefaultProvider("test")
967+
provider := testProvider("test")
968+
provider.ConfigureProvider(providers.ConfigureProviderRequest{})
969+
provider.GetProviderSchemaResponse = getListProviderSchemaResp()
970+
provider.ListResourceFn = func(request providers.ListResourceRequest) providers.ListResourceResponse {
971+
if request.Config.IsNull() || request.Config.GetAttr("config").IsNull() {
972+
t.Fatalf("config should never be null, got null for %s", request.TypeName)
973+
}
974+
975+
resp := providers.ListResourceResponse{}
976+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Identity schema not found for resource type %s; this is a bug in the provider - please report it there", request.TypeName))
977+
978+
return resp
979+
}
980+
981+
ctx, diags := NewContext(&ContextOpts{
982+
Providers: map[addrs.Provider]providers.Factory{
983+
providerAddr: testProviderFuncFixed(provider),
984+
},
985+
})
986+
tfdiags.AssertNoDiagnostics(t, diags)
987+
988+
_, diags = ctx.Plan(mod, states.NewState(), &PlanOpts{
989+
Mode: plans.NormalMode,
990+
SetVariables: testInputValuesUnset(mod.Module.Variables),
991+
Query: true,
992+
})
993+
if len(diags) != 1 {
994+
t.Fatalf("expected 1 diagnostics, got %d \n - diags: %s", len(diags), diags)
995+
}
996+
}
997+
951998
// getListProviderSchemaResp returns a mock provider schema response for testing list resources.
952999
// THe schema returned here is a mock of what the internal protobuf layer would return
9531000
// for a provider that supports list resources.

internal/terraform/node_resource_plan_instance_query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func (n *NodePlannableResourceInstance) listResourceExecute(ctx EvalContext) (di
113113
Limit: limit,
114114
IncludeResourceObject: includeRsc,
115115
})
116+
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
117+
if diags.HasErrors() {
118+
return diags
119+
}
116120
results := plans.QueryResults{
117121
Value: resp.Result,
118122
}
@@ -132,10 +136,6 @@ func (n *NodePlannableResourceInstance) listResourceExecute(ctx EvalContext) (di
132136
ctx.Hook(func(h Hook) (HookAction, error) {
133137
return h.PostListQuery(rId, results, identityVersion)
134138
})
135-
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
136-
if diags.HasErrors() {
137-
return diags
138-
}
139139

140140
query := &plans.QueryInstance{
141141
Addr: n.Addr,

0 commit comments

Comments
 (0)