@@ -17,6 +17,7 @@ import (
1717 "github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1818 "github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
1919 "github.com/hashicorp/terraform-plugin-framework/resource"
20+ "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
2021 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2122 "github.com/hashicorp/terraform-plugin-framework/tfsdk"
2223)
@@ -48,6 +49,30 @@ func TestPlanResourceChangeRequest(t *testing.T) {
4849 },
4950 }
5051
52+ testIdentityProto5Type := tftypes.Object {
53+ AttributeTypes : map [string ]tftypes.Type {
54+ "test_identity_attribute" : tftypes .String ,
55+ },
56+ }
57+
58+ testIdentityProto5Value := tftypes .NewValue (testIdentityProto5Type , map [string ]tftypes.Value {
59+ "test_identity_attribute" : tftypes .NewValue (tftypes .String , "id-123" ),
60+ })
61+
62+ testIdentityProto5DynamicValue , err := tfprotov5 .NewDynamicValue (testIdentityProto5Type , testIdentityProto5Value )
63+
64+ if err != nil {
65+ t .Fatalf ("unexpected error calling tfprotov5.NewDynamicValue(): %s" , err )
66+ }
67+
68+ testIdentitySchema := identityschema.Schema {
69+ Attributes : map [string ]identityschema.Attribute {
70+ "test_identity_attribute" : identityschema.StringAttribute {
71+ RequiredForImport : true ,
72+ },
73+ },
74+ }
75+
5176 testProviderKeyValue := privatestate .MustMarshalToJson (map [string ][]byte {
5277 "providerKeyOne" : []byte (`{"pKeyOne": {"k0": "zero", "k1": 1}}` ),
5378 })
@@ -58,6 +83,7 @@ func TestPlanResourceChangeRequest(t *testing.T) {
5883 input * tfprotov5.PlanResourceChangeRequest
5984 resourceBehavior resource.ResourceBehavior
6085 resourceSchema fwschema.Schema
86+ identitySchema fwschema.Schema
6187 resource resource.Resource
6288 providerMetaSchema fwschema.Schema
6389 expected * fwserver.PlanResourceChangeRequest
@@ -182,6 +208,42 @@ func TestPlanResourceChangeRequest(t *testing.T) {
182208 ResourceSchema : testFwSchema ,
183209 },
184210 },
211+ "prioridentity-missing-schema" : {
212+ input : & tfprotov5.PlanResourceChangeRequest {
213+ PriorIdentity : & tfprotov5.ResourceIdentityData {
214+ IdentityData : & testIdentityProto5DynamicValue ,
215+ },
216+ },
217+ resourceSchema : testFwSchema ,
218+ expected : & fwserver.PlanResourceChangeRequest {
219+ ResourceSchema : testFwSchema ,
220+ },
221+ expectedDiagnostics : diag.Diagnostics {
222+ diag .NewErrorDiagnostic (
223+ "Unable to Convert Resource Identity" ,
224+ "An unexpected error was encountered when converting the resource identity from the protocol type. " +
225+ "Identity data was sent in the protocol to a resource that doesn't support identity.\n \n " +
226+ "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer." ,
227+ ),
228+ },
229+ },
230+ "prioridentity" : {
231+ input : & tfprotov5.PlanResourceChangeRequest {
232+ PriorIdentity : & tfprotov5.ResourceIdentityData {
233+ IdentityData : & testIdentityProto5DynamicValue ,
234+ },
235+ },
236+ identitySchema : testIdentitySchema ,
237+ resourceSchema : testFwSchema ,
238+ expected : & fwserver.PlanResourceChangeRequest {
239+ IdentitySchema : testIdentitySchema ,
240+ PriorIdentity : & tfsdk.ResourceIdentity {
241+ Raw : testIdentityProto5Value ,
242+ Schema : testIdentitySchema ,
243+ },
244+ ResourceSchema : testFwSchema ,
245+ },
246+ },
185247 "providermeta-missing-data" : {
186248 input : & tfprotov5.PlanResourceChangeRequest {},
187249 resourceSchema : testFwSchema ,
@@ -265,7 +327,7 @@ func TestPlanResourceChangeRequest(t *testing.T) {
265327 t .Run (name , func (t * testing.T ) {
266328 t .Parallel ()
267329
268- got , diags := fromproto5 .PlanResourceChangeRequest (context .Background (), testCase .input , testCase .resource , testCase .resourceSchema , testCase .providerMetaSchema , testCase .resourceBehavior )
330+ got , diags := fromproto5 .PlanResourceChangeRequest (context .Background (), testCase .input , testCase .resource , testCase .resourceSchema , testCase .providerMetaSchema , testCase .resourceBehavior , testCase . identitySchema )
269331
270332 if diff := cmp .Diff (got , testCase .expected , cmp .AllowUnexported (privatestate.ProviderData {})); diff != "" {
271333 t .Errorf ("unexpected difference: %s" , diff )
0 commit comments