Skip to content

Commit b4551c0

Browse files
committed
Adding to GetMetadata for ListResources, tests not passing yet for internal/proto5server/server_getmetadata_test.go, something is missing to add ListResource here
1 parent 812bad4 commit b4551c0

File tree

3 files changed

+285
-14
lines changed

3 files changed

+285
-14
lines changed

internal/proto5server/server_getmetadata_test.go

Lines changed: 165 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package proto5server
55

66
import (
77
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/list"
89
"sort"
910
"testing"
1011

@@ -63,6 +64,7 @@ func TestServerGetMetadata(t *testing.T) {
6364
},
6465
},
6566
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
67+
ListResources: []tfprotov5.ListResourceMetadata{},
6668
Functions: []tfprotov5.FunctionMetadata{},
6769
Resources: []tfprotov5.ResourceMetadata{},
6870
ServerCapabilities: &tfprotov5.ServerCapabilities{
@@ -110,8 +112,9 @@ func TestServerGetMetadata(t *testing.T) {
110112
"This is always an issue with the provider and should be reported to the provider developers.",
111113
},
112114
},
113-
Functions: []tfprotov5.FunctionMetadata{},
114-
Resources: []tfprotov5.ResourceMetadata{},
115+
ListResources: []tfprotov5.ListResourceMetadata{},
116+
Functions: []tfprotov5.FunctionMetadata{},
117+
Resources: []tfprotov5.ResourceMetadata{},
115118
ServerCapabilities: &tfprotov5.ServerCapabilities{
116119
GetProviderSchemaOptional: true,
117120
MoveResourceState: true,
@@ -149,8 +152,9 @@ func TestServerGetMetadata(t *testing.T) {
149152
"This is always an issue with the provider and should be reported to the provider developers.",
150153
},
151154
},
152-
Functions: []tfprotov5.FunctionMetadata{},
153-
Resources: []tfprotov5.ResourceMetadata{},
155+
ListResources: []tfprotov5.ListResourceMetadata{},
156+
Functions: []tfprotov5.FunctionMetadata{},
157+
Resources: []tfprotov5.ResourceMetadata{},
154158
ServerCapabilities: &tfprotov5.ServerCapabilities{
155159
GetProviderSchemaOptional: true,
156160
MoveResourceState: true,
@@ -194,8 +198,9 @@ func TestServerGetMetadata(t *testing.T) {
194198
TypeName: "test_ephemeral_resource2",
195199
},
196200
},
197-
Functions: []tfprotov5.FunctionMetadata{},
198-
Resources: []tfprotov5.ResourceMetadata{},
201+
ListResources: []tfprotov5.ListResourceMetadata{},
202+
Functions: []tfprotov5.FunctionMetadata{},
203+
Resources: []tfprotov5.ResourceMetadata{},
199204
ServerCapabilities: &tfprotov5.ServerCapabilities{
200205
GetProviderSchemaOptional: true,
201206
MoveResourceState: true,
@@ -241,8 +246,9 @@ func TestServerGetMetadata(t *testing.T) {
241246
"This is always an issue with the provider and should be reported to the provider developers.",
242247
},
243248
},
244-
Functions: []tfprotov5.FunctionMetadata{},
245-
Resources: []tfprotov5.ResourceMetadata{},
249+
ListResources: []tfprotov5.ListResourceMetadata{},
250+
Functions: []tfprotov5.FunctionMetadata{},
251+
Resources: []tfprotov5.ResourceMetadata{},
246252
ServerCapabilities: &tfprotov5.ServerCapabilities{
247253
GetProviderSchemaOptional: true,
248254
MoveResourceState: true,
@@ -280,6 +286,141 @@ func TestServerGetMetadata(t *testing.T) {
280286
"This is always an issue with the provider and should be reported to the provider developers.",
281287
},
282288
},
289+
ListResources: []tfprotov5.ListResourceMetadata{},
290+
Functions: []tfprotov5.FunctionMetadata{},
291+
Resources: []tfprotov5.ResourceMetadata{},
292+
ServerCapabilities: &tfprotov5.ServerCapabilities{
293+
GetProviderSchemaOptional: true,
294+
MoveResourceState: true,
295+
PlanDestroy: true,
296+
},
297+
},
298+
},
299+
"listresources": {
300+
server: &Server{
301+
FrameworkServer: fwserver.Server{
302+
Provider: &testprovider.Provider{
303+
ListResourcesMethod: func(_ context.Context) []func() list.ListResource {
304+
return []func() list.ListResource{
305+
func() list.ListResource {
306+
return &testprovider.ListResource{
307+
MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
308+
resp.TypeName = "test_list_resource1"
309+
},
310+
}
311+
},
312+
func() list.ListResource {
313+
return &testprovider.ListResource{
314+
MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
315+
resp.TypeName = "test_list_resource2"
316+
},
317+
}
318+
},
319+
}
320+
},
321+
},
322+
},
323+
},
324+
request: &tfprotov5.GetMetadataRequest{},
325+
expectedResponse: &tfprotov5.GetMetadataResponse{
326+
DataSources: []tfprotov5.DataSourceMetadata{},
327+
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
328+
ListResources: []tfprotov5.ListResourceMetadata{
329+
{
330+
TypeName: "test_list_resource1",
331+
},
332+
{
333+
TypeName: "test_list_resource2",
334+
},
335+
},
336+
Functions: []tfprotov5.FunctionMetadata{},
337+
Resources: []tfprotov5.ResourceMetadata{},
338+
ServerCapabilities: &tfprotov5.ServerCapabilities{
339+
GetProviderSchemaOptional: true,
340+
MoveResourceState: true,
341+
PlanDestroy: true,
342+
},
343+
},
344+
},
345+
"listresources-duplicate-type-name": {
346+
server: &Server{
347+
FrameworkServer: fwserver.Server{
348+
Provider: &testprovider.Provider{
349+
ListResourcesMethod: func(_ context.Context) []func() list.ListResource {
350+
return []func() list.ListResource{
351+
func() list.ListResource {
352+
return &testprovider.ListResource{
353+
MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
354+
resp.TypeName = "test_list_resource"
355+
},
356+
}
357+
},
358+
func() list.ListResource {
359+
return &testprovider.ListResource{
360+
MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
361+
resp.TypeName = "test_list_resource"
362+
},
363+
}
364+
},
365+
}
366+
},
367+
},
368+
},
369+
},
370+
request: &tfprotov5.GetMetadataRequest{},
371+
expectedResponse: &tfprotov5.GetMetadataResponse{
372+
DataSources: []tfprotov5.DataSourceMetadata{},
373+
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
374+
ListResources: []tfprotov5.ListResourceMetadata{},
375+
Diagnostics: []*tfprotov5.Diagnostic{
376+
{
377+
Severity: tfprotov5.DiagnosticSeverityError,
378+
Summary: "Duplicate List Resource Type Defined",
379+
Detail: "The test_list_resource list resource type name was returned for multiple list resources. " +
380+
"List resource type names must be unique. " +
381+
"This is always an issue with the provider and should be reported to the provider developers.",
382+
},
383+
},
384+
Functions: []tfprotov5.FunctionMetadata{},
385+
Resources: []tfprotov5.ResourceMetadata{},
386+
ServerCapabilities: &tfprotov5.ServerCapabilities{
387+
GetProviderSchemaOptional: true,
388+
MoveResourceState: true,
389+
PlanDestroy: true,
390+
},
391+
},
392+
},
393+
"listresources-empty-type-name": {
394+
server: &Server{
395+
FrameworkServer: fwserver.Server{
396+
Provider: &testprovider.Provider{
397+
ListResourcesMethod: func(_ context.Context) []func() list.ListResource {
398+
return []func() list.ListResource{
399+
func() list.ListResource {
400+
return &testprovider.ListResource{
401+
MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
402+
resp.TypeName = ""
403+
},
404+
}
405+
},
406+
}
407+
},
408+
},
409+
},
410+
},
411+
request: &tfprotov5.GetMetadataRequest{},
412+
expectedResponse: &tfprotov5.GetMetadataResponse{
413+
DataSources: []tfprotov5.DataSourceMetadata{},
414+
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
415+
ListResources: []tfprotov5.ListResourceMetadata{},
416+
Diagnostics: []*tfprotov5.Diagnostic{
417+
{
418+
Severity: tfprotov5.DiagnosticSeverityError,
419+
Summary: "List Resource Type Name Missing",
420+
Detail: "The *testprovider.ListResource ListResource returned an empty string from the Metadata method. " +
421+
"This is always an issue with the provider and should be reported to the provider developers.",
422+
},
423+
},
283424
Functions: []tfprotov5.FunctionMetadata{},
284425
Resources: []tfprotov5.ResourceMetadata{},
285426
ServerCapabilities: &tfprotov5.ServerCapabilities{
@@ -318,6 +459,7 @@ func TestServerGetMetadata(t *testing.T) {
318459
expectedResponse: &tfprotov5.GetMetadataResponse{
319460
DataSources: []tfprotov5.DataSourceMetadata{},
320461
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
462+
ListResources: []tfprotov5.ListResourceMetadata{},
321463
Functions: []tfprotov5.FunctionMetadata{
322464
{
323465
Name: "function1",
@@ -372,8 +514,9 @@ func TestServerGetMetadata(t *testing.T) {
372514
"This is always an issue with the provider and should be reported to the provider developers.",
373515
},
374516
},
375-
Functions: []tfprotov5.FunctionMetadata{},
376-
Resources: []tfprotov5.ResourceMetadata{},
517+
ListResources: []tfprotov5.ListResourceMetadata{},
518+
Functions: []tfprotov5.FunctionMetadata{},
519+
Resources: []tfprotov5.ResourceMetadata{},
377520
ServerCapabilities: &tfprotov5.ServerCapabilities{
378521
GetProviderSchemaOptional: true,
379522
MoveResourceState: true,
@@ -411,8 +554,9 @@ func TestServerGetMetadata(t *testing.T) {
411554
"This is always an issue with the provider and should be reported to the provider developers.",
412555
},
413556
},
414-
Functions: []tfprotov5.FunctionMetadata{},
415-
Resources: []tfprotov5.ResourceMetadata{},
557+
ListResources: []tfprotov5.ListResourceMetadata{},
558+
Functions: []tfprotov5.FunctionMetadata{},
559+
Resources: []tfprotov5.ResourceMetadata{},
416560
ServerCapabilities: &tfprotov5.ServerCapabilities{
417561
GetProviderSchemaOptional: true,
418562
MoveResourceState: true,
@@ -449,6 +593,7 @@ func TestServerGetMetadata(t *testing.T) {
449593
expectedResponse: &tfprotov5.GetMetadataResponse{
450594
DataSources: []tfprotov5.DataSourceMetadata{},
451595
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
596+
ListResources: []tfprotov5.ListResourceMetadata{},
452597
Functions: []tfprotov5.FunctionMetadata{},
453598
Resources: []tfprotov5.ResourceMetadata{
454599
{
@@ -503,8 +648,9 @@ func TestServerGetMetadata(t *testing.T) {
503648
"This is always an issue with the provider and should be reported to the provider developers.",
504649
},
505650
},
506-
Functions: []tfprotov5.FunctionMetadata{},
507-
Resources: []tfprotov5.ResourceMetadata{},
651+
ListResources: []tfprotov5.ListResourceMetadata{},
652+
Functions: []tfprotov5.FunctionMetadata{},
653+
Resources: []tfprotov5.ResourceMetadata{},
508654
ServerCapabilities: &tfprotov5.ServerCapabilities{
509655
GetProviderSchemaOptional: true,
510656
MoveResourceState: true,
@@ -534,6 +680,7 @@ func TestServerGetMetadata(t *testing.T) {
534680
expectedResponse: &tfprotov5.GetMetadataResponse{
535681
DataSources: []tfprotov5.DataSourceMetadata{},
536682
EphemeralResources: []tfprotov5.EphemeralResourceMetadata{},
683+
ListResources: []tfprotov5.ListResourceMetadata{},
537684
Diagnostics: []*tfprotov5.Diagnostic{
538685
{
539686
Severity: tfprotov5.DiagnosticSeverityError,
@@ -572,6 +719,10 @@ func TestServerGetMetadata(t *testing.T) {
572719
return got.EphemeralResources[i].TypeName < got.EphemeralResources[j].TypeName
573720
})
574721

722+
sort.Slice(got.ListResources, func(i, j int) bool {
723+
return got.ListResources[i].TypeName < got.ListResources[j].TypeName
724+
})
725+
575726
sort.Slice(got.Functions, func(i int, j int) bool {
576727
return got.Functions[i].Name < got.Functions[j].Name
577728
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto5_test
5+
6+
import (
7+
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testschema"
11+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
16+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
17+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
18+
)
19+
20+
func TestListResourceResult(t *testing.T) {
21+
t.Parallel()
22+
23+
testListResultData := &fwserver.ListResult{
24+
Identity: nil,
25+
Resource: &tfsdk.Resource{
26+
Schema: testschema.Schema{
27+
Attributes: map[string]fwschema.Attribute{
28+
"test_attribute": testschema.Attribute{
29+
Required: true,
30+
Type: types.StringType,
31+
},
32+
},
33+
},
34+
},
35+
DisplayName: "test-display-name",
36+
Diagnostics: nil,
37+
}
38+
39+
testCases := map[string]struct {
40+
input *fwserver.ListResult
41+
expected tfprotov5.ListResourceResult
42+
}{
43+
"nil": {
44+
input: &fwserver.ListResult{
45+
Identity: nil,
46+
Resource: nil,
47+
DisplayName: "",
48+
Diagnostics: nil,
49+
},
50+
expected: tfprotov5.ListResourceResult{
51+
Identity: nil,
52+
Resource: nil,
53+
DisplayName: "",
54+
Diagnostics: nil,
55+
},
56+
},
57+
"valid": {
58+
input: testListResultData,
59+
expected: tfprotov5.ListResourceResult{
60+
DisplayName: "test-display-name",
61+
},
62+
},
63+
}
64+
65+
for name, testCase := range testCases {
66+
t.Run(name, func(t *testing.T) {
67+
t.Parallel()
68+
69+
got := toproto5.ListResourceResult(context.Background(), testCase.input)
70+
71+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
72+
t.Errorf("unexpected difference: %s", diff)
73+
}
74+
})
75+
}
76+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto5_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
14+
)
15+
16+
func TestListResourceMetadata(t *testing.T) {
17+
t.Parallel()
18+
19+
testCases := map[string]struct {
20+
fw fwserver.ListResourceMetadata
21+
expected tfprotov5.ListResourceMetadata
22+
}{
23+
"TypeName": {
24+
fw: fwserver.ListResourceMetadata{
25+
TypeName: "test",
26+
},
27+
expected: tfprotov5.ListResourceMetadata{
28+
TypeName: "test",
29+
},
30+
},
31+
}
32+
33+
for name, testCase := range testCases {
34+
t.Run(name, func(t *testing.T) {
35+
t.Parallel()
36+
37+
got := toproto5.ListResourceMetadata(context.Background(), testCase.fw)
38+
39+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
40+
t.Errorf("unexpected difference: %s", diff)
41+
}
42+
})
43+
}
44+
}

0 commit comments

Comments
 (0)