@@ -18,6 +18,7 @@ import (
1818 "github.com/hashicorp/terraform-plugin-framework/provider"
1919 "github.com/hashicorp/terraform-plugin-framework/provider/metaschema"
2020 "github.com/hashicorp/terraform-plugin-framework/resource"
21+ "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
2122 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
2223 "github.com/hashicorp/terraform-plugin-framework/types"
2324)
@@ -34,6 +35,20 @@ func TestServerApplyResourceChange(t *testing.T) {
3435
3536 testEmptyDynamicValue , _ := tfprotov6 .NewDynamicValue (testSchemaType , tftypes .NewValue (testSchemaType , nil ))
3637
38+ testIdentityType := tftypes.Object {
39+ AttributeTypes : map [string ]tftypes.Type {
40+ "test_id" : tftypes .String ,
41+ },
42+ }
43+
44+ testPlannedIdentityValue := testNewDynamicValue (t , testIdentityType , map [string ]tftypes.Value {
45+ "test_id" : tftypes .NewValue (tftypes .String , "id-123" ),
46+ })
47+
48+ testNewIdentityDynamicValue := testNewDynamicValue (t , testIdentityType , map [string ]tftypes.Value {
49+ "test_id" : tftypes .NewValue (tftypes .String , "new-id-123" ),
50+ })
51+
3752 testSchema := schema.Schema {
3853 Attributes : map [string ]schema.Attribute {
3954 "test_computed" : schema.StringAttribute {
@@ -45,6 +60,14 @@ func TestServerApplyResourceChange(t *testing.T) {
4560 },
4661 }
4762
63+ testIdentitySchema := identityschema.Schema {
64+ Attributes : map [string ]identityschema.Attribute {
65+ "test_id" : identityschema.StringAttribute {
66+ RequiredForImport : true ,
67+ },
68+ },
69+ }
70+
4871 type testSchemaData struct {
4972 TestComputed types.String `tfsdk:"test_computed"`
5073 TestRequired types.String `tfsdk:"test_required"`
@@ -194,6 +217,79 @@ func TestServerApplyResourceChange(t *testing.T) {
194217 }),
195218 },
196219 },
220+ "create-request-plannedidentity" : {
221+ server : & Server {
222+ FrameworkServer : fwserver.Server {
223+ Provider : & testprovider.Provider {
224+ ResourcesMethod : func (_ context.Context ) []func () resource.Resource {
225+ return []func () resource.Resource {
226+ func () resource.Resource {
227+ return & testprovider.ResourceWithIdentity {
228+ Resource : & testprovider.Resource {
229+ SchemaMethod : func (_ context.Context , _ resource.SchemaRequest , resp * resource.SchemaResponse ) {
230+ resp .Schema = testSchema
231+ },
232+ MetadataMethod : func (_ context.Context , _ resource.MetadataRequest , resp * resource.MetadataResponse ) {
233+ resp .TypeName = "test_resource"
234+ },
235+ CreateMethod : func (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
236+ var identityData struct {
237+ TestID types.String `tfsdk:"test_id"`
238+ }
239+
240+ resp .Diagnostics .Append (req .Identity .Get (ctx , & identityData )... )
241+
242+ if identityData .TestID .ValueString () != "id-123" {
243+ resp .Diagnostics .AddError ("Unexpected req.Identity" , identityData .TestID .ValueString ())
244+ }
245+
246+ // Prevent missing resource state error diagnostic
247+ var data testSchemaData
248+ resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
249+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
250+ },
251+ DeleteMethod : func (_ context.Context , _ resource.DeleteRequest , resp * resource.DeleteResponse ) {
252+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
253+ },
254+ UpdateMethod : func (_ context.Context , _ resource.UpdateRequest , resp * resource.UpdateResponse ) {
255+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Update" )
256+ },
257+ },
258+ IdentitySchemaMethod : func (ctx context.Context , req resource.IdentitySchemaRequest , resp * resource.IdentitySchemaResponse ) {
259+ resp .IdentitySchema = testIdentitySchema
260+ },
261+ }
262+ },
263+ }
264+ },
265+ },
266+ },
267+ },
268+ request : & tfprotov6.ApplyResourceChangeRequest {
269+ Config : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
270+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
271+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
272+ }),
273+ PlannedState : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
274+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
275+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
276+ }),
277+ PlannedIdentity : & tfprotov6.ResourceIdentityData {
278+ IdentityData : testPlannedIdentityValue ,
279+ },
280+ PriorState : & testEmptyDynamicValue ,
281+ TypeName : "test_resource" ,
282+ },
283+ expectedResponse : & tfprotov6.ApplyResourceChangeResponse {
284+ NewState : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
285+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
286+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
287+ }),
288+ NewIdentity : & tfprotov6.ResourceIdentityData {
289+ IdentityData : testPlannedIdentityValue ,
290+ },
291+ },
292+ },
197293 "create-request-providermeta" : {
198294 server : & Server {
199295 FrameworkServer : fwserver.Server {
@@ -372,6 +468,73 @@ func TestServerApplyResourceChange(t *testing.T) {
372468 }),
373469 },
374470 },
471+ "create-response-newidentity" : {
472+ server : & Server {
473+ FrameworkServer : fwserver.Server {
474+ Provider : & testprovider.Provider {
475+ ResourcesMethod : func (_ context.Context ) []func () resource.Resource {
476+ return []func () resource.Resource {
477+ func () resource.Resource {
478+ return & testprovider.ResourceWithIdentity {
479+ Resource : & testprovider.Resource {
480+ SchemaMethod : func (_ context.Context , _ resource.SchemaRequest , resp * resource.SchemaResponse ) {
481+ resp .Schema = testSchema
482+ },
483+ MetadataMethod : func (_ context.Context , _ resource.MetadataRequest , resp * resource.MetadataResponse ) {
484+ resp .TypeName = "test_resource"
485+ },
486+ CreateMethod : func (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
487+ identityData := struct {
488+ TestID types.String `tfsdk:"test_id"`
489+ }{
490+ TestID : types .StringValue ("new-id-123" ),
491+ }
492+ resp .Diagnostics .Append (resp .Identity .Set (ctx , identityData )... )
493+
494+ var data testSchemaData
495+
496+ resp .Diagnostics .Append (req .Plan .Get (ctx , & data )... )
497+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
498+ },
499+ DeleteMethod : func (_ context.Context , _ resource.DeleteRequest , resp * resource.DeleteResponse ) {
500+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Delete" )
501+ },
502+ UpdateMethod : func (_ context.Context , _ resource.UpdateRequest , resp * resource.UpdateResponse ) {
503+ resp .Diagnostics .AddError ("Unexpected Method Call" , "Expected: Create, Got: Update" )
504+ },
505+ },
506+ IdentitySchemaMethod : func (ctx context.Context , req resource.IdentitySchemaRequest , resp * resource.IdentitySchemaResponse ) {
507+ resp .IdentitySchema = testIdentitySchema
508+ },
509+ }
510+ },
511+ }
512+ },
513+ },
514+ },
515+ },
516+ request : & tfprotov6.ApplyResourceChangeRequest {
517+ Config : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
518+ "test_computed" : tftypes .NewValue (tftypes .String , nil ),
519+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
520+ }),
521+ PlannedState : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
522+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
523+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
524+ }),
525+ PriorState : & testEmptyDynamicValue ,
526+ TypeName : "test_resource" ,
527+ },
528+ expectedResponse : & tfprotov6.ApplyResourceChangeResponse {
529+ NewIdentity : & tfprotov6.ResourceIdentityData {
530+ IdentityData : testNewIdentityDynamicValue ,
531+ },
532+ NewState : testNewDynamicValue (t , testSchemaType , map [string ]tftypes.Value {
533+ "test_computed" : tftypes .NewValue (tftypes .String , "test-plannedstate-value" ),
534+ "test_required" : tftypes .NewValue (tftypes .String , "test-config-value" ),
535+ }),
536+ },
537+ },
375538 "create-response-newstate-null" : {
376539 server : & Server {
377540 FrameworkServer : fwserver.Server {
0 commit comments