Skip to content

Commit 7633960

Browse files
authored
list: minor fixes in ListResource and ValidateListResourceConfig (#1192)
* call Configure in fwserver ListResource RPC * remove ListResource call in ValidateListResourceConfig * capture diagnostics produced by the Configure call in a diagsStream * remove ListResource call in ValidateListResourceConfig for proto6 * explanatory comment
1 parent cca5c5a commit 7633960

File tree

3 files changed

+59
-74
lines changed

3 files changed

+59
-74
lines changed

internal/fwserver/server_listresource.go

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ package fwserver
55

66
import (
77
"context"
8-
"github.com/hashicorp/terraform-plugin-go/tftypes"
98
"iter"
109

1110
"github.com/hashicorp/terraform-plugin-framework/diag"
1211
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1312
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
1413
"github.com/hashicorp/terraform-plugin-framework/list"
14+
"github.com/hashicorp/terraform-plugin-framework/resource"
1515
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
16+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1617
)
1718

1819
// ListRequest is the framework server request for the ListResource RPC.
@@ -99,6 +100,47 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
99100
}
100101
}
101102

103+
// TODO verdict is still out on how to handle diagnostics that pertain to the List call as a whole and not individual list results
104+
diagsStream := &list.ListResultsStream{}
105+
106+
if listResourceWithConfigure, ok := listResource.(list.ListResourceWithConfigure); ok {
107+
logging.FrameworkTrace(ctx, "ListResource implements ListResourceWithConfigure")
108+
109+
configureReq := resource.ConfigureRequest{
110+
ProviderData: s.ListResourceConfigureData,
111+
}
112+
113+
configureResp := resource.ConfigureResponse{}
114+
115+
logging.FrameworkTrace(ctx, "Called provider defined ListResource Configure")
116+
listResourceWithConfigure.Configure(ctx, configureReq, &configureResp)
117+
logging.FrameworkTrace(ctx, "Called provider defined ListResource Configure")
118+
119+
if len(configureResp.Diagnostics) > 0 {
120+
diagsResp := list.ListResult{}
121+
122+
diagsResp.Diagnostics.Append(configureResp.Diagnostics...)
123+
124+
// Captures any diags from the Configure call
125+
diagsStream.Results = func(push func(list.ListResult) bool) {
126+
if !push(diagsResp) {
127+
return
128+
}
129+
}
130+
131+
if diagsResp.Diagnostics.HasError() {
132+
fwStream.Results = func(push func(ListResult) bool) {
133+
for result := range diagsStream.Results {
134+
if !push(ListResult(result)) {
135+
return
136+
}
137+
}
138+
}
139+
return
140+
}
141+
}
142+
}
143+
102144
req := list.ListRequest{
103145
Config: *fwReq.Config,
104146
IncludeResource: fwReq.IncludeResource,
@@ -118,14 +160,20 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
118160
stream.Results = list.NoListResults
119161
}
120162

121-
fwStream.Results = processListResults(req, stream.Results)
163+
if diagsStream.Results == nil {
164+
diagsStream.Results = list.NoListResults
165+
}
166+
167+
fwStream.Results = processListResults(req, stream.Results, diagsStream.Results)
122168
}
123169

124-
func processListResults(req list.ListRequest, stream iter.Seq[list.ListResult]) iter.Seq[ListResult] {
170+
func processListResults(req list.ListRequest, streams ...iter.Seq[list.ListResult]) iter.Seq[ListResult] {
125171
return func(push func(ListResult) bool) {
126-
for result := range stream {
127-
if !push(processListResult(req, result)) {
128-
return
172+
for _, stream := range streams {
173+
for result := range stream {
174+
if !push(processListResult(req, result)) {
175+
return
176+
}
129177
}
130178
}
131179
}
@@ -138,6 +186,11 @@ func processListResult(req list.ListRequest, result list.ListResult) ListResult
138186
return ListResult(result)
139187
}
140188

189+
// Allow any non-error diags to pass through
190+
if len(result.Diagnostics) > 0 && result.DisplayName == "" && result.Identity == nil && result.Resource == nil {
191+
return ListResult(result)
192+
}
193+
141194
if result.Identity == nil || result.Identity.Raw.IsNull() {
142195
return ListResultError(
143196
"Incomplete List Result",

internal/proto5server/server_validatelistresourceconfig.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,6 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto5Req *tfpr
3434
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
3535
}
3636

37-
config, diags := fromproto5.Config(ctx, proto5Req.Config, listResourceSchema)
38-
39-
fwResp.Diagnostics.Append(diags...)
40-
41-
if diags.HasError() {
42-
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
43-
}
44-
45-
resourceSchema, diags := s.FrameworkServer.ResourceSchema(ctx, proto5Req.TypeName)
46-
47-
fwResp.Diagnostics.Append(diags...)
48-
49-
if diags.HasError() {
50-
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
51-
}
52-
53-
identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName)
54-
55-
fwResp.Diagnostics.Append(diags...)
56-
57-
if diags.HasError() {
58-
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
59-
}
60-
61-
req := &fwserver.ListRequest{
62-
Config: config,
63-
ListResource: listResource,
64-
ResourceSchema: resourceSchema,
65-
ResourceIdentitySchema: identitySchema,
66-
}
67-
stream := &fwserver.ListResultsStream{}
68-
69-
s.FrameworkServer.ListResource(ctx, req, stream)
70-
7137
fwReq, diags := fromproto5.ValidateListResourceConfigRequest(ctx, proto5Req, listResource, listResourceSchema)
7238

7339
fwResp.Diagnostics.Append(diags...)

internal/proto6server/server_validatelistresourceconfig.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,6 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto6Req *tfpr
3333
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
3434
}
3535

36-
config, diags := fromproto6.Config(ctx, proto6Req.Config, listResourceSchema)
37-
38-
fwResp.Diagnostics.Append(diags...)
39-
40-
if diags.HasError() {
41-
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
42-
}
43-
44-
resourceSchema, diags := s.FrameworkServer.ResourceSchema(ctx, proto6Req.TypeName)
45-
46-
fwResp.Diagnostics.Append(diags...)
47-
48-
if diags.HasError() {
49-
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
50-
}
51-
52-
identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName)
53-
54-
fwResp.Diagnostics.Append(diags...)
55-
56-
if diags.HasError() {
57-
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
58-
}
59-
60-
req := &fwserver.ListRequest{
61-
Config: config,
62-
ListResource: listResource,
63-
ResourceSchema: resourceSchema,
64-
ResourceIdentitySchema: identitySchema,
65-
}
66-
stream := &fwserver.ListResultsStream{}
67-
68-
s.FrameworkServer.ListResource(ctx, req, stream)
69-
7036
fwReq, diags := fromproto6.ValidateListResourceConfigRequest(ctx, proto6Req, listResource, listResourceSchema)
7137

7238
fwResp.Diagnostics.Append(diags...)

0 commit comments

Comments
 (0)