@@ -145,6 +145,15 @@ func TestServerImportResourceState(t *testing.T) {
145145 Schema : testSchema ,
146146 }
147147
148+ testStatePassThroughIdentity := & tfsdk.State {
149+ Raw : tftypes .NewValue (testType , map [string ]tftypes.Value {
150+ "id" : tftypes .NewValue (tftypes .String , "id-123" ),
151+ "optional" : tftypes .NewValue (tftypes .String , nil ),
152+ "required" : tftypes .NewValue (tftypes .String , nil ),
153+ }),
154+ Schema : testSchema ,
155+ }
156+
148157 testImportedResourceIdentity := & tfsdk.ResourceIdentity {
149158 Raw : testImportedResourceIdentityValue ,
150159 Schema : testIdentitySchema ,
@@ -655,6 +664,122 @@ func TestServerImportResourceState(t *testing.T) {
655664 },
656665 },
657666 },
667+ "response-importedresources-passthrough-identity-imported-by-id" : {
668+ server : & fwserver.Server {
669+ Provider : & testprovider.Provider {},
670+ },
671+ request : & fwserver.ImportResourceStateRequest {
672+ EmptyState : * testEmptyState ,
673+ ID : "id-123" ,
674+ IdentitySchema : testIdentitySchema ,
675+ Resource : & testprovider.ResourceWithImportState {
676+ Resource : & testprovider.Resource {},
677+ ImportStateMethod : func (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
678+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("id" ), path .Root ("test_id" ), req , resp )
679+ },
680+ },
681+ TypeName : "test_resource" ,
682+ },
683+ expectedResponse : & fwserver.ImportResourceStateResponse {
684+ ImportedResources : []fwserver.ImportedResource {
685+ {
686+ State : * testStatePassThroughIdentity ,
687+ Identity : & tfsdk.ResourceIdentity {
688+ Raw : tftypes .NewValue (testIdentityType , nil ),
689+ Schema : testIdentitySchema ,
690+ },
691+ TypeName : "test_resource" ,
692+ Private : testEmptyPrivate ,
693+ },
694+ },
695+ },
696+ },
697+ "response-importedresources-passthrough-identity-imported-by-identity" : {
698+ server : & fwserver.Server {
699+ Provider : & testprovider.Provider {},
700+ },
701+ request : & fwserver.ImportResourceStateRequest {
702+ EmptyState : * testEmptyState ,
703+ Identity : testRequestIdentity ,
704+ IdentitySchema : testIdentitySchema ,
705+ Resource : & testprovider.ResourceWithImportState {
706+ Resource : & testprovider.Resource {},
707+ ImportStateMethod : func (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
708+ resp .Diagnostics .Append (resp .Identity .SetAttribute (ctx , path .Root ("other_test_id" ), types .StringValue ("new-value-123" ))... )
709+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("id" ), path .Root ("test_id" ), req , resp )
710+ },
711+ },
712+ TypeName : "test_resource" ,
713+ },
714+ expectedResponse : & fwserver.ImportResourceStateResponse {
715+ ImportedResources : []fwserver.ImportedResource {
716+ {
717+ State : * testStatePassThroughIdentity ,
718+ Identity : testImportedResourceIdentity ,
719+ TypeName : "test_resource" ,
720+ Private : testEmptyPrivate ,
721+ },
722+ },
723+ },
724+ },
725+ "response-importedresources-passthrough-identity-invalid-state-path" : {
726+ server : & fwserver.Server {
727+ Provider : & testprovider.Provider {},
728+ },
729+ request : & fwserver.ImportResourceStateRequest {
730+ EmptyState : * testEmptyState ,
731+ ID : "id-123" ,
732+ IdentitySchema : testIdentitySchema ,
733+ Resource : & testprovider.ResourceWithImportState {
734+ Resource : & testprovider.Resource {},
735+ ImportStateMethod : func (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
736+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("not-valid" ), path .Root ("test_id" ), req , resp )
737+ },
738+ },
739+ TypeName : "test_resource" ,
740+ },
741+ expectedResponse : & fwserver.ImportResourceStateResponse {
742+ Diagnostics : diag.Diagnostics {
743+ diag .NewAttributeErrorDiagnostic (
744+ path .Root ("not-valid" ),
745+ "State Write Error" ,
746+ "An unexpected error was encountered trying to retrieve type information at a given path. " +
747+ "This is always an error in the provider. Please report the following to the provider developer:\n \n " +
748+ "Error: AttributeName(\" not-valid\" ) still remains in the path: could not find attribute or block " +
749+ "\" not-valid\" in schema" ,
750+ ),
751+ },
752+ },
753+ },
754+ "response-importedresources-passthrough-identity-invalid-identity-path" : {
755+ server : & fwserver.Server {
756+ Provider : & testprovider.Provider {},
757+ },
758+ request : & fwserver.ImportResourceStateRequest {
759+ EmptyState : * testEmptyState ,
760+ Identity : testRequestIdentity ,
761+ IdentitySchema : testIdentitySchema ,
762+ Resource : & testprovider.ResourceWithImportState {
763+ Resource : & testprovider.Resource {},
764+ ImportStateMethod : func (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
765+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("id" ), path .Root ("not-valid" ), req , resp )
766+ },
767+ },
768+ TypeName : "test_resource" ,
769+ },
770+ expectedResponse : & fwserver.ImportResourceStateResponse {
771+ Diagnostics : diag.Diagnostics {
772+ diag .NewAttributeErrorDiagnostic (
773+ path .Root ("not-valid" ),
774+ "Resource Identity Read Error" ,
775+ "An unexpected error was encountered trying to retrieve type information at a given path. " +
776+ "This is always an error in the provider. Please report the following to the provider developer:\n \n " +
777+ "Error: AttributeName(\" not-valid\" ) still remains in the path: could not find attribute or block " +
778+ "\" not-valid\" in schema" ,
779+ ),
780+ },
781+ },
782+ },
658783 }
659784
660785 for name , testCase := range testCases {
0 commit comments