Skip to content

Commit 2cb66a6

Browse files
committed
refactor
1 parent cc42117 commit 2cb66a6

File tree

5 files changed

+96
-84
lines changed

5 files changed

+96
-84
lines changed

internal/proto6server/serve.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
var _ tfprotov6.ProviderServer = &Server{}
15+
var _ tfprotov6.ProviderServerWithListResource = &Server{}
1516

1617
// Provider server implementation.
1718
type Server struct {

internal/proto6server/server_listresource.go

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,28 @@ package proto6server
33
import (
44
"context"
55

6-
"github.com/hashicorp/terraform-plugin-framework/diag"
76
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
87
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
98
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
109
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1110
)
1211

13-
type ResourceSchemaNotFoundError struct {
14-
TypeName string
15-
}
16-
17-
func (e *ResourceSchemaNotFoundError) Error() string {
18-
return "resource schema not found for type: " + e.TypeName
19-
}
20-
21-
func (e *ResourceSchemaNotFoundError) Is(err error) bool {
22-
compatibleErr, ok := err.(*ResourceSchemaNotFoundError)
23-
if !ok {
24-
return false
25-
}
26-
27-
return e.TypeName == compatibleErr.TypeName
28-
}
29-
30-
type ResourceIdentitySchemaNotFoundError struct {
31-
TypeName string
32-
}
33-
34-
func (e *ResourceIdentitySchemaNotFoundError) Error() string {
35-
return "resource identity schema not found for type: " + e.TypeName
36-
}
37-
38-
func (e *ResourceIdentitySchemaNotFoundError) Is(err error) bool {
39-
compatibleErr, ok := err.(*ResourceIdentitySchemaNotFoundError)
40-
if !ok {
41-
return false
42-
}
43-
44-
return e.TypeName == compatibleErr.TypeName
45-
}
46-
47-
type ListResourceSchemaNotFoundError struct {
48-
TypeName string
49-
}
50-
51-
func (e *ListResourceSchemaNotFoundError) Error() string {
52-
return "list resource schema not found for type: " + e.TypeName
53-
}
12+
func (s *Server) ListResource(ctx context.Context, proto6Req *tfprotov6.ListResourceRequest) (*tfprotov6.ListResourceServerStream, error) {
13+
// proto6Stream := &tfprotov6.ListResourceServerStream{Results: tfprotov6.NoListResults}
14+
proto6Stream := &tfprotov6.ListResourceServerStream{Results: func(func(tfprotov6.ListResourceResult) bool) {}}
5415

55-
func (e *ListResourceSchemaNotFoundError) Is(err error) bool {
56-
compatibleErr, ok := err.(*ListResourceSchemaNotFoundError)
57-
if !ok {
58-
return false
16+
// TODO: extract fromproto6.ListResourceConfig
17+
listResourceSchema, diags := s.FrameworkServer.ListResourceSchema(ctx, proto6Req.TypeName)
18+
if diags.HasError() {
19+
return proto6Stream, &ListResourceSchemaNotFoundError{TypeName: proto6Req.TypeName}
5920
}
6021

61-
return e.TypeName == compatibleErr.TypeName
62-
}
63-
64-
type ListResourceConfigError struct {
65-
TypeName string
66-
Diagnostics diag.Diagnostics
67-
}
68-
69-
func (e *ListResourceConfigError) Error() string {
70-
return "list resource config error for type: " + e.TypeName // + ": " + e.Diagnostics.Error()
71-
}
72-
73-
func (e *ListResourceConfigError) Is(err error) bool {
74-
compatibleErr, ok := err.(*ListResourceConfigError)
75-
if !ok {
76-
return false
22+
config, diags := fromproto6.Config(ctx, proto6Req.Config, listResourceSchema)
23+
if diags.HasError() {
24+
return proto6Stream, &ListResourceConfigError{TypeName: proto6Req.TypeName, Diagnostics: diags}
7725
}
7826

79-
return e.TypeName != compatibleErr.TypeName
80-
}
81-
82-
func (s *Server) ListResource(ctx context.Context, proto6Req *tfprotov6.ListResourceRequest) (*tfprotov6.ListResourceServerStream, error) {
83-
// proto6Stream := &tfprotov6.ListResourceServerStream{Results: tfprotov6.NoListResults}
84-
proto6Stream := &tfprotov6.ListResourceServerStream{Results: func(func(tfprotov6.ListResourceResult) bool) {}}
85-
27+
// TODO: extract fromproto6.ListRequest
8628
listResource, err := s.FrameworkServer.ListResourceOrError(ctx, proto6Req.TypeName)
8729
if err != nil {
8830
return proto6Stream, err
@@ -98,16 +40,6 @@ func (s *Server) ListResource(ctx context.Context, proto6Req *tfprotov6.ListReso
9840
return proto6Stream, &ResourceIdentitySchemaNotFoundError{TypeName: proto6Req.TypeName}
9941
}
10042

101-
listResourceSchema, diags := s.FrameworkServer.ListResourceSchema(ctx, proto6Req.TypeName)
102-
if diags.HasError() {
103-
return proto6Stream, &ListResourceSchemaNotFoundError{TypeName: proto6Req.TypeName}
104-
}
105-
106-
config, diags := fromproto6.Config(ctx, proto6Req.Config, listResourceSchema)
107-
if diags.HasError() {
108-
return proto6Stream, &ListResourceConfigError{TypeName: proto6Req.TypeName, Diagnostics: diags}
109-
}
110-
11143
req := &fwserver.ListRequest{
11244
Config: *config,
11345
ListResource: listResource,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package proto6server
2+
3+
import "github.com/hashicorp/terraform-plugin-framework/diag"
4+
5+
type ResourceSchemaNotFoundError struct {
6+
TypeName string
7+
}
8+
9+
func (e *ResourceSchemaNotFoundError) Error() string {
10+
return "resource schema not found for type: " + e.TypeName
11+
}
12+
13+
func (e *ResourceSchemaNotFoundError) Is(err error) bool {
14+
compatibleErr, ok := err.(*ResourceSchemaNotFoundError)
15+
if !ok {
16+
return false
17+
}
18+
19+
return e.TypeName == compatibleErr.TypeName
20+
}
21+
22+
type ResourceIdentitySchemaNotFoundError struct {
23+
TypeName string
24+
}
25+
26+
func (e *ResourceIdentitySchemaNotFoundError) Error() string {
27+
return "resource identity schema not found for type: " + e.TypeName
28+
}
29+
30+
func (e *ResourceIdentitySchemaNotFoundError) Is(err error) bool {
31+
compatibleErr, ok := err.(*ResourceIdentitySchemaNotFoundError)
32+
if !ok {
33+
return false
34+
}
35+
36+
return e.TypeName == compatibleErr.TypeName
37+
}
38+
39+
type ListResourceSchemaNotFoundError struct {
40+
TypeName string
41+
}
42+
43+
func (e *ListResourceSchemaNotFoundError) Error() string {
44+
return "list resource schema not found for type: " + e.TypeName
45+
}
46+
47+
func (e *ListResourceSchemaNotFoundError) Is(err error) bool {
48+
compatibleErr, ok := err.(*ListResourceSchemaNotFoundError)
49+
if !ok {
50+
return false
51+
}
52+
53+
return e.TypeName == compatibleErr.TypeName
54+
}
55+
56+
type ListResourceConfigError struct {
57+
TypeName string
58+
Diagnostics diag.Diagnostics
59+
}
60+
61+
func (e *ListResourceConfigError) Error() string {
62+
return "list resource config error for type: " + e.TypeName // + ": " + e.Diagnostics.Error()
63+
}
64+
65+
func (e *ListResourceConfigError) Is(err error) bool {
66+
compatibleErr, ok := err.(*ListResourceConfigError)
67+
if !ok {
68+
return false
69+
}
70+
71+
return e.TypeName != compatibleErr.TypeName
72+
}

internal/proto6server/server_listresource_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ func TestServerListResource(t *testing.T) {
103103
continue
104104
}
105105

106-
result := req.ToResult(
107-
ctx,
108-
resources[name].ThingResourceIdentity,
109-
resources[name],
110-
name)
106+
result := req.ToResult(ctx, resources[name].ThingResourceIdentity, resources[name], name)
111107

112108
results = append(results, result)
113109
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package proto6server
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
7+
)
8+
9+
func (s *Server) ValidateListResourceConfig(ctx context.Context, request *tfprotov6.ValidateListResourceConfigRequest) (*tfprotov6.ValidateListResourceConfigResponse, error) {
10+
return nil, nil
11+
}

0 commit comments

Comments
 (0)