Skip to content

Commit c1ee69d

Browse files
committed
Addressed PR commenta
1 parent cb52c33 commit c1ee69d

9 files changed

+56
-194
lines changed

internal/fwserver/server_upgraderesourceidentity.go

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,16 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
6969
},
7070
}
7171

72-
// TODO: Maybe throw error if the schemas are the same, it is a bug in core
73-
74-
// Terraform CLI can call UpgradeResourceIdentity even if the stored Identity
75-
// version matches the current schema. Presumably this is to account for
76-
// the previous terraform-plugin-sdk implementation, which handled some
77-
// Identity fixups on behalf of Terraform CLI. When this happens, we do not
78-
// want to return errors for a missing ResourceWithUpgradeIdentity
79-
// implementation or an undefined version within an existing
80-
// ResourceWithUpgradeIdentity implementation as that would be confusing
81-
// detail for provider developers. Instead, the framework will attempt to
82-
// roundtrip the prior RawState to a Identity matching the current Schema.
83-
//
84-
// TODO: To prevent provider developers from accidentally implementing
85-
// ResourceWithUpgradeIdentity with a version matching the current schema
86-
// version which would never get called, the framework can introduce a
87-
// unit test helper.
88-
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/113
89-
//
90-
// UnmarshalWithOpts allows optionally ignoring instances in which elements being
91-
// do not have a corresponding attribute within the schema.
92-
/* if req.Version == req.IdentitySchema.GetVersion() {
93-
logging.FrameworkTrace(ctx, "UpgradeResourceIdentity request version matches current Schema version, using framework defined passthrough implementation")
94-
95-
identitySchemaType := req.IdentitySchema.Type().TerraformType(ctx)
96-
97-
rawStateValue, err := req.RawState.UnmarshalWithOpts(identitySchemaType, unmarshalOpts)
98-
99-
if err != nil {
100-
resp.Diagnostics.AddError(
101-
"Unable to Read Previously Saved Identity for UpgradeResourceIdentity",
102-
"There was an error reading the saved resource Identity using the current resource schema.\n\n"+
103-
"If this resource Identity was last refreshed with Terraform CLI 0.11 and earlier, it must be refreshed or applied with an older provider version first. "+
104-
"If you manually modified the resource Identity, you will need to manually modify it to match the current resource schema. "+
105-
"Otherwise, please report this to the provider developer:\n\n"+err.Error(),
106-
)
107-
return
108-
}
109-
110-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
111-
Schema: req.IdentitySchema,
112-
Raw: rawStateValue,
113-
}
114-
72+
if req.Version == req.IdentitySchema.GetVersion() {
73+
resp.Diagnostics.AddError(
74+
"Unable to Read Previously Saved Identity for UpgradeResourceIdentity",
75+
"There was an error reading the saved resource identity using the current resource identity schema.\n\n"+
76+
"If this resource Identity was last refreshed with Terraform CLI 0.11 and earlier, it must be refreshed or applied with an older provider version first. "+
77+
"If you manually modified the resource Identity, you will need to manually modify it to match the current resource identity schema. "+
78+
"Otherwise, please report this to the provider developer:\n\n",
79+
)
11580
return
116-
}*/
81+
}
11782

11883
if resourceWithConfigure, ok := req.Resource.(resource.ResourceWithConfigure); ok {
11984
logging.FrameworkTrace(ctx, "Resource implements ResourceWithConfigure")
@@ -178,7 +143,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
178143

179144
priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx)
180145

181-
_, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts)
146+
rawIdentityValue, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts)
182147

183148
if err != nil {
184149
resp.Diagnostics.AddError(
@@ -189,12 +154,15 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
189154
return
190155
}
191156

192-
upgradeResourceIdentityRequest.RawState = req.RawState
157+
upgradeResourceIdentityRequest.Identity = &tfsdk.ResourceIdentity{
158+
Raw: rawIdentityValue, // from the output of req.RawState.UnmarshalWithOpts
159+
Schema: *resourceIdentityUpgrader.PriorSchema,
160+
}
193161

194162
}
195163

