Skip to content

Commit 707e888

Browse files
committed
refactor
1 parent 6ff26ee commit 707e888

File tree

2 files changed

+66
-68
lines changed

2 files changed

+66
-68
lines changed

internal/proto5server/server_listresource_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ package proto5server
55

66
import (
77
"context"
8+
"fmt"
89
"slices"
910
"strings"
1011
"testing"
1112

1213
"github.com/google/go-cmp/cmp"
1314
"github.com/google/go-cmp/cmp/cmpopts"
15+
"github.com/hashicorp/go-cty/cty/msgpack"
1416
"github.com/hashicorp/terraform-plugin-framework/diag"
17+
"github.com/hashicorp/terraform-plugin-framework/hcl2shim"
1518
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1619
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
1720
"github.com/hashicorp/terraform-plugin-framework/list"
@@ -21,6 +24,8 @@ import (
2124
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2225
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
2326
"github.com/hashicorp/terraform-plugin-go/tftypes"
27+
sdk "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28+
terraformsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2429
)
2530

2631
func TestServerListResource(t *testing.T) {
@@ -276,3 +281,64 @@ func TestServerListResource(t *testing.T) {
276281
})
277282
}
278283
}
284+
285+
func TestServerListResourceProto5ToProto5(t *testing.T) {
286+
t.Parallel()
287+
288+
// 1: we have a resource type defined in SDKv2
289+
sdkResource := sdk.Resource{
290+
Schema: map[string]*sdk.Schema{
291+
"id": &sdk.Schema{
292+
Type: sdk.TypeString,
293+
},
294+
"name": &sdk.Schema{
295+
Type: sdk.TypeString,
296+
},
297+
},
298+
}
299+
300+
// 2: from the resource type, we can obtain an initialized ResourceData value
301+
d := sdkResource.Data(&terraformsdk.InstanceState{ID: "#groot"})
302+
303+
// 3: the initialized ResourceData value is schema-aware
304+
if err := d.Set("name", "Groot"); err != nil {
305+
t.Fatalf("Error setting `name`: %v", err)
306+
}
307+
308+
if err := d.Set("nom", "groot"); err == nil {
309+
t.Fatal("False negative outcome: `nom` is not a schema attribute")
310+
}
311+
312+
displayName := "I am Groot"
313+
314+
// 4: mimic SDK GRPCProviderServer.ReadResource ResourceData -> MsgPack
315+
state := d.State()
316+
if state == nil {
317+
t.Fatal("Expected state to be non-nil")
318+
}
319+
320+
schemaBlock := sdkResource.CoreConfigSchema()
321+
if schemaBlock == nil {
322+
t.Fatal("Expected schemaBlock to be non-nil")
323+
}
324+
325+
// Copied hcl2shim wholesale for purposes of making the test pass
326+
newStateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Attributes, schemaBlock.ImpliedType())
327+
if err != nil {
328+
t.Fatalf("Error converting state attributes to HCL2 value: %v", err)
329+
}
330+
331+
// newStateVal = normalizeNullValues(newStateVal, stateVal, false)
332+
333+
pack, err := msgpack.Marshal(newStateVal, schemaBlock.ImpliedType())
334+
if err != nil {
335+
t.Fatalf("Error marshaling new state value to MsgPack: %v", err)
336+
}
337+
338+
fmt.Printf("MsgPack: %s\n", pack)
339+
340+
// 5: construct a tfprotov5.ListResourceResult
341+
listResult := tfprotov5.ListResourceResult{}
342+
listResult.Resource = &tfprotov5.DynamicValue{MsgPack: pack}
343+
listResult.DisplayName = displayName
344+
}

list/list_resource_test.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ package list_test
55

66
import (
77
"context"
8-
"fmt"
9-
"testing"
108

11-
"github.com/hashicorp/go-cty/cty/msgpack"
12-
"github.com/hashicorp/terraform-plugin-framework/hcl2shim"
139
"github.com/hashicorp/terraform-plugin-framework/list"
1410
"github.com/hashicorp/terraform-plugin-framework/resource"
15-
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
16-
sdk "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17-
tsdk "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1811
)
1912

2013
type ComputeInstanceResource struct {
@@ -56,64 +49,3 @@ func ExampleResource_listable() {
5649

5750
// Output:
5851
}
59-
60-
func TestListResultToResourceData(t *testing.T) {
61-
t.Parallel()
62-
63-
// 1: we have a resource type defined in SDKv2
64-
sdkResource := sdk.Resource{
65-
Schema: map[string]*sdk.Schema{
66-
"id": &sdk.Schema{
67-
Type: sdk.TypeString,
68-
},
69-
"name": &sdk.Schema{
70-
Type: sdk.TypeString,
71-
},
72-
},
73-
}
74-
75-
// 2: from the resource type, we can obtain an initialized ResourceData value
76-
d := sdkResource.Data(&tsdk.InstanceState{ID: "#groot"})
77-
78-
// 3: the initialized ResourceData value is schema-aware
79-
if err := d.Set("name", "Groot"); err != nil {
80-
t.Fatalf("Error setting `name`: %v", err)
81-
}
82-
83-
if err := d.Set("nom", "groot"); err == nil {
84-
t.Fatal("False negative outcome: `nom` is not a schema attribute")
85-
}
86-
87-
displayName := "I am Groot"
88-
89-
// 4: mimic SDK GRPCProviderServer.ReadResource ResourceData -> MsgPack
90-
state := d.State()
91-
if state == nil {
92-
t.Fatal("Expected state to be non-nil")
93-
}
94-
95-
schemaBlock := sdkResource.CoreConfigSchema()
96-
if schemaBlock == nil {
97-
t.Fatal("Expected schemaBlock to be non-nil")
98-
}
99-
100-
// Copied hcl2shim wholesale for purposes of making the test pass
101-
newStateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Attributes, schemaBlock.ImpliedType())
102-
if err != nil {
103-
t.Fatalf("Error converting state attributes to HCL2 value: %v", err)
104-
}
105-
106-
// newStateVal = normalizeNullValues(newStateVal, stateVal, false)
107-
108-
pack, err := msgpack.Marshal(newStateVal, schemaBlock.ImpliedType())
109-
if err != nil {
110-
t.Fatalf("Error marshaling new state value to MsgPack: %v", err)
111-
}
112-
113-
fmt.Printf("MsgPack: %s\n", pack)
114-
115-
// 5: construct a tfprotov5.ListResourceResult
116-
listResult := tfprotov5.ListResourceResult{}
117-
listResult.Resource = &tfprotov5.DynamicValue{MsgPack: pack}
118-
listResult.DisplayName = displayName
119-
}

0 commit comments

Comments
 (0)