Skip to content

Commit 49f3d27

Browse files
committed
fwserver: validate each ListResult
1 parent 877b2b8 commit 49f3d27

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

internal/fwserver/server_listresource.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,47 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
8585
logging.FrameworkTrace(ctx, "Called provider defined ListResource")
8686

8787
if stream.Results == nil {
88-
// If the provider returned a nil results stream, we treat it as an empty stream.
88+
// If the provider returned a nil results stream, we return an empty stream.
8989
stream.Results = func(func(list.ListResult) bool) {}
9090
}
9191

92-
fwStream.Results = listResourceEventStreamAdapter(stream.Results)
92+
fwStream.Results = processListResults(req, stream.Results)
9393
}
9494

95-
func listResourceEventStreamAdapter(stream iter.Seq[list.ListResult]) iter.Seq[ListResult] {
96-
// TODO: is this any more efficient than a for-range?
95+
func processListResults(req list.ListRequest, stream iter.Seq[list.ListResult]) iter.Seq[ListResult] {
9796
return func(yieldFw func(ListResult) bool) {
98-
yield := func(event list.ListResult) bool {
99-
return yieldFw(ListResult(event))
97+
for result := range stream {
98+
var fwResult ListResult
99+
100+
diags := validateListResult(req, result)
101+
if diags.HasError() {
102+
fwResult = ListResult{Diagnostics: diags}
103+
} else {
104+
fwResult = ListResult(result)
105+
}
106+
if !yieldFw(fwResult) {
107+
return
108+
}
100109
}
101-
stream(yield)
102110
}
103111
}
112+
113+
func validateListResult(req list.ListRequest, result list.ListResult) diag.Diagnostics {
114+
var diags diag.Diagnostics
115+
116+
if result.Identity == nil {
117+
diags.AddError(
118+
"Incomplete List Result",
119+
"ListResult.Identity is nil.",
120+
)
121+
}
122+
123+
if req.IncludeResource && result.Resource == nil {
124+
diags.AddWarning(
125+
"Incomplete List Result",
126+
"ListRequest.IncludeResource is true and ListResult.Resource is nil.",
127+
)
128+
}
129+
130+
return diags
131+
}

0 commit comments

Comments
 (0)