196164
upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{
197-
UpgradedIdentity: &tfsdk.ResourceIdentity{
165+
Identity: &tfsdk.ResourceIdentity{
198166
Schema: req.IdentitySchema,
199167
// Raw is intentionally not set.
200168
},
@@ -215,7 +183,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
215183
return
216184
}
217185

218-
if upgradeResourceIdentityResponse.UpgradedIdentity.Raw.Type() == nil || upgradeResourceIdentityResponse.UpgradedIdentity.Raw.IsNull() {
186+
if upgradeResourceIdentityResponse.Identity.Raw.Type() == nil || upgradeResourceIdentityResponse.Identity.Raw.IsNull() {
219187
resp.Diagnostics.AddError(
220188
"Missing Upgraded Resource Identity",
221189
fmt.Sprintf("After attempting a resource Identity upgrade to version %d, the provider did not return any Identity data. ", req.Version)+
@@ -225,16 +193,16 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
225193
return
226194
}
227195

228-
if upgradeResourceIdentityResponse.UpgradedIdentity != nil {
229-
logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse Raw State set, overriding State")
196+
if upgradeResourceIdentityResponse.Identity.Raw.Type() != nil {
197+
logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse RawState set, overriding State")
230198

231199
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
232200
Schema: req.IdentitySchema,
233-
Raw: upgradeResourceIdentityResponse.UpgradedIdentity.Raw,
201+
Raw: upgradeResourceIdentityResponse.Identity.Raw,
234202
}
235203

236204
return
237205
}
238206

239-
resp.UpgradedIdentity = upgradeResourceIdentityResponse.UpgradedIdentity
207+
resp.UpgradedIdentity = upgradeResourceIdentityResponse.Identity
240208
}

internal/fwserver/server_upgraderesourceidentity_test.go

Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
139139
Id: id,
140140
}
141141

142-
resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...)
142+
resp.Diagnostics.Append(resp.Identity.Set(ctx, upgradedIdentityData)...)
143143
},
144144
},
145145
}
@@ -221,7 +221,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
221221
return
222222
}
223223

224-
resp.UpgradedIdentity = ResourceIdentity
224+
resp.Identity = ResourceIdentity
225225
},
226226
},
227227
}
@@ -273,7 +273,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
273273
Schema: testIdentitySchema,
274274
}
275275

276-
resp.UpgradedIdentity = &ResourceIdentity
276+
resp.Identity = &ResourceIdentity
277277
},
278278
},
279279
}
@@ -449,7 +449,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
449449
Id: id,
450450
}
451451

