Skip to content

Commit 53ff1ec

Browse files
committed
Updating ListResource to return diag.Diagnotics instead of error
1 parent 4606268 commit 53ff1ec

File tree

8 files changed

+52
-16
lines changed

8 files changed

+52
-16
lines changed

internal/fwserver/server_listresource.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package fwserver
55

66
import (
77
"context"
8-
"errors"
8+
"fmt"
99
"iter"
1010

1111
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -80,12 +80,17 @@ type ListResult struct {
8080
var NoListResults = func(func(ListResult) bool) {}
8181

8282
// ListResource implements the framework server ListResource RPC.
83-
func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream *ListResultsStream) error {
83+
func (s *Server) ListResource(ctx context.Context, fwReq *ListRequest, fwStream *ListResultsStream) diag.Diagnostics {
8484
listResource := fwReq.ListResource
8585

8686
if fwReq.Config == nil {
8787
fwStream.Results = NoListResults
88-
return errors.New("Invalid ListResource request: Config cannot be nil")
88+
return diag.Diagnostics{
89+
diag.NewErrorDiagnostic(
90+
"Invalid ListResource Request",
91+
fmt.Sprintf("Config cannot be nil"),
92+
),
93+
}
8994
}
9095

9196
req := list.ListRequest{

internal/fwserver/server_listresource_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestServerListResource(t *testing.T) {
7373
server *fwserver.Server
7474
request *fwserver.ListRequest
7575
expectedStreamEvents []fwserver.ListResult
76-
expectedError string
76+
expectedError diag.Diagnostics
7777
}{
7878
"success-with-zero-results": {
7979
server: &fwserver.Server{
@@ -179,7 +179,9 @@ func TestServerListResource(t *testing.T) {
179179
},
180180
},
181181
},
182-
expectedError: "Invalid ListResource request: Config cannot be nil",
182+
expectedError: diag.Diagnostics{
183+
diag.NewErrorDiagnostic("Invalid ListResource Request", "Config cannot be nil"),
184+
},
183185
expectedStreamEvents: []fwserver.ListResult{},
184186
},
185187
"error-on-nil-resource-identity": {
@@ -319,8 +321,8 @@ func TestServerListResource(t *testing.T) {
319321

320322
response := &fwserver.ListResultsStream{}
321323
err := testCase.server.ListResource(context.Background(), testCase.request, response)
322-
if err != nil && err.Error() != testCase.expectedError {
323-
t.Fatalf("unexpected error: %s", err)
324+
if diff := cmp.Diff(testCase.expectedError, err); diff != "" {
325+
t.Errorf("unexpected error difference: %s", diff)
324326
}
325327

326328
opts := cmp.Options{

internal/proto5server/server_listresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// ListRequestErrorDiagnostics returns a value suitable for
1717
// [ListResourceServerStream.Results]. It yields a single result that contains
1818
// the given error diagnostics.
19-
func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic) (*tfprotov5.ListResourceServerStream, error) {
19+
func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic) (*tfprotov5.ListResourceServerStream, diag.Diagnostics) {
2020
protoDiags := toproto5.Diagnostics(ctx, diags)
2121
return &tfprotov5.ListResourceServerStream{
2222
Results: func(push func(tfprotov5.ListResourceResult) bool) {
@@ -25,7 +25,7 @@ func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic)
2525
}, nil
2626
}
2727

28-
func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov5.ListResourceRequest) (*tfprotov5.ListResourceServerStream, error) {
28+
func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov5.ListResourceRequest) (*tfprotov5.ListResourceServerStream, diag.Diagnostics) {
2929
protoStream := &tfprotov5.ListResourceServerStream{Results: tfprotov5.NoListResults}
3030
allDiags := diag.Diagnostics{}
3131

internal/proto5server/server_listresource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestServerListResource(t *testing.T) {
173173
testCases := map[string]struct {
174174
server *Server
175175
request *tfprotov5.ListResourceRequest
176-
expectedError error
176+
expectedError diag.Diagnostics
177177
expectedDiagnostics diag.Diagnostics
178178
expectedResults []tfprotov5.ListResourceResult
179179
}{

internal/proto5server/server_validatelistresourceconfig.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,41 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto5Req *tfpr
1919
fwResp := &fwserver.ValidateListResourceConfigResponse{}
2020

2121
listResource, diags := s.FrameworkServer.ListResourceType(ctx, proto5Req.TypeName)
22+
23+
fwResp.Diagnostics.Append(diags...)
24+
2225
if diags.HasError() {
2326
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
2427
}
2528

2629
listResourceSchema, diags := s.FrameworkServer.ListResourceSchema(ctx, proto5Req.TypeName)
30+
31+
fwResp.Diagnostics.Append(diags...)
32+
2733
if diags.HasError() {
2834
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
2935
}
3036

3137
config, diags := fromproto5.Config(ctx, proto5Req.Config, listResourceSchema)
38+
39+
fwResp.Diagnostics.Append(diags...)
40+
3241
if diags.HasError() {
3342
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
3443
}
3544

3645
resourceSchema, diags := s.FrameworkServer.ResourceSchema(ctx, proto5Req.TypeName)
46+
47+
fwResp.Diagnostics.Append(diags...)
48+
3749
if diags.HasError() {
3850
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
3951
}
4052

4153
identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto5Req.TypeName)
54+
55+
fwResp.Diagnostics.Append(diags...)
56+
4257
if diags.HasError() {
4358
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
4459
}
@@ -53,7 +68,7 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto5Req *tfpr
5368

5469
err := s.FrameworkServer.ListResource(ctx, req, stream)
5570
if err != nil {
56-
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), err
71+
return toproto5.ValidateListResourceConfigResponse(ctx, fwResp), nil
5772
}
5873

5974
fwReq, diags := fromproto5.ValidateListResourceConfigRequest(ctx, proto5Req, listResource, listResourceSchema)

internal/proto6server/server_listresource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// ListRequestErrorDiagnostics returns a value suitable for
1717
// [ListResourceServerStream.Results]. It yields a single result that contains
1818
// the given error diagnostics.
19-
func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic) (*tfprotov6.ListResourceServerStream, error) {
19+
func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic) (*tfprotov6.ListResourceServerStream, diag.Diagnostics) {
2020
protoDiags := toproto6.Diagnostics(ctx, diags)
2121
return &tfprotov6.ListResourceServerStream{
2222
Results: func(push func(tfprotov6.ListResourceResult) bool) {
@@ -25,7 +25,7 @@ func ListRequestErrorDiagnostics(ctx context.Context, diags ...diag.Diagnostic)
2525
}, nil
2626
}
2727

28-
func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov6.ListResourceRequest) (*tfprotov6.ListResourceServerStream, error) {
28+
func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov6.ListResourceRequest) (*tfprotov6.ListResourceServerStream, diag.Diagnostics) {
2929
protoStream := &tfprotov6.ListResourceServerStream{Results: tfprotov6.NoListResults}
3030
allDiags := diag.Diagnostics{}
3131

internal/proto6server/server_listresource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestServerListResource(t *testing.T) {
193193
testCases := map[string]struct {
194194
server *Server
195195
request *tfprotov6.ListResourceRequest
196-
expectedError error
196+
expectedError diag.Diagnostics
197197
expectedDiagnostics diag.Diagnostics
198198
expectedResults []tfprotov6.ListResourceResult
199199
}{

internal/proto6server/server_validatelistresourceconfig.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package proto6server
55

66
import (
77
"context"
8-
98
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
109
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1110
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
@@ -19,26 +18,41 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto6Req *tfpr
1918
fwResp := &fwserver.ValidateListResourceConfigResponse{}
2019

2120
listResource, diags := s.FrameworkServer.ListResourceType(ctx, proto6Req.TypeName)
21+
22+
fwResp.Diagnostics.Append(diags...)
23+
2224
if diags.HasError() {
2325
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
2426
}
2527

2628
listResourceSchema, diags := s.FrameworkServer.ListResourceSchema(ctx, proto6Req.TypeName)
29+
30+
fwResp.Diagnostics.Append(diags...)
31+
2732
if diags.HasError() {
2833
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
2934
}
3035

3136
config, diags := fromproto6.Config(ctx, proto6Req.Config, listResourceSchema)
37+
38+
fwResp.Diagnostics.Append(diags...)
39+
3240
if diags.HasError() {
3341
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
3442
}
3543

3644
resourceSchema, diags := s.FrameworkServer.ResourceSchema(ctx, proto6Req.TypeName)
45+
46+
fwResp.Diagnostics.Append(diags...)
47+
3748
if diags.HasError() {
3849
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
3950
}
4051

4152
identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName)
53+
54+
fwResp.Diagnostics.Append(diags...)
55+
4256
if diags.HasError() {
4357
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
4458
}
@@ -53,7 +67,7 @@ func (s *Server) ValidateListResourceConfig(ctx context.Context, proto6Req *tfpr
5367

5468
err := s.FrameworkServer.ListResource(ctx, req, stream)
5569
if err != nil {
56-
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), err
70+
return toproto6.ValidateListResourceConfigResponse(ctx, fwResp), nil
5771
}
5872

5973
fwReq, diags := fromproto6.ValidateListResourceConfigRequest(ctx, proto6Req, listResource, listResourceSchema)

0 commit comments

Comments
 (0)