@@ -16,6 +16,7 @@ import (
1616 "github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1717 "github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1818 "github.com/hashicorp/terraform-plugin-framework/resource"
19+ "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
1920 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2021 "github.com/hashicorp/terraform-plugin-framework/tfsdk"
2122)
@@ -31,6 +32,30 @@ func TestImportResourceStateRequest(t *testing.T) {
3132 },
3233 }
3334
35+ testIdentityProto5Type := tftypes.Object {
36+ AttributeTypes : map [string ]tftypes.Type {
37+ "test_identity_attribute" : tftypes .String ,
38+ },
39+ }
40+
41+ testIdentityProto5Value := tftypes .NewValue (testIdentityProto5Type , map [string ]tftypes.Value {
42+ "test_identity_attribute" : tftypes .NewValue (tftypes .String , "id-123" ),
43+ })
44+
45+ testIdentityProto5DynamicValue , err := tfprotov5 .NewDynamicValue (testIdentityProto5Type , testIdentityProto5Value )
46+
47+ if err != nil {
48+ t .Fatalf ("unexpected error calling tfprotov5.NewDynamicValue(): %s" , err )
49+ }
50+
51+ testIdentitySchema := identityschema.Schema {
52+ Attributes : map [string ]identityschema.Attribute {
53+ "test_identity_attribute" : identityschema.StringAttribute {
54+ RequiredForImport : true ,
55+ },
56+ },
57+ }
58+
3459 testFwEmptyState := tfsdk.State {
3560 Raw : tftypes .NewValue (testFwSchema .Type ().TerraformType (context .Background ()), nil ),
3661 Schema : testFwSchema ,
@@ -39,6 +64,7 @@ func TestImportResourceStateRequest(t *testing.T) {
3964 testCases := map [string ]struct {
4065 input * tfprotov5.ImportResourceStateRequest
4166 resourceSchema fwschema.Schema
67+ identitySchema fwschema.Schema
4268 resource resource.Resource
4369 expected * fwserver.ImportResourceStateRequest
4470 expectedDiagnostics diag.Diagnostics
@@ -67,6 +93,42 @@ func TestImportResourceStateRequest(t *testing.T) {
6793 ),
6894 },
6995 },
96+ "identity-missing-schema" : {
97+ input : & tfprotov5.ImportResourceStateRequest {
98+ Identity : & tfprotov5.ResourceIdentityData {
99+ IdentityData : & testIdentityProto5DynamicValue ,
100+ },
101+ },
102+ resourceSchema : testFwSchema ,
103+ expected : & fwserver.ImportResourceStateRequest {
104+ EmptyState : testFwEmptyState ,
105+ },
106+ expectedDiagnostics : diag.Diagnostics {
107+ diag .NewErrorDiagnostic (
108+ "Unable to Convert Resource Identity" ,
109+ "An unexpected error was encountered when converting the resource identity from the protocol type. " +
110+ "Identity data was sent in the protocol to a resource that doesn't support identity.\n \n " +
111+ "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer." ,
112+ ),
113+ },
114+ },
115+ "identity" : {
116+ input : & tfprotov5.ImportResourceStateRequest {
117+ Identity : & tfprotov5.ResourceIdentityData {
118+ IdentityData : & testIdentityProto5DynamicValue ,
119+ },
120+ },
121+ resourceSchema : testFwSchema ,
122+ identitySchema : testIdentitySchema ,
123+ expected : & fwserver.ImportResourceStateRequest {
124+ EmptyState : testFwEmptyState ,
125+ IdentitySchema : testIdentitySchema ,
126+ Identity : & tfsdk.ResourceIdentity {
127+ Raw : testIdentityProto5Value ,
128+ Schema : testIdentitySchema ,
129+ },
130+ },
131+ },
70132 "id" : {
71133 input : & tfprotov5.ImportResourceStateRequest {
72134 ID : "test-id" ,
@@ -122,7 +184,7 @@ func TestImportResourceStateRequest(t *testing.T) {
122184 t .Run (name , func (t * testing.T ) {
123185 t .Parallel ()
124186
125- got , diags := fromproto5 .ImportResourceStateRequest (context .Background (), testCase .input , testCase .resource , testCase .resourceSchema )
187+ got , diags := fromproto5 .ImportResourceStateRequest (context .Background (), testCase .input , testCase .resource , testCase .resourceSchema , testCase . identitySchema )
126188
127189 if diff := cmp .Diff (got , testCase .expected ); diff != "" {
128190 t .Errorf ("unexpected difference: %s" , diff )
0 commit comments