452-
resp.Diagnostics.Append(resp.UpgradedIdentity.Set(ctx, upgradedIdentityData)...)
452+
resp.Diagnostics.Append(resp.Identity.Set(ctx, upgradedIdentityData)...)
453453
},
454454
},
455455
}
@@ -500,115 +500,6 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
500500
},
501501
},
502502
},
503-
/*"Version-current-flatmap": { // TODO: See if we need to add these tests back
504-
server: &fwserver.Server{
505-
Provider: &testprovider.Provider{
506-
ResourcesMethod: func(_ context.Context) []func() resource.Resource {
507-
return []func() resource.Resource{
508-
func() resource.Resource {
509-
return &testprovider.ResourceWithUpgradeIdentity{
510-
511-
Resource: &testprovider.Resource{},
512-
UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader {
513-
return map[int64]resource.IdentityUpgrader{
514-
0: {
515-
IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
516-
// Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity
517-
},
518-
},
519-
}
520-
},
521-
}
522-
},
523-
}
524-
},
525-
},
526-
},
527-
request: &fwserver.UpgradeResourceIdentityRequest{
528-
RawState: &tfprotov6.RawState{
529-
Flatmap: map[string]string{
530-
"flatmap": "is not supported",
531-
},
532-
},
533-
IdentitySchema: testIdentitySchema,
534-
Resource: &testprovider.ResourceWithUpgradeIdentity{},
535-
Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation
536-
},
537-
expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
538-
Diagnostics: diag.Diagnostics{
539-
diag.NewErrorDiagnostic(
540-
"Unable to Read Previously Saved Identity for UpgradeResourceIdentity",
541-
"There was an error reading the saved resource Identity using the current resource schema.\n\n"+
542-
"If this resource Identity was last refreshed with Terraform CLI 0.11 and earlier, it must be refreshed or applied with an older provider version first. "+
543-
"If you manually modified the resource Identity, you will need to manually modify it to match the current resource schema. "+
544-
"Otherwise, please report this to the provider developer:\n\n"+
545-
"flatmap Identitys cannot be unmarshaled, only Identitys written by Terraform 0.12 and higher can be unmarshaled",
546-
),
547-
},
548-
},
549-
},
550-
"Version-current-json-match": {
551-
server: &fwserver.Server{
552-
Provider: &testprovider.Provider{
553-
ResourcesMethod: func(_ context.Context) []func() resource.Resource {
554-
return []func() resource.Resource{
555-
func() resource.Resource {
556-
return &testprovider.ResourceWithUpgradeIdentity{
557-
Resource: &testprovider.Resource{},
558-
UpgradeResourceIdentityMethod: func(ctx context.Context) map[int64]resource.IdentityUpgrader {
559-
return map[int64]resource.IdentityUpgrader{
560-
0: {
561-
IdentityUpgrader: func(ctx context.Context, req resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
562-
// Purposfully not setting resp.ResourceIdentity or resp.UpgradedIdentity
563-
},
564-
},
565-
}
566-
},
567-
}
568-
},
569-
}
570-
},
571-
},
572-
},
573-
request: &fwserver.UpgradeResourceIdentityRequest{
574-
RawState: testNewRawState(t, map[string]interface{}{
575-
"id": "test-id-value",
576-
}),
577-
IdentitySchema: testIdentitySchema,
578-
Resource: &testprovider.ResourceWithUpgradeIdentity{},
579-
Version: 1, // Must match current tfsdk.Schema version to trigger framework implementation
580-
},
581-
expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
582-
UpgradedIdentity: &tfsdk.ResourceIdentity{
583-
Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{
584-
"id": tftypes.NewValue(tftypes.String, "test-id-value"),
585-
}),
586-
Schema: testIdentitySchema,
587-
},
588-
},
589-
},
590-
"Version-current-json-mismatch": {
591-
server: &fwserver.Server{
592-
Provider: &testprovider.Provider{},
593-
},
594-
request: &fwserver.UpgradeResourceIdentityRequest{
595-
RawState: testNewRawState(t, map[string]interface{}{
596-
"id": "test-id-value",
597-
"nonexistent_attribute": "value",
598-
}),
599-
IdentitySchema: testIdentitySchema,
600-
Resource: &testprovider.ResourceWithUpgradeIdentity{},
601-
Version: 1, // Must match current tfsdk.IdentitySchema version to trigger framework implementation
602-
},
603-
expectedResponse: &fwserver.UpgradeResourceIdentityResponse{
604-
UpgradedIdentity: &tfsdk.ResourceIdentity{
605-
Raw: tftypes.NewValue(schemaIdentityType, map[string]tftypes.Value{
606-
"id": tftypes.NewValue(tftypes.String, "test-id-value"),
607-
}),
608-
Schema: testIdentitySchema,
609-
},
610-
},
611-
},*/
612503
"Version-not-implemented": {
613504
server: &fwserver.Server{
614505
Provider: &testprovider.Provider{},

internal/proto5server/server_upgraderesourceidentity.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
// UpgradeResourceIdentity satisfies the tfprotov5.ProviderServer interface.
1717
func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto5Req *tfprotov5.UpgradeResourceIdentityRequest) (*tfprotov5.UpgradeResourceIdentityResponse, error) {
18-
// panic("unimplemented") // TODO:ResourceIdentity: implement
1918
ctx = s.registerContext(ctx)
2019
ctx = logging.InitContext(ctx)
2120

internal/proto5server/server_upgraderesourceidentity_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
4242
RequiredForImport: true,
4343
},
4444
},
45+
Version: 1, // Must be above 0
4546
}
4647

4748
ctx := context.Background()
@@ -91,7 +92,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
9192
}
9293

9394
// Prevent Missing Upgraded Resource Identity error
94-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
95+
resp.Identity = &tfsdk.ResourceIdentity{
9596
Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{
9697
"test_id": tftypes.NewValue(tftypes.String, "test-id-value"),
9798
}),
@@ -241,7 +242,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
241242
return map[int64]resource.IdentityUpgrader{
242243
0: {
243244
IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
244-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
245+
resp.Identity = &tfsdk.ResourceIdentity{
245246
Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{
246247
"test_id": tftypes.NewValue(tftypes.String, "test-id-value"),
247248
}),

internal/proto6server/server_upgraderesourceidentity.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
// UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface.
1717
func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) {
18-
// panic("unimplemented") // TODO:ResourceIdentity: implement
1918
ctx = s.registerContext(ctx)
2019
ctx = logging.InitContext(ctx)
2120

internal/proto6server/server_upgraderesourceidentity_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
4242
RequiredForImport: true,
4343
},
4444
},
45+
Version: 1, // Must be above 0
4546
}
4647

