Skip to content

Commit d979233

Browse files
committed
Addressed PR comments round 2
1 parent 1af31a2 commit d979233

19 files changed

+169
-186
lines changed

internal/fromproto5/upgraderesourceidentity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest
1717
// equivalent of a *tfprotov5.UpgradeResourceIdentityRequest.
18-
func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) {
18+
func UpgradeIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) {
1919
if proto5 == nil {
2020
return nil, nil
2121
}
@@ -36,7 +36,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.Upgra
3636
return nil, diags
3737
}
3838

39-
fw := &fwserver.UpgradeResourceIdentityRequest{
39+
fw := &fwserver.UpgradeIdentityRequest{
4040
RawState: (*tfprotov6.RawState)(proto5.RawIdentity),
4141
IdentitySchema: identitySchema,
4242
Resource: resource,

internal/fromproto5/upgraderesourceidentity_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
3333
input *tfprotov5.UpgradeResourceIdentityRequest
3434
identitySchema fwschema.Schema
3535
resource resource.Resource
36-
expected *fwserver.UpgradeResourceIdentityRequest
36+
expected *fwserver.UpgradeIdentityRequest
3737
expectedDiagnostics diag.Diagnostics
3838
}{
3939
"nil": {
@@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
4747
}),
4848
},
4949
identitySchema: testIdentitySchema,
50-
expected: &fwserver.UpgradeResourceIdentityRequest{
50+
expected: &fwserver.UpgradeIdentityRequest{
5151
RawState: testNewTfprotov6RawState(t, map[string]interface{}{
5252
"test_attribute": "test-value",
5353
}),
@@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
5757
"resourceschema": {
5858
input: &tfprotov5.UpgradeResourceIdentityRequest{},
5959
identitySchema: testIdentitySchema,
60-
expected: &fwserver.UpgradeResourceIdentityRequest{
60+
expected: &fwserver.UpgradeIdentityRequest{
6161
IdentitySchema: testIdentitySchema,
6262
},
6363
},
@@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
7979
Version: 123,
8080
},
8181
identitySchema: testIdentitySchema,
82-
expected: &fwserver.UpgradeResourceIdentityRequest{
82+
expected: &fwserver.UpgradeIdentityRequest{
8383
IdentitySchema: testIdentitySchema,
8484
Version: 123,
8585
},
@@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
9090
t.Run(name, func(t *testing.T) {
9191
t.Parallel()
9292

93-
got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
93+
got, diags := fromproto5.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
9494

9595
if diff := cmp.Diff(got, testCase.expected); diff != "" {
9696
t.Errorf("unexpected difference: %s", diff)

internal/fromproto6/upgraderesourceidentity.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1313
)
1414

15-
// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest
16-
// equivalent of a *tfprotov6.UpgradeResourceIdentityRequest.
17-
func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) {
15+
// UpgradeIdentityRequest returns the *fwserver.UpgradeIdentityRequest
16+
// equivalent of a *tfprotov6.UpgradeIdentityRequest.
17+
func UpgradeIdentityRequest(ctx context.Context, proto6 *tfprotov6.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeIdentityRequest, diag.Diagnostics) {
1818
if proto6 == nil {
1919
return nil, nil
2020
}
@@ -35,7 +35,7 @@ func UpgradeResourceIdentityRequest(ctx context.Context, proto6 *tfprotov6.Upgra
3535
return nil, diags
3636
}
3737

38-
fw := &fwserver.UpgradeResourceIdentityRequest{
38+
fw := &fwserver.UpgradeIdentityRequest{
3939
RawState: proto6.RawIdentity,
4040
IdentitySchema: identitySchema,
4141
Resource: resource,

internal/fromproto6/upgraderesourceidentity_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1919
)
2020

21-
func TestUpgradeResourceIdentityRequest(t *testing.T) {
21+
func TestUpgradeIdentityRequest(t *testing.T) {
2222
t.Parallel()
2323

2424
testIdentitySchema := identityschema.Schema{
@@ -33,7 +33,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
3333
input *tfprotov6.UpgradeResourceIdentityRequest
3434
identitySchema fwschema.Schema
3535
resource resource.Resource
36-
expected *fwserver.UpgradeResourceIdentityRequest
36+
expected *fwserver.UpgradeIdentityRequest
3737
expectedDiagnostics diag.Diagnostics
3838
}{
3939
"nil": {
@@ -47,7 +47,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
4747
}),
4848
},
4949
identitySchema: testIdentitySchema,
50-
expected: &fwserver.UpgradeResourceIdentityRequest{
50+
expected: &fwserver.UpgradeIdentityRequest{
5151
RawState: testNewRawState(t, map[string]interface{}{
5252
"test_attribute": "test-value",
5353
}),
@@ -57,7 +57,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
5757
"resourceschema": {
5858
input: &tfprotov6.UpgradeResourceIdentityRequest{},
5959
identitySchema: testIdentitySchema,
60-
expected: &fwserver.UpgradeResourceIdentityRequest{
60+
expected: &fwserver.UpgradeIdentityRequest{
6161
IdentitySchema: testIdentitySchema,
6262
},
6363
},
@@ -79,7 +79,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
7979
Version: 123,
8080
},
8181
identitySchema: testIdentitySchema,
82-
expected: &fwserver.UpgradeResourceIdentityRequest{
82+
expected: &fwserver.UpgradeIdentityRequest{
8383
IdentitySchema: testIdentitySchema,
8484
Version: 123,
8585
},
@@ -90,7 +90,7 @@ func TestUpgradeResourceIdentityRequest(t *testing.T) {
9090
t.Run(name, func(t *testing.T) {
9191
t.Parallel()
9292

93-
got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
93+
got, diags := fromproto6.UpgradeIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
9494

9595
if diff := cmp.Diff(got, testCase.expected); diff != "" {
9696
t.Errorf("unexpected difference: %s", diff)

internal/fwserver/server_upgraderesourceidentity.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1717
)
1818

19-
// UpgradeResourceIdentityRequest is the framework server request for the
20-
// UpgradeResourceIdentity RPC.
21-
type UpgradeResourceIdentityRequest struct {
19+
// UpgradeIdentityRequest is the framework server request for the
20+
// UpgradeIdentity RPC.
21+
type UpgradeIdentityRequest struct {
2222
Resource resource.Resource
2323
IdentitySchema fwschema.Schema
2424
// TypeName is the type of resource that Terraform needs to upgrade the
@@ -28,10 +28,6 @@ type UpgradeResourceIdentityRequest struct {
2828
// Version is the version of the identity state the resource currently has.
2929
Version int64
3030

31-
// Using the tfprotov6 type here was a pragmatic effort decision around when
32-
// the framework introduced compatibility promises. This type was chosen as
33-
// it was readily available and trivial to convert between tfprotov5.
34-
//
3531
// Using a terraform-plugin-go type is not ideal for the framework as almost
3632
// all terraform-plugin-go types have framework abstractions, but if there
3733
// is ever a time where it makes sense to re-evaluate this decision, such as
@@ -40,15 +36,15 @@ type UpgradeResourceIdentityRequest struct {
4036
RawState *tfprotov6.RawState
4137
}
4238

43-
// UpgradeResourceIdentityResponse is the framework server response for the
44-
// UpgradeResourceIdentity RPC.
45-
type UpgradeResourceIdentityResponse struct {
39+
// UpgradeIdentityResponse is the framework server response for the
40+
// UpgradeIdentity RPC.
41+
type UpgradeIdentityResponse struct {
4642
UpgradedIdentity *tfsdk.ResourceIdentity
4743
Diagnostics diag.Diagnostics
4844
}
4945

50-
// UpgradeResourceIdentity implements the framework server UpgradeResourceIdentity RPC.
51-
func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResourceIdentityRequest, resp *UpgradeResourceIdentityResponse) {
46+
// UpgradeIdentity implements the framework server UpgradeIdentity RPC.
47+
func (s *Server) UpgradeIdentity(ctx context.Context, req *UpgradeIdentityRequest, resp *UpgradeIdentityResponse) {
5248
if req == nil {
5349
return
5450
}
@@ -71,11 +67,10 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
7167

7268
if req.Version == req.IdentitySchema.GetVersion() {
7369
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",
70+
"Unexpected Identity Upgrade Request",
71+
"Terraform Core invoked UpgradeResourceIdentity even though the stored identity schema version matches the current version. "+
72+
"This likely indicates a bug in the Terraform provider framework or Terraform Core. "+
73+
"Please report this issue to the provider developer.",
7974
)
8075
return
8176
}
@@ -114,7 +109,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
114109
logging.FrameworkTrace(ctx, "Resource implements ResourceWithUpgradeIdentity")
115110

116111
logging.FrameworkTrace(ctx, "Calling provider defined Resource UpgradeIdentity")
117-
resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeResourceIdentity(ctx)
112+
resourceIdentityUpgraders := resourceWithUpgradeIdentity.UpgradeIdentity(ctx)
118113
logging.FrameworkTrace(ctx, "Called provider defined Resource UpgradeIdentity")
119114

120115
// Panic prevention
@@ -134,20 +129,20 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
134129
return
135130
}
136131

137-
upgradeResourceIdentityRequest := resource.UpgradeResourceIdentityRequest{
138-
RawState: req.RawState,
132+
upgradeResourceIdentityRequest := resource.UpgradeIdentityRequest{
133+
RawIdentity: req.RawState,
139134
}
140135

141136
if resourceIdentityUpgrader.PriorSchema != nil {
142-
logging.FrameworkTrace(ctx, "Initializing populated UpgradeResourceIdentityRequest Identity from provider defined prior schema and request RawState")
137+
logging.FrameworkTrace(ctx, "Initializing populated UpgradeIdentityRequest Identity from provider defined prior schema and request RawState")
143138

144139
priorSchemaType := resourceIdentityUpgrader.PriorSchema.Type().TerraformType(ctx)
145140

146141
rawIdentityValue, err := req.RawState.UnmarshalWithOpts(priorSchemaType, unmarshalOpts)
147142

148143
if err != nil {
149144
resp.Diagnostics.AddError(
150-
"Unable to Read Previously Saved Identity for UpgradeResourceIdentity",
145+
"Unable to Read Previously Saved Identity for UpgradeIdentity",
151146
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)+
152147
"Please report this to the provider developer:\n\n"+err.Error(),
153148
)
@@ -161,7 +156,7 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
161156

162157
}
163158

164-
upgradeResourceIdentityResponse := resource.UpgradeResourceIdentityResponse{
159+
upgradeResourceIdentityResponse := resource.UpgradeIdentityResponse{
165160
Identity: &tfsdk.ResourceIdentity{
166161
Schema: req.IdentitySchema,
167162
// Raw is intentionally not set.
@@ -193,16 +188,5 @@ func (s *Server) UpgradeResourceIdentity(ctx context.Context, req *UpgradeResour
193188
return
194189
}
195190

196-
if upgradeResourceIdentityResponse.Identity.Raw.Type() != nil {
197-
logging.FrameworkTrace(ctx, "UpgradeResourceIdentityResponse RawState set, overriding State")
198-
199-
resp.UpgradedIdentity = &tfsdk.ResourceIdentity{
200-
Schema: req.IdentitySchema,
201-
Raw: upgradeResourceIdentityResponse.Identity.Raw,
202-
}
203-
204-
return
205-
}
206-
207191
resp.UpgradedIdentity = upgradeResourceIdentityResponse.Identity
208192
}

0 commit comments

Comments
 (0)