@@ -5,6 +5,7 @@ package schema
55
66import (
77 "fmt"
8+ "github.com/hashicorp/terraform-plugin-go/tftypes"
89 "math"
910 "reflect"
1011 "testing"
@@ -4314,6 +4315,125 @@ func TestResourceDataIdentity_no_schema(t *testing.T) {
43144315 }
43154316}
43164317
4318+ func TestResourceData_TfTypeIdentityState (t * testing.T ) {
4319+ d := & ResourceData {
4320+ identitySchema : map [string ]* Schema {
4321+ "foo" : {
4322+ Type : TypeString ,
4323+ RequiredForImport : true ,
4324+ },
4325+ },
4326+ }
4327+
4328+ d .SetId ("baz" ) // just required to be able to call .State()
4329+
4330+ identity , err := d .Identity ()
4331+ if err != nil {
4332+ t .Fatalf ("err: %s" , err )
4333+ }
4334+
4335+ err = identity .Set ("foo" , "bar" )
4336+ if err != nil {
4337+ t .Fatalf ("err: %s" , err )
4338+ }
4339+
4340+ expectedIdentity := tftypes .NewValue (tftypes.Object {
4341+ AttributeTypes : map [string ]tftypes.Type {
4342+ "foo" : tftypes .String ,
4343+ }}, map [string ]tftypes.Value {
4344+ "foo" : tftypes .NewValue (tftypes .String , "bar" ),
4345+ })
4346+
4347+ tfTypeIdentity := d .TfTypeIdentityState ()
4348+
4349+ if ! tfTypeIdentity .Equal (expectedIdentity ) {
4350+ t .Fatalf ("expected tftype value of identity to be %+v, got %+v" , expectedIdentity , tfTypeIdentity )
4351+ }
4352+ }
4353+
4354+ func TestResourceData_TfTypeResourceState (t * testing.T ) {
4355+ cases := []struct {
4356+ d * ResourceData
4357+ expected tftypes.Value
4358+ }{
4359+ {
4360+ d : & ResourceData {
4361+ schema : map [string ]* Schema {
4362+ "location" : {
4363+ Type : TypeString ,
4364+ Optional : true ,
4365+ },
4366+ },
4367+ timeouts : timeoutForValues (30 , 5 , 30 , 5 , 5 ),
4368+ },
4369+ expected : tftypes .NewValue (tftypes.Object {
4370+ AttributeTypes : map [string ]tftypes.Type {
4371+ "location" : tftypes .String ,
4372+ "id" : tftypes .String ,
4373+ "timeouts" : tftypes.Object {
4374+ AttributeTypes : map [string ]tftypes.Type {
4375+ "create" : tftypes .String ,
4376+ "delete" : tftypes .String ,
4377+ "read" : tftypes .String ,
4378+ "update" : tftypes .String ,
4379+ "default" : tftypes .String ,
4380+ },
4381+ },
4382+ }}, map [string ]tftypes.Value {
4383+ "location" : tftypes .NewValue (tftypes .String , "westeurope" ),
4384+ "id" : tftypes .NewValue (tftypes .String , "baz" ),
4385+ "timeouts" : tftypes .NewValue (tftypes.Object {
4386+ AttributeTypes : map [string ]tftypes.Type {
4387+ "create" : tftypes .String ,
4388+ "default" : tftypes .String ,
4389+ "delete" : tftypes .String ,
4390+ "read" : tftypes .String ,
4391+ "update" : tftypes .String ,
4392+ },
4393+ }, map [string ]tftypes.Value {
4394+ "create" : tftypes .NewValue (tftypes .String , nil ),
4395+ "default" : tftypes .NewValue (tftypes .String , nil ),
4396+ "delete" : tftypes .NewValue (tftypes .String , nil ),
4397+ "read" : tftypes .NewValue (tftypes .String , nil ),
4398+ "update" : tftypes .NewValue (tftypes .String , nil ),
4399+ }),
4400+ }),
4401+ },
4402+ {
4403+ d : & ResourceData {
4404+ schema : map [string ]* Schema {
4405+ "location" : {
4406+ Type : TypeString ,
4407+ Optional : true ,
4408+ },
4409+ },
4410+ },
4411+ expected : tftypes .NewValue (tftypes.Object {
4412+ AttributeTypes : map [string ]tftypes.Type {
4413+ "location" : tftypes .String ,
4414+ "id" : tftypes .String ,
4415+ }}, map [string ]tftypes.Value {
4416+ "location" : tftypes .NewValue (tftypes .String , "westeurope" ),
4417+ "id" : tftypes .NewValue (tftypes .String , "baz" ),
4418+ }),
4419+ },
4420+ }
4421+
4422+ for _ , tc := range cases {
4423+ tc .d .SetId ("baz" ) // just required to be able to call .State()
4424+
4425+ if err := tc .d .Set ("location" , "westeurope" ); err != nil {
4426+ t .Fatalf ("err: %s" , err )
4427+ }
4428+
4429+ tfTypeIdentity := tc .d .TfTypeResourceState ()
4430+
4431+ if ! tfTypeIdentity .Equal (tc .expected ) {
4432+ t .Fatalf ("expected tftype value of identity to be %+v, got %+v" , tc .expected , tfTypeIdentity )
4433+ }
4434+ }
4435+ }
4436+
43174437func testPtrTo (raw interface {}) interface {} {
43184438 return & raw
43194439}
0 commit comments