Skip to content

Commit 5107239

Browse files
committed
address review comments
1 parent ae78c70 commit 5107239

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

internal/fwserver/server_listresource.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,10 @@ func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream
161161
}
162162

163163
if stream.Results == nil {
164-
fwStream.Results = processListResults(req, list.NoListResults, diagsStream.Results)
164+
stream.Results = list.NoListResults
165165
}
166166

167-
if stream.Results != nil {
168-
fwStream.Results = processListResults(req, stream.Results, diagsStream.Results)
169-
}
167+
fwStream.Results = processListResults(req, stream.Results, diagsStream.Results)
170168
}
171169

172170
func processListResults(req list.ListRequest, streams ...iter.Seq[list.ListResult]) iter.Seq[ListResult] {

internal/fwserver/server_listresource_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ func TestServerListResource(t *testing.T) {
184184
expectedStreamEvents: []fwserver.ListResult{},
185185
expectedError: "config cannot be nil",
186186
},
187+
"zero-results-with-warning-diagnostic": {
188+
server: &fwserver.Server{
189+
Provider: &testprovider.Provider{},
190+
},
191+
request: &fwserver.ListRequest{
192+
Config: &tfsdk.Config{},
193+
ListResource: &testprovider.ListResourceWithConfigure{
194+
ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
195+
resp.Diagnostics.AddWarning("Test Warning", "This is a test warning diagnostic")
196+
},
197+
ListResource: &testprovider.ListResource{
198+
ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) {
199+
resp.Results = list.NoListResults
200+
},
201+
},
202+
},
203+
},
204+
expectedStreamEvents: []fwserver.ListResult{
205+
{
206+
Identity: nil,
207+
Resource: nil,
208+
DisplayName: "",
209+
Diagnostics: diag.Diagnostics{
210+
diag.NewWarningDiagnostic("Test Warning", "This is a test warning diagnostic"),
211+
},
212+
},
213+
},
214+
},
187215
"listresource-configure-data": {
188216
server: &fwserver.Server{
189217
ListResourceConfigureData: "test-provider-configure-value",

internal/fwserver/server_listresources.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func (s *Server) ListResourceFuncs(ctx context.Context) (map[string]func() list.
8686
}
8787

8888
schemasResp := list.SchemaResponse{}
89-
if listResourceWithSchemas, ok := listResource.(list.ListResourceWithProtoSchemas); ok {
90-
listResourceWithSchemas.Schemas(ctx, &schemasResp)
89+
if listResourceWithSchemas, ok := listResource.(list.ListResourceWithRawV5Schemas); ok {
90+
listResourceWithSchemas.RawV5Schemas(ctx, list.SchemaRequest{}, &schemasResp)
9191
}
9292

9393
resourceFuncs, _ := s.ResourceFuncs(ctx)
@@ -96,7 +96,7 @@ func (s *Server) ListResourceFuncs(ctx context.Context) (map[string]func() list.
9696
s.listResourceFuncsDiags.AddError(
9797
"ListResource Type Defined without a Matching Managed Resource Type",
9898
fmt.Sprintf("The %s ListResource type name was returned, but no matching managed Resource type was defined. ", typeName)+
99-
"If the matching managed Resource type is a legacy resource, ProtoV5Schema and ProtoV5IdentitySchema must be specified in the Metadata method. "+
99+
"If the matching managed Resource type is a legacy resource, ProtoV5Schema and ProtoV5IdentitySchema must be specified in the RawV5Schemas method. "+
100100
"This is always an issue with the provider and should be reported to the provider developers.",
101101
)
102102
continue

internal/proto5server/server_listresource.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,28 @@ func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov5.ListResou
5656
}
5757

5858
schemaResp := list.SchemaResponse{}
59-
if listResourceWithProtoSchemas, ok := listResource.(list.ListResourceWithProtoSchemas); ok {
60-
listResourceWithProtoSchemas.Schemas(ctx, &schemaResp)
59+
if listResourceWithProtoSchemas, ok := listResource.(list.ListResourceWithRawV5Schemas); ok {
60+
listResourceWithProtoSchemas.RawV5Schemas(ctx, list.SchemaRequest{}, &schemaResp)
6161
}
6262

6363
// There's validation in ListResources that ensures both are set if either is provided so it should be sufficient to only nil check Identity
6464
if schemaResp.ProtoV5IdentitySchema != nil {
65-
req.ResourceSchema, _ = fromproto5.ResourceSchema(ctx, schemaResp.ProtoV5Schema())
66-
req.ResourceIdentitySchema, _ = fromproto5.IdentitySchema(ctx, schemaResp.ProtoV5IdentitySchema())
65+
var err error
66+
67+
req.ResourceSchema, err = fromproto5.ResourceSchema(ctx, schemaResp.ProtoV5Schema)
68+
if err != nil {
69+
diags.AddError("Converting Resource Schema", err.Error())
70+
allDiags.Append(diags...)
71+
return ListRequestErrorDiagnostics(ctx, allDiags...)
72+
}
73+
74+
req.ResourceIdentitySchema, err = fromproto5.IdentitySchema(ctx, schemaResp.ProtoV5IdentitySchema)
75+
if err != nil {
76+
diags.AddError("Converting Resource Identity Schema", err.Error())
77+
allDiags.Append(diags...)
78+
return ListRequestErrorDiagnostics(ctx, allDiags...)
79+
}
80+
6781
} else {
6882
req.ResourceSchema, diags = s.FrameworkServer.ResourceSchema(ctx, protoReq.TypeName)
6983
allDiags.Append(diags...)

list/list_resource.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ type ListResource interface {
4242
List(context.Context, ListRequest, *ListResultsStream)
4343
}
4444

45-
// ListResourceWithProtoSchemas is an interface type that extends ListResource to include a method
45+
// ListResourceWithRawV5Schemas is an interface type that extends ListResource to include a method
4646
// which allows provider developers to supply the ProtoV5 representations of resource and resource identity
4747
// schemas. This is necessary if list functionality is being used with a legacy resource.
48-
type ListResourceWithProtoSchemas interface {
48+
type ListResourceWithRawV5Schemas interface {
4949
ListResource
5050

51-
// Schemas is called to provide the ProtoV5 representations of the resource and resource identity schemas.
52-
Schemas(context.Context, *SchemaResponse)
51+
// RawV5Schemas is called to provide the ProtoV5 representations of the resource and resource identity schemas.
52+
RawV5Schemas(context.Context, SchemaRequest, *SchemaResponse)
5353
}
5454

5555
// ListResourceWithConfigure is an interface type that extends ListResource to include a method
@@ -193,18 +193,23 @@ type ListResult struct {
193193
Diagnostics diag.Diagnostics
194194
}
195195

196+
// SchemaRequest represents a request for the ListResource to return the
197+
// ProtoV5 schemas. An instance of this request struct is supplied as an argument
198+
// to the ListResource type RawV5Schemas method.
199+
type SchemaRequest struct{}
200+
196201
// SchemaResponse represents a response that is populated by the Schemas method
197202
// and is used to pass along the ProtoV5 representations of the resource and resource identity schemas.
198203
type SchemaResponse struct {
199204
// ProtoV5IdentitySchema is the ProtoV5 representation of the resource identity
200205
// schema. This should only be supplied if framework functionality is being used
201206
// with a legacy resource. Currently, this only applies to list.
202-
ProtoV5IdentitySchema func() *tfprotov5.ResourceIdentitySchema
207+
ProtoV5IdentitySchema *tfprotov5.ResourceIdentitySchema
203208

204209
// ProtoV5Schema is the ProtoV5 representation of the resource schema
205210
// This should only be supplied if framework functionality is being used
206211
// with a legacy resource. Currently, this only applies to list.
207-
ProtoV5Schema func() *tfprotov5.Schema
212+
ProtoV5Schema *tfprotov5.Schema
208213
}
209214

210215
// ValidateConfigRequest represents a request to validate the configuration of

0 commit comments

Comments
 (0)