Skip to content

Commit 8ac8872

Browse files
committed
capture diagnostics produced by the Configure call in a diagsStream
1 parent 95576c8 commit 8ac8872

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

internal/fwserver/server_listresource.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
100100
}
101101
}
102102

103-
resp := ListResult{}
103+
diagsStream := &list.ListResultsStream{}
104104

105105
if listResourceWithConfigure, ok := listResource.(list.ListResourceWithConfigure); ok {
106106
logging.FrameworkTrace(ctx, "ListResource implements ListResourceWithConfigure")
@@ -115,12 +115,27 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
115115
listResourceWithConfigure.Configure(ctx, configureReq, &configureResp)
116116
logging.FrameworkTrace(ctx, "Called provider defined ListResource Configure")
117117

118-
resp.Diagnostics.Append(configureResp.Diagnostics...)
118+
diagsResp := list.ListResult{}
119119

120-
if resp.Diagnostics.HasError() {
121-
return
120+
diagsResp.Diagnostics.Append(configureResp.Diagnostics...)
121+
122+
// Captures any diags from the Configure call
123+
diagsStream.Results = func(push func(list.ListResult) bool) {
124+
if !push(diagsResp) {
125+
return
126+
}
122127
}
123128

129+
if diagsResp.Diagnostics.HasError() {
130+
fwStream.Results = func(push func(ListResult) bool) {
131+
for result := range diagsStream.Results {
132+
if !push(ListResult(result)) {
133+
return
134+
}
135+
}
136+
}
137+
return
138+
}
124139
}
125140

126141
req := list.ListRequest{
@@ -142,17 +157,16 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
142157
stream.Results = list.NoListResults
143158
}
144159

145-
// How should we handle the diags produced by Configure called on line 115? Appending them to an empty stream
146-
// might be misleading, not to mention will error below because Identity will be nil
147-
148-
fwStream.Results = processListResults(req, stream.Results)
160+
fwStream.Results = processListResults(req, stream.Results, diagsStream.Results)
149161
}
150162

151-
func processListResults(req list.ListRequest, stream iter.Seq[list.ListResult]) iter.Seq[ListResult] {
163+
func processListResults(req list.ListRequest, streams ...iter.Seq[list.ListResult]) iter.Seq[ListResult] {
152164
return func(push func(ListResult) bool) {
153-
for result := range stream {
154-
if !push(processListResult(req, result)) {
155-
return
165+
for _, stream := range streams {
166+
for result := range stream {
167+
if !push(processListResult(req, result)) {
168+
return
169+
}
156170
}
157171
}
158172
}
@@ -165,6 +179,11 @@ func processListResult(req list.ListRequest, result list.ListResult) ListResult
165179
return ListResult(result)
166180
}
167181

182+
// Allow any non-error diags to pass through
183+
if len(result.Diagnostics) > 0 && result.DisplayName == "" && result.Identity == nil && result.Resource == nil {
184+
return ListResult(result)
185+
}
186+
168187
if result.Identity == nil || result.Identity.Raw.IsNull() {
169188
return ListResultError(
170189
"Incomplete List Result",

0 commit comments

Comments
 (0)