Skip to content

Commit 9168f7f

Browse files
committed
Refactor
1 parent 8603aea commit 9168f7f

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

internal/proto6server/server_listresource.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ResourceSchemaNotFoundError struct {
1717
func (e *ResourceSchemaNotFoundError) Error() string {
1818
return "resource schema not found for type: " + e.TypeName
1919
}
20+
2021
func (e *ResourceSchemaNotFoundError) Is(err error) bool {
2122
compatibleErr, ok := err.(*ResourceSchemaNotFoundError)
2223
if !ok {
@@ -50,6 +51,7 @@ type ListResourceSchemaNotFoundError struct {
5051
func (e *ListResourceSchemaNotFoundError) Error() string {
5152
return "list resource schema not found for type: " + e.TypeName
5253
}
54+
5355
func (e *ListResourceSchemaNotFoundError) Is(err error) bool {
5456
compatibleErr, ok := err.(*ListResourceSchemaNotFoundError)
5557
if !ok {
@@ -78,39 +80,31 @@ func (e *ListResourceConfigError) Is(err error) bool {
7880
}
7981

8082
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+
8186
listResource, err := s.FrameworkServer.ListResourceOrError(ctx, proto6Req.TypeName)
8287
if err != nil {
83-
proto6Stream := &tfprotov6.ListResourceServerStream{}
84-
proto6Stream.Results = func(func(tfprotov6.ListResourceResult) bool) {}
85-
8688
return proto6Stream, err
8789
}
8890

8991
resourceSchema, diags := s.FrameworkServer.ResourceSchema(ctx, proto6Req.TypeName)
9092
if diags.HasError() {
91-
proto6Stream := &tfprotov6.ListResourceServerStream{}
92-
proto6Stream.Results = func(func(tfprotov6.ListResourceResult) bool) {}
9393
return proto6Stream, &ResourceSchemaNotFoundError{TypeName: proto6Req.TypeName}
9494
}
9595

9696
identitySchema, diags := s.FrameworkServer.ResourceIdentitySchema(ctx, proto6Req.TypeName)
9797
if diags.HasError() {
98-
proto6Stream := &tfprotov6.ListResourceServerStream{}
99-
proto6Stream.Results = func(func(tfprotov6.ListResourceResult) bool) {}
100-
return proto6Stream, &ResourceIdentitySchemaNotFoundError{TypeName: proto6Req.TypeName} // TODO: return a diagnostic instead of an error?
98+
return proto6Stream, &ResourceIdentitySchemaNotFoundError{TypeName: proto6Req.TypeName}
10199
}
102100

103101
listResourceSchema, diags := s.FrameworkServer.ListResourceSchema(ctx, proto6Req.TypeName)
104102
if diags.HasError() {
105-
proto6Stream := &tfprotov6.ListResourceServerStream{}
106-
proto6Stream.Results = func(func(tfprotov6.ListResourceResult) bool) {}
107103
return proto6Stream, &ListResourceSchemaNotFoundError{TypeName: proto6Req.TypeName}
108104
}
109105

110106
config, diags := fromproto6.Config(ctx, proto6Req.Config, listResourceSchema)
111107
if diags.HasError() {
112-
proto6Stream := &tfprotov6.ListResourceServerStream{}
113-
proto6Stream.Results = func(func(tfprotov6.ListResourceResult) bool) {}
114108
return proto6Stream, &ListResourceConfigError{TypeName: proto6Req.TypeName, Diagnostics: diags}
115109
}
116110

@@ -128,7 +122,6 @@ func (s *Server) ListResource(ctx context.Context, proto6Req *tfprotov6.ListReso
128122
return nil, err
129123
}
130124

131-
proto6Stream := &tfprotov6.ListResourceServerStream{}
132125
proto6Stream.Results = func(push func(tfprotov6.ListResourceResult) bool) {
133126
for result := range stream.Results {
134127
var proto6Result tfprotov6.ListResourceResult

internal/proto6server/server_listresource_test.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,42 @@ import (
2323
func TestServerListResource(t *testing.T) {
2424
t.Parallel()
2525

26-
type ThingResource struct {
27-
Name string `tfsdk:"name"`
26+
type ThingResourceIdentity struct {
27+
Id string `tfsdk:"id"`
2828
}
2929

30-
type ThingResourceIdentity struct {
30+
type ThingResource struct {
31+
// TODO: how do we feel about this?
32+
ThingResourceIdentity
3133
Name string `tfsdk:"name"`
3234
}
3335

3436
resources := map[string]ThingResource{}
35-
resourceIdentities := map[string]ThingResourceIdentity{}
3637
expectedResources := map[string]*tfprotov6.DynamicValue{}
3738
expectedResourceIdentities := map[string]*tfprotov6.ResourceIdentityData{}
3839

3940
examples := []string{"bookbag", "bookshelf", "bookworm", "plateau", "platinum", "platypus"}
4041
for _, example := range examples {
41-
resources[example] = ThingResource{Name: example}
42-
resourceIdentities[example] = ThingResourceIdentity{Name: example}
42+
id := "id-" + example
43+
resources[example] = ThingResource{Name: example, ThingResourceIdentity: ThingResourceIdentity{Id: id}}
4344

4445
expectedResources[example] = testNewDynamicValue(t, tftypes.Object{
4546
AttributeTypes: map[string]tftypes.Type{
47+
"id": tftypes.String,
4648
"name": tftypes.String,
4749
},
4850
}, map[string]tftypes.Value{
51+
"id": tftypes.NewValue(tftypes.String, id),
4952
"name": tftypes.NewValue(tftypes.String, example),
5053
})
5154

5255
expectedResourceIdentities[example] = &tfprotov6.ResourceIdentityData{
5356
IdentityData: testNewDynamicValue(t, tftypes.Object{
5457
AttributeTypes: map[string]tftypes.Type{
55-
"name": tftypes.String,
58+
"id": tftypes.String,
5659
},
5760
}, map[string]tftypes.Value{
58-
"name": tftypes.NewValue(tftypes.String, example),
61+
"id": tftypes.NewValue(tftypes.String, id),
5962
}),
6063
}
6164
}
@@ -100,25 +103,12 @@ func TestServerListResource(t *testing.T) {
100103
continue
101104
}
102105

103-
result := list.ListResult{}
104-
identity, diags := req.ToIdentity(ctx, resourceIdentities[name])
105-
if diags.HasError() {
106-
result.Diagnostics = diags
107-
results = append(results, result)
108-
continue
109-
}
110-
111-
resource, diags := req.ToResource(ctx, resources[name])
112-
if diags.HasError() {
113-
result.Diagnostics = diags
114-
results = append(results, result)
115-
continue
116-
}
106+
result := req.ToResult(
107+
ctx,
108+
resources[name].ThingResourceIdentity,
109+
resources[name],
110+
name)
117111

118-
result.DisplayName = name
119-
result.Identity = identity
120-
result.Resource = resource // maybe only include if request says so
121-
result.Diagnostics = diags
122112
results = append(results, result)
123113
}
124114
resp.Results = slices.Values(results)
@@ -134,7 +124,7 @@ func TestServerListResource(t *testing.T) {
134124
IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
135125
resp.IdentitySchema = identityschema.Schema{
136126
Attributes: map[string]identityschema.Attribute{
137-
"name": identityschema.StringAttribute{},
127+
"id": identityschema.StringAttribute{},
138128
},
139129
}
140130
},
@@ -145,6 +135,7 @@ func TestServerListResource(t *testing.T) {
145135
SchemaMethod: func(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
146136
resp.Schema = resourceschema.Schema{
147137
Attributes: map[string]resourceschema.Attribute{
138+
"id": resourceschema.StringAttribute{},
148139
"name": resourceschema.StringAttribute{},
149140
},
150141
}

list/list_resource.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,31 @@ func (r ListRequest) ToIdentity(ctx context.Context, val any) (*tfsdk.ResourceId
131131
return identity, diags
132132
}
133133

134+
func (r ListRequest) ToResult(ctx context.Context, identityVal any, resourceVal any, displayName string) ListResult {
135+
diags := diag.Diagnostics{}
136+
identity, d := r.ToIdentity(ctx, identityVal)
137+
diags.Append(d...)
138+
if diags.HasError() {
139+
return ListResult{Diagnostics: diags}
140+
}
141+
142+
var resource *tfsdk.Resource
143+
if r.IncludeResource {
144+
resource, d = r.ToResource(ctx, resourceVal)
145+
diags.Append(d...)
146+
if diags.HasError() {
147+
return ListResult{Diagnostics: diags}
148+
}
149+
}
150+
151+
return ListResult{
152+
DisplayName: displayName,
153+
Resource: resource,
154+
Identity: identity,
155+
Diagnostics: diags,
156+
}
157+
}
158+
134159
// ListResultsStream represents a streaming response to a ListRequest. An
135160
// instance of this struct is supplied as an argument to the provider's
136161
// ListResource function. The provider should set a Results iterator function

0 commit comments

Comments
 (0)