@@ -4178,6 +4178,142 @@ func TestResourceDataSetType(t *testing.T) {
41784178 }
41794179}
41804180
4181+ func TestResourceDataIdentity (t * testing.T ) {
4182+ d := & ResourceData {
4183+ identitySchema : map [string ]* Schema {
4184+ "foo" : {
4185+ Type : TypeString ,
4186+ RequiredForImport : true ,
4187+ },
4188+ },
4189+ }
4190+ d .SetId ("baz" ) // just required to be able to call .State()
4191+ identity , err := d .Identity ()
4192+ if err != nil {
4193+ t .Fatalf ("err: %s" , err )
4194+ }
4195+
4196+ // test setting
4197+ err = identity .Set ("foo" , "bar" )
4198+ if err != nil {
4199+ t .Fatalf ("err: %s" , err )
4200+ }
4201+
4202+ // test memoization
4203+ identity2 , err := d .Identity ()
4204+ if err != nil {
4205+ t .Fatalf ("err: %s" , err )
4206+ }
4207+ if identity2 .Get ("foo" ).(string ) != "bar" {
4208+ t .Fatalf ("expected identity to contain value for foo: %#v" , identity2 )
4209+ }
4210+
4211+ // test identity added to state
4212+ state := d .State ()
4213+ if state .Identity == nil {
4214+ t .Fatalf ("expected identity to be added to state: %#v" , state )
4215+ }
4216+ if state .Identity ["foo" ] != "bar" {
4217+ t .Fatalf ("expected identity to contain value for foo: %#v" , state )
4218+ }
4219+ }
4220+
4221+ func TestResourceDataIdentity_initial_data_from_state (t * testing.T ) {
4222+ d := & ResourceData {
4223+ identitySchema : map [string ]* Schema {
4224+ "foo" : {
4225+ Type : TypeString ,
4226+ RequiredForImport : true ,
4227+ },
4228+ },
4229+ state : & terraform.InstanceState {
4230+ Identity : map [string ]string {
4231+ "foo" : "bar" ,
4232+ },
4233+ },
4234+ }
4235+ identity , err := d .Identity ()
4236+ if err != nil {
4237+ t .Fatalf ("err: %s" , err )
4238+ }
4239+ if identity .Get ("foo" ).(string ) != "bar" {
4240+ t .Fatalf ("expected identity to contain value for foo: %#v" , identity )
4241+ }
4242+ }
4243+
4244+ func TestResourceDataIdentity_initial_data_from_diff (t * testing.T ) {
4245+ d := & ResourceData {
4246+ identitySchema : map [string ]* Schema {
4247+ "foo" : {
4248+ Type : TypeString ,
4249+ RequiredForImport : true ,
4250+ },
4251+ },
4252+ // we also keep this to ensure diff takes precedence over state
4253+ state : & terraform.InstanceState {
4254+ Identity : map [string ]string {
4255+ "foo" : "bar" ,
4256+ },
4257+ },
4258+ diff : & terraform.InstanceDiff {
4259+ Identity : map [string ]string {
4260+ "foo" : "baz" ,
4261+ },
4262+ },
4263+ }
4264+ identity , err := d .Identity ()
4265+ if err != nil {
4266+ t .Fatalf ("err: %s" , err )
4267+ }
4268+ if identity .Get ("foo" ).(string ) != "baz" {
4269+ t .Fatalf ("expected identity to contain baz value for foo: %#v" , identity )
4270+ }
4271+ }
4272+
4273+ func TestResourceDataIdentity_changing_initial_data (t * testing.T ) {
4274+ d := & ResourceData {
4275+ identitySchema : map [string ]* Schema {
4276+ "foo" : {
4277+ Type : TypeString ,
4278+ RequiredForImport : true ,
4279+ },
4280+ },
4281+ diff : & terraform.InstanceDiff {
4282+ Identity : map [string ]string {
4283+ "foo" : "baz" ,
4284+ },
4285+ },
4286+ }
4287+ d .SetId ("bar" ) // just required to be able to call .State()
4288+ identity , err := d .Identity ()
4289+ if err != nil {
4290+ t .Fatalf ("err: %s" , err )
4291+ }
4292+ err = identity .Set ("foo" , "qux" )
4293+ if err != nil {
4294+ t .Fatalf ("err: %s" , err )
4295+ }
4296+
4297+ state := d .State ()
4298+ if state .Identity == nil {
4299+ t .Fatalf ("expected identity to be added to state: %#v" , state )
4300+ }
4301+ if state .Identity ["foo" ] != "qux" {
4302+ t .Fatalf ("expected identity to contain qux value for foo: %#v" , state )
4303+ }
4304+ }
4305+
4306+ func TestResourceDataIdentity_no_schema (t * testing.T ) {
4307+ d := & ResourceData {}
4308+ _ , err := d .Identity ()
4309+ if err == nil {
4310+ t .Fatalf ("expected error since there's no identity schema, got: nil" )
4311+ }
4312+ if diff := cmp .Diff ("Resource does not have Identity schema. Please set one in order to use Identity(). This is always a problem in the provider code." , err .Error ()); diff != "" {
4313+ t .Fatalf ("unexpected error message (-want +got):\n %s" , diff )
4314+ }
4315+ }
4316+
41814317func testPtrTo (raw interface {}) interface {} {
41824318 return & raw
41834319}
0 commit comments