@@ -5,13 +5,16 @@ package proto5server
55
66import (
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
2631func 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+ }
0 commit comments