@@ -25,20 +25,6 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
2525 t .Parallel ()
2626
2727 ctx := context .Background ()
28- /* testSchema := schema.Schema{
29- Attributes: map[string]schema.Attribute{
30- "id": schema.StringAttribute{
31- Computed: true,
32- },
33- "optional_attribute": schema.StringAttribute{
34- Optional: true,
35- },
36- "required_attribute": schema.StringAttribute{
37- Required: true,
38- },
39- },
40- Version: 1, // Must be above 0
41- }*/
4228
4329 testIdentitySchema := identityschema.Schema {
4430 Attributes : map [string ]identityschema.Attribute {
@@ -108,38 +94,35 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
10894 // use it here. The fact that Configure is able to
10995 // read the data proves this can work.
11096
111- var priorIdentityData struct {
112- Id string `tfsdk:"id"`
113- }
114-
115- //resp.Diagnostics.Append(req.State.Get(ctx, &priorStateData)...) need to find the equivalent
116- // of this line for state for identity
117-
118- //rawStateValue, err := req.RawState.Unmarshal(schemaIdentityType)
119- // rawStateValue, err := req.RawState.Unmarshal(tftypes.Object{
120- // AttributeTypes: map[string]tftypes.Type{
121- // "id": tftypes.String,
122- // },
123- // })
124-
125- // if err != nil {
126- // resp.Diagnostics.AddError(
127- // "Unable to Read Previously Saved Identity for UpgradeResourceIdentity",
128- // fmt.Sprintf("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n\n", req.Version)+
129- // "Please report this to the provider developer:\n\n"+err.Error(),
130- // )
131- // }
132-
133- // priorIdentityData.Id = rawStateValue
97+ rawStateValue , err := req .RawState .Unmarshal (tftypes.Object {
98+ AttributeTypes : map [string ]tftypes.Type {
99+ "id" : tftypes .String ,
100+ },
101+ })
134102
135- if resp .Diagnostics .HasError () {
103+ if err != nil {
104+ resp .Diagnostics .AddError (
105+ "Unable to Read Previously Saved Identity for UpgradeResourceIdentity" ,
106+ fmt .Sprintf ("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n \n " , req .Version )+
107+ "Please report this to the provider developer:\n \n " + err .Error (),
108+ )
109+ return
110+ }
111+ rawValues := make (map [string ]tftypes.Value )
112+ err = rawStateValue .As (& rawValues )
113+ if err != nil {
114+ resp .Diagnostics .AddError (
115+ "Unable to convert raw state value into prior identity struct" ,
116+ fmt .Sprintf ("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n \n " , req .Version )+
117+ "Please report this to the provider developer:\n \n " + err .Error (),
118+ )
136119 return
137120 }
138121
139122 upgradedIdentityData := struct {
140123 Id string `tfsdk:"id"`
141124 }{
142- Id : priorIdentityData . Id ,
125+ Id : id ,
143126 }
144127
145128 resp .Diagnostics .Append (resp .UpgradedIdentity .Set (ctx , upgradedIdentityData )... )
@@ -153,8 +136,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
153136 expectedResponse : & fwserver.UpgradeResourceIdentityResponse {
154137 UpgradedIdentity : & tfsdk.ResourceIdentity {
155138 Raw : tftypes .NewValue (schemaIdentityType , map [string ]tftypes.Value {
156- //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value
157- "id" : tftypes .NewValue (tftypes .String , "" ),
139+ "id" : tftypes .NewValue (tftypes .String , "test-id-value" ),
158140 }),
159141 Schema : testIdentitySchema ,
160142 },
@@ -408,71 +390,49 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
408390 0 : {
409391 PriorSchema : & testIdentitySchema ,
410392 IdentityUpgrader : func (ctx context.Context , req resource.UpgradeResourceIdentityRequest , resp * resource.UpgradeResourceIdentityResponse ) {
411- var priorIdentityData struct {
412- Id string `tfsdk:"id"`
413- }
414-
415- // resp.Diagnostics.Append(req.Identity.Get(ctx, &priorIdentityData)... )
393+ rawStateValue , err := req . RawState . Unmarshal (tftypes. Object {
394+ AttributeTypes : map [ string ]tftypes. Type {
395+ "id" : tftypes . String ,
396+ },
397+ } )
416398
417- if resp .Diagnostics .HasError () {
399+ if err != nil {
400+ resp .Diagnostics .AddError (
401+ "Unable to Read Previously Saved Identity for UpgradeResourceIdentity" ,
402+ fmt .Sprintf ("There was an error reading the saved resource Identity using the prior resource schema defined for version %d upgrade.\n \n " , req .Version )+
403+ "Please report this to the provider developer:\n \n " + err .Error (),
404+ )
418405 return
419406 }
420-
421- upgradedIdentityData := struct {
422- Id string `tfsdk:"id"`
423- }{
424- Id : priorIdentityData .Id ,
425- }
426-
427- resp .Diagnostics .Append (resp .UpgradedIdentity .Set (ctx , upgradedIdentityData )... )
428- },
429- },
430- }
431- },
432- },
433- Version : 0 ,
434- },
435- expectedResponse : & fwserver.UpgradeResourceIdentityResponse {
436- UpgradedIdentity : & tfsdk.ResourceIdentity {
437- Raw : tftypes .NewValue (schemaIdentityType , map [string ]tftypes.Value {
438- //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value
439- "id" : tftypes .NewValue (tftypes .String , "" ),
440- }),
441- Schema : testIdentitySchema ,
442- },
443- },
444- },
445- "PriorSchema-and-Identity-json-mismatch" : {
446- server : & fwserver.Server {
447- Provider : & testprovider.Provider {},
448- },
449- request : & fwserver.UpgradeResourceIdentityRequest {
450- RawState : testNewRawState (t , map [string ]interface {}{
451- "id" : "test-id-value" ,
452- "nonexistent_attribute" : "value" ,
453- }),
454- IdentitySchema : testIdentitySchema ,
455- Resource : & testprovider.ResourceWithUpgradeIdentity {
456- Resource : & testprovider.Resource {},
457- UpgradeResourceIdentityMethod : func (ctx context.Context ) map [int64 ]resource.IdentityUpgrader {
458- return map [int64 ]resource.IdentityUpgrader {
459- 0 : {
460- PriorSchema : & testIdentitySchema ,
461- IdentityUpgrader : func (ctx context.Context , req resource.UpgradeResourceIdentityRequest , resp * resource.UpgradeResourceIdentityResponse ) {
462- var priorIdentityData struct {
463- Id string `tfsdk:"id"`
407+ rawValues := make (map [string ]tftypes.Value )
408+ err = rawStateValue .As (& rawValues )
409+ if err != nil {
410+ resp .Diagnostics .AddError (
411+ "Unable to convert raw state value into prior identity struct" ,
412+ fmt .Sprintf ("There was an error converting the raw state value into the prior resource identity struct for version %d upgrade.\n \n " , req .Version )+
413+ "Please report this to the provider developer:\n \n " + err .Error (),
414+ )
415+ return
464416 }
465417
466- // resp.Diagnostics.Append(req.RawState.Unmarshal())
467-
468- if resp .Diagnostics .HasError () {
469- return
418+ priorIdentityId := rawValues ["id" ]
419+ var id string
420+ if priorIdentityId .Type ().Is (tftypes .String ) {
421+ err := priorIdentityId .As (& id )
422+ if err != nil {
423+ resp .Diagnostics .AddError (
424+ "Unable to convert raw state id value into string" ,
425+ fmt .Sprintf ("There was an error converting the raw state id value into string for version %d upgrade.\n \n " , req .Version )+
426+ "Please report this to the provider developer:\n \n " + err .Error (),
427+ )
428+ return
429+ }
470430 }
471431
472432 upgradedIdentityData := struct {
473433 Id string `tfsdk:"id"`
474434 }{
475- Id : priorIdentityData . Id ,
435+ Id : id ,
476436 }
477437
478438 resp .Diagnostics .Append (resp .UpgradedIdentity .Set (ctx , upgradedIdentityData )... )
@@ -486,8 +446,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
486446 expectedResponse : & fwserver.UpgradeResourceIdentityResponse {
487447 UpgradedIdentity : & tfsdk.ResourceIdentity {
488448 Raw : tftypes .NewValue (schemaIdentityType , map [string ]tftypes.Value {
489- //"id": tftypes.NewValue(tftypes.String, "test-id-value"), // TODO: make test pass with value
490- "id" : tftypes .NewValue (tftypes .String , "" ),
449+ "id" : tftypes .NewValue (tftypes .String , "test-id-value" ),
491450 }),
492451 Schema : testIdentitySchema ,
493452 },
@@ -527,18 +486,23 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
527486 },
528487 },
529488 },
530- /*"Version-current-flatmap": { // TODO: add tests back
489+ /*"Version-current-flatmap": { // TODO: See if we need to add these tests back
531490 server: &fwserver.Server{
532491 Provider: &testprovider.Provider{
533492 ResourcesMethod: func(_ context.Context) []func() resource.Resource {
534493 return []func() resource.Resource{
535494 func() resource.Resource {
536- return &testprovider.Resource{
537- SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
538- resp.Schema = testSchema
539- },
540- MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
541- resp.TypeName = "test_resource"
495+ return &testprovider.ResourceWithUpgradeIdentity{
496+
497+ Resource: &testprovider.Resource{},
498+ UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader {
499+ return map[int64]resource.IdentityUpgrader{
500+ 0: {
501+ IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
502+ // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity
503+ },
504+ },
505+ }
542506 },
543507 }
544508 },
@@ -553,7 +517,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
553517 },
554518 },
555519 IdentitySchema: testIdentitySchema,
556- Resource: &testprovider.Resource {},
520+ Resource: &testprovider.ResourceWithUpgradeIdentity {},
557521 Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation
558522 },
559523 expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
@@ -575,12 +539,16 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
575539 ResourcesMethod: func(_ context.Context) []func() resource.Resource {
576540 return []func() resource.Resource{
577541 func() resource.Resource {
578- return &testprovider.Resource{
579- SchemaMethod: func(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
580- resp.Schema = testSchema
581- },
582- MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
583- resp.TypeName = "test_resource"
542+ return &testprovider.ResourceWithUpgradeIdentity{
543+ Resource: &testprovider.Resource{},
544+ UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader {
545+ return map[int64]resource.IdentityUpgrader{
546+ 0: {
547+ IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
548+ // Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity
549+ },
550+ },
551+ }
584552 },
585553 }
586554 },
@@ -593,7 +561,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
593561 "id": "test-id-value",
594562 }),
595563 IdentitySchema: testIdentitySchema,
596- Resource: &testprovider.Resource {},
564+ Resource: &testprovider.ResourceWithUpgradeIdentity {},
597565 Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation
598566 },
599567 expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
@@ -615,7 +583,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
615583 "nonexistent_attribute": "value",
616584 }),
617585 IdentitySchema: testIdentitySchema,
618- Resource: &testprovider.Resource {},
586+ Resource: &testprovider.ResourceWithUpgradeIdentity {},
619587 Version: 1, // Must match current tfsdk.IdentitySchema version to trigger framework implementation
620588 },
621589 expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
0 commit comments