4748
ctx := context.Background()
@@ -91,7 +92,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
9192
}
9293

9394
// Prevent Missing Upgraded Resource Identity error
94-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
95+
resp.Identity = &tfsdk.ResourceIdentity{
9596
Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{
9697
"test_id": tftypes.NewValue(tftypes.String, "test-id-value"),
9798
}),
@@ -241,7 +242,7 @@ func TestServerUpgradeResourceIdentity(t *testing.T) {
241242
return map[int64]resource.IdentityUpgrader{
242243
0: {
243244
IdentityUpgrader: func(_ context.Context, _ resource.UpgradeResourceIdentityRequest, resp *resource.UpgradeResourceIdentityResponse) {
244-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
245+
resp.Identity = &tfsdk.ResourceIdentity{
245246
Raw: tftypes.NewValue(testIdentityType, map[string]tftypes.Value{
246247
"test_id": tftypes.NewValue(tftypes.String, "test-id-value"),
247248
}),

resource/identity_upgrader.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,39 @@ import (
99
"github.com/hashicorp/terraform-plugin-go/tftypes"
1010
)
1111

12-
// Implementation handler for a UpgradeState operation.
12+
// Implementation handler for an UpgradeIdentity operation.
1313
//
14-
// This is used to encapsulate all upgrade logic from a prior state to the
15-
// current schema version when a Resource implements the
16-
// ResourceWithUpgradeState interface.
14+
// This is used to encapsulate all upgrade logic from a prior identity to the
15+
// current version when a Resource implements the
16+
// ResourceWithUpgradeIdentity interface.
1717
type IdentityUpgrader struct {
18-
// Schema information for the prior state version. While not required,
19-
// setting this will populate the UpgradeStateRequest type State
18+
// Schema information for the prior identity version. While not required,
19+
// setting this will populate the UpgradeResourceIdentityRequest type Identity
2020
// field similar to other Resource data types. This allows for easier data
2121
// handling such as calling Get() or GetAttribute().
2222
//
23-
// If not set, prior state data is available in the
24-
// UpgradeResourceStateRequest type RawState field.
23+
// If not set, prior identity data is available in the
24+
// UpgradeResourceIdentityRequest type RawIdentity field.
2525
PriorSchema *identityschema.Schema
2626

2727
// Version is the version schema that this Upgrader will handle, converting
2828
// it to Version+1.
2929
Version int64
3030

31-
// Type describes the schema that this function can upgrade. Type is
32-
// required to decode the schema if the state was stored in a legacy
33-
// flatmap format.
31+
// Type describes the schema that this function can upgrade.
3432
Type tftypes.Type
3533

36-
// Provider defined logic for upgrading a resource state from the prior
37-
// state version to the current schema version.
34+
// Provider defined logic for upgrading a resource identity from the prior
35+
// identity version to the current schema version.
3836
//
3937
// The context.Context parameter contains framework-defined loggers and
4038
// supports request cancellation.
4139
//
42-
// The UpgradeStateRequest parameter contains the prior state data.
43-
// If PriorSchema was set, the State field will be available. Otherwise,
44-
// the RawState must be used.
40+
// The UpgradeResourceIdentityRequest parameter contains the prior identity data.
41+
// If PriorSchema was set, the Identity field will be available. Otherwise,
42+
// the RawIdentity must be used.
4543
//
46-
// The UpgradeStateResponse parameter should contain the upgraded
47-
// state data and can be used to signal any logic warnings or errors.
44+
// The UpgradeResourceIdentityResponse parameter should contain the upgraded
45+
// identity data and can be used to signal any logic warnings or errors.
4846
IdentityUpgrader func(context.Context, UpgradeResourceIdentityRequest, *UpgradeResourceIdentityResponse)
4947
}

0 commit comments

Comments
 (0)