Fix list resource diagnostics check (#37863)

* Fix list resource diagnostics check

* Improve error message on missing identity schema

* Add test case for list response diagnostics

* Add changelog
pull/37864/head^2
Daniel Banck 6 months ago committed by GitHub
parent f5a28cfa8b
commit 6b038754f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'query: improve error handling for missing identity schemas'
time: 2025-11-04T12:23:22.096828+01:00
custom:
Issue: "37863"

@ -1321,7 +1321,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
if !ok || resourceSchema.Identity == nil {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
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))
return resp
}

@ -1328,7 +1328,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
if !ok || resourceSchema.Identity == nil {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
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))
return resp
}

@ -683,12 +683,10 @@ func TestContext2Plan_queryList(t *testing.T) {
if tc.transformSchema != nil {
tc.transformSchema(provider.GetProviderSchemaResponse)
}
var requestConfigs = make(map[string]cty.Value)
provider.ListResourceFn = func(request providers.ListResourceRequest) providers.ListResourceResponse {
if request.Config.IsNull() || request.Config.GetAttr("config").IsNull() {
t.Fatalf("config should never be null, got null for %s", request.TypeName)
}
requestConfigs[request.TypeName] = request.Config
return listResourceFn(request)
}
@ -948,6 +946,55 @@ func TestContext2Plan_queryListArgs(t *testing.T) {
}
}
func TestContext2Plan_queryListDiags(t *testing.T) {
configFiles := map[string]string{}
configFiles["main.tf"] = `
terraform {
required_providers {
test = {
source = "hashicorp/test"
version = "1.0.0"
}
}
}`
configFiles["main.tfquery.hcl"] = `
list "test_resource" "test1" {
provider = test
}`
mod := testModuleInline(t, configFiles, configs.MatchQueryFiles())
providerAddr := addrs.NewDefaultProvider("test")
provider := testProvider("test")
provider.ConfigureProvider(providers.ConfigureProviderRequest{})
provider.GetProviderSchemaResponse = getListProviderSchemaResp()
provider.ListResourceFn = func(request providers.ListResourceRequest) providers.ListResourceResponse {
if request.Config.IsNull() || request.Config.GetAttr("config").IsNull() {
t.Fatalf("config should never be null, got null for %s", request.TypeName)
}
resp := providers.ListResourceResponse{}
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))
return resp
}
ctx, diags := NewContext(&ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
providerAddr: testProviderFuncFixed(provider),
},
})
tfdiags.AssertNoDiagnostics(t, diags)
_, diags = ctx.Plan(mod, states.NewState(), &PlanOpts{
Mode: plans.NormalMode,
SetVariables: testInputValuesUnset(mod.Module.Variables),
Query: true,
})
if len(diags) != 1 {
t.Fatalf("expected 1 diagnostics, got %d \n - diags: %s", len(diags), diags)
}
}
// getListProviderSchemaResp returns a mock provider schema response for testing list resources.
// THe schema returned here is a mock of what the internal protobuf layer would return
// for a provider that supports list resources.

@ -113,6 +113,10 @@ func (n *NodePlannableResourceInstance) listResourceExecute(ctx EvalContext) (di
Limit: limit,
IncludeResourceObject: includeRsc,
})
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
if diags.HasErrors() {
return diags
}
results := plans.QueryResults{
Value: resp.Result,
}
@ -132,10 +136,6 @@ func (n *NodePlannableResourceInstance) listResourceExecute(ctx EvalContext) (di
ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostListQuery(rId, results, identityVersion)
})
diags = diags.Append(resp.Diagnostics.InConfigBody(config.Config, n.Addr.String()))
if diags.HasErrors() {
return diags
}
query := &plans.QueryInstance{
Addr: n.Addr,

Loading…
Cancel
Save