Skip to content

Commit e1d72e4

Browse files
authored
Resource Identity: Add the UpgradeRPC for resource identity (#1135)
* Adding the upgradeRPC for resource identity. Still requires some tests. * Updated tests in server_upgraderesourceidentity_test.go * Updated tests in server_upgraderesourceidentity_test.go * Updated tests in server_upgraderesourceidentity_test.go * Addressed PR commenta * Addressed PR comments round 2 * Reverting the renaming for now * Testing renaming again * Renaming to UpgradeIdentity confirmed to fail corner tests * Renaming just for the resource * Renaming the method and interface just for the resource
1 parent 5c1ab2d commit e1d72e4

19 files changed

+2258
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto5
5+
6+
import (
7+
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
11+
"github.com/hashicorp/terraform-plugin-framework/resource"
12+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
)
15+
16+
// UpgradeResourceIdentityRequest returns the *fwserver.UpgradeResourceIdentityRequest
17+
// equivalent of a *tfprotov5.UpgradeResourceIdentityRequest.
18+
func UpgradeResourceIdentityRequest(ctx context.Context, proto5 *tfprotov5.UpgradeResourceIdentityRequest, resource resource.Resource, identitySchema fwschema.Schema) (*fwserver.UpgradeResourceIdentityRequest, diag.Diagnostics) {
19+
if proto5 == nil {
20+
return nil, nil
21+
}
22+
23+
var diags diag.Diagnostics
24+
25+
// Panic prevention here to simplify the calling implementations.
26+
// This should not happen, but just in case.
27+
if identitySchema == nil {
28+
diags.AddError(
29+
"Unable to Create Empty Identity",
30+
"An unexpected error was encountered when creating the empty Identity. "+
31+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
32+
"Please report this to the provider developer:\n\n"+
33+
"Missing schema.",
34+
)
35+
36+
return nil, diags
37+
}
38+
39+
fw := &fwserver.UpgradeResourceIdentityRequest{
40+
RawState: (*tfprotov6.RawState)(proto5.RawIdentity),
41+
IdentitySchema: identitySchema,
42+
Resource: resource,
43+
Version: proto5.Version,
44+
}
45+
46+
return fw, diags
47+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto5_test
5+
6+
import (
7+
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
9+
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/hashicorp/terraform-plugin-framework/diag"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
15+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
16+
"github.com/hashicorp/terraform-plugin-framework/resource"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
18+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
19+
)
20+
21+
func TestUpgradeResourceIdentityRequest(t *testing.T) {
22+
t.Parallel()
23+
24+
testIdentitySchema := identityschema.Schema{
25+
Attributes: map[string]identityschema.Attribute{
26+
"test_attribute": schema.StringAttribute{
27+
Required: true,
28+
},
29+
},
30+
}
31+
32+
testCases := map[string]struct {
33+
input *tfprotov5.UpgradeResourceIdentityRequest
34+
identitySchema fwschema.Schema
35+
resource resource.Resource
36+
expected *fwserver.UpgradeResourceIdentityRequest
37+
expectedDiagnostics diag.Diagnostics
38+
}{
39+
"nil": {
40+
input: nil,
41+
expected: nil,
42+
},
43+
"rawstate": {
44+
input: &tfprotov5.UpgradeResourceIdentityRequest{
45+
RawIdentity: testNewTfprotov5RawState(t, map[string]interface{}{
46+
"test_attribute": "test-value",
47+
}),
48+
},
49+
identitySchema: testIdentitySchema,
50+
expected: &fwserver.UpgradeResourceIdentityRequest{
51+
RawState: testNewTfprotov6RawState(t, map[string]interface{}{
52+
"test_attribute": "test-value",
53+
}),
54+
IdentitySchema: testIdentitySchema,
55+
},
56+
},
57+
"resourceschema": {
58+
input: &tfprotov5.UpgradeResourceIdentityRequest{},
59+
identitySchema: testIdentitySchema,
60+
expected: &fwserver.UpgradeResourceIdentityRequest{
61+
IdentitySchema: testIdentitySchema,
62+
},
63+
},
64+
"identityschema-missing": {
65+
input: &tfprotov5.UpgradeResourceIdentityRequest{},
66+
expected: nil,
67+
expectedDiagnostics: diag.Diagnostics{
68+
diag.NewErrorDiagnostic(
69+
"Unable to Create Empty Identity",
70+
"An unexpected error was encountered when creating the empty Identity. "+
71+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
72+
"Please report this to the provider developer:\n\n"+
73+
"Missing schema.",
74+
),
75+
},
76+
},
77+
"version": {
78+
input: &tfprotov5.UpgradeResourceIdentityRequest{
79+
Version: 123,
80+
},
81+
identitySchema: testIdentitySchema,
82+
expected: &fwserver.UpgradeResourceIdentityRequest{
83+
IdentitySchema: testIdentitySchema,
84+
Version: 123,
85+
},
86+
},
87+
}
88+
89+
for name, testCase := range testCases {
90+
t.Run(name, func(t *testing.T) {
91+
t.Parallel()
92+
93+
got, diags := fromproto5.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
94+
95+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
96+
t.Errorf("unexpected difference: %s", diff)
97+
}
98+
99+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
100+
t.Errorf("unexpected diagnostics difference: %s", diff)
101+
}
102+
})
103+
}
104+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6
5+
6+
import (
7+
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
11+
"github.com/hashicorp/terraform-plugin-framework/resource"
12+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
13+
)
14+
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) {
18+
if proto6 == nil {
19+
return nil, nil
20+
}
21+
22+
var diags diag.Diagnostics
23+
24+
// Panic prevention here to simplify the calling implementations.
25+
// This should not happen, but just in case.
26+
if identitySchema == nil {
27+
diags.AddError(
28+
"Unable to Create Empty Identity",
29+
"An unexpected error was encountered when creating the empty Identity. "+
30+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
31+
"Please report this to the provider developer:\n\n"+
32+
"Missing schema.",
33+
)
34+
35+
return nil, diags
36+
}
37+
38+
fw := &fwserver.UpgradeResourceIdentityRequest{
39+
RawState: proto6.RawIdentity,
40+
IdentitySchema: identitySchema,
41+
Resource: resource,
42+
Version: proto6.Version,
43+
}
44+
45+
return fw, diags
46+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6_test
5+
6+
import (
7+
"context"
8+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
9+
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
"github.com/hashicorp/terraform-plugin-framework/diag"
14+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
15+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
16+
"github.com/hashicorp/terraform-plugin-framework/resource"
17+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
18+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
19+
)
20+
21+
func TestUpgradeResourceIdentityRequest(t *testing.T) {
22+
t.Parallel()
23+
24+
testIdentitySchema := identityschema.Schema{
25+
Attributes: map[string]identityschema.Attribute{
26+
"test_attribute": schema.StringAttribute{
27+
Required: true,
28+
},
29+
},
30+
}
31+
32+
testCases := map[string]struct {
33+
input *tfprotov6.UpgradeResourceIdentityRequest
34+
identitySchema fwschema.Schema
35+
resource resource.Resource
36+
expected *fwserver.UpgradeResourceIdentityRequest
37+
expectedDiagnostics diag.Diagnostics
38+
}{
39+
"nil": {
40+
input: nil,
41+
expected: nil,
42+
},
43+
"rawIdentity": {
44+
input: &tfprotov6.UpgradeResourceIdentityRequest{
45+
RawIdentity: testNewRawState(t, map[string]interface{}{
46+
"test_attribute": "test-value",
47+
}),
48+
},
49+
identitySchema: testIdentitySchema,
50+
expected: &fwserver.UpgradeResourceIdentityRequest{
51+
RawState: testNewRawState(t, map[string]interface{}{
52+
"test_attribute": "test-value",
53+
}),
54+
IdentitySchema: testIdentitySchema,
55+
},
56+
},
57+
"resourceschema": {
58+
input: &tfprotov6.UpgradeResourceIdentityRequest{},
59+
identitySchema: testIdentitySchema,
60+
expected: &fwserver.UpgradeResourceIdentityRequest{
61+
IdentitySchema: testIdentitySchema,
62+
},
63+
},
64+
"resourceschema-missing": {
65+
input: &tfprotov6.UpgradeResourceIdentityRequest{},
66+
expected: nil,
67+
expectedDiagnostics: diag.Diagnostics{
68+
diag.NewErrorDiagnostic(
69+
"Unable to Create Empty Identity",
70+
"An unexpected error was encountered when creating the empty Identity. "+
71+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
72+
"Please report this to the provider developer:\n\n"+
73+
"Missing schema.",
74+
),
75+
},
76+
},
77+
"version": {
78+
input: &tfprotov6.UpgradeResourceIdentityRequest{
79+
Version: 123,
80+
},
81+
identitySchema: testIdentitySchema,
82+
expected: &fwserver.UpgradeResourceIdentityRequest{
83+
IdentitySchema: testIdentitySchema,
84+
Version: 123,
85+
},
86+
},
87+
}
88+
89+
for name, testCase := range testCases {
90+
t.Run(name, func(t *testing.T) {
91+
t.Parallel()
92+
93+
got, diags := fromproto6.UpgradeResourceIdentityRequest(context.Background(), testCase.input, testCase.resource, testCase.identitySchema)
94+
95+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
96+
t.Errorf("unexpected difference: %s", diff)
97+
}
98+
99+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
100+
t.Errorf("unexpected diagnostics difference: %s", diff)
101+
}
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)