Skip to content

Commit aa4c3e9

Browse files
authored
all: Initial MoveResourceState implementation (#1307)
Reference: hashicorp/terraform-plugin-go#351 Reference: https://developer.hashicorp.com/terraform/plugin/framework/migrating The next versions of the plugin protocol (5.5/6.5) include support for moving resource state across resource types. The terraform-plugin-sdk Go module will not be receiving this feature, however this Go module must be updated to handle the new RPC with errors. Provider developers can implement move resource state in their terraform-plugin-sdk based providers by following the framework migration documentation to introduce terraform-plugin-mux and migrating the resource type to terraform-plugin-framework.
1 parent 9c3358e commit aa4c3e9

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
kind: NOTES
2+
body: 'helper/schema: While this Go module will not receive support for moving
3+
resource state across resource types, the provider server is updated to handle
4+
the new operation, which will be required to prevent errors when updating
5+
terraform-plugin-framework or terraform-plugin-mux in the future.'
6+
time: 2024-01-29T08:48:40.990566-05:00
7+
custom:
8+
Issue: "1307"

helper/schema/grpc_provider.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const (
2828
newExtraKey = "_new_extra_shim"
2929
)
3030

31+
// Verify provider server interface implementation.
32+
var _ tfprotov5.ProviderServer = (*GRPCProviderServer)(nil)
33+
3134
func NewGRPCProviderServer(p *Provider) *GRPCProviderServer {
3235
return &GRPCProviderServer{
3336
provider: p,
@@ -1208,6 +1211,42 @@ func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfpro
12081211
return resp, nil
12091212
}
12101213

1214+
func (s *GRPCProviderServer) MoveResourceState(ctx context.Context, req *tfprotov5.MoveResourceStateRequest) (*tfprotov5.MoveResourceStateResponse, error) {
1215+
if req == nil {
1216+
return nil, fmt.Errorf("MoveResourceState request is nil")
1217+
}
1218+
1219+
ctx = logging.InitContext(ctx)
1220+
1221+
logging.HelperSchemaTrace(ctx, "Returning error for MoveResourceState")
1222+
1223+
resp := &tfprotov5.MoveResourceStateResponse{}
1224+
1225+
_, ok := s.provider.ResourcesMap[req.TargetTypeName]
1226+
1227+
if !ok {
1228+
resp.Diagnostics = []*tfprotov5.Diagnostic{
1229+
{
1230+
Severity: tfprotov5.DiagnosticSeverityError,
1231+
Summary: "Unknown Resource Type",
1232+
Detail: fmt.Sprintf("The %q resource type is not supported by this provider.", req.TargetTypeName),
1233+
},
1234+
}
1235+
1236+
return resp, nil
1237+
}
1238+
1239+
resp.Diagnostics = []*tfprotov5.Diagnostic{
1240+
{
1241+
Severity: tfprotov5.DiagnosticSeverityError,
1242+
Summary: "Move Resource State Not Supported",
1243+
Detail: fmt.Sprintf("The %q resource type does not support moving resource state across resource types.", req.TargetTypeName),
1244+
},
1245+
}
1246+
1247+
return resp, nil
1248+
}
1249+
12111250
func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) {
12121251
ctx = logging.InitContext(ctx)
12131252
resp := &tfprotov5.ReadDataSourceResponse{}

helper/schema/grpc_provider_test.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import (
2424
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2525
)
2626

27-
// The GRPCProviderServer will directly implement the go protobuf server
28-
var _ tfprotov5.ProviderServer = (*GRPCProviderServer)(nil)
29-
3027
func TestGRPCProviderServerConfigureProvider(t *testing.T) {
3128
t.Parallel()
3229

@@ -2209,6 +2206,74 @@ func TestGRPCProviderServerGetMetadata(t *testing.T) {
22092206
}
22102207
}
22112208

2209+
func TestGRPCProviderServerMoveResourceState(t *testing.T) {
2210+
t.Parallel()
2211+
2212+
testCases := map[string]struct {
2213+
server *GRPCProviderServer
2214+
request *tfprotov5.MoveResourceStateRequest
2215+
expected *tfprotov5.MoveResourceStateResponse
2216+
}{
2217+
"nil": {
2218+
server: NewGRPCProviderServer(&Provider{}),
2219+
request: nil,
2220+
expected: nil,
2221+
},
2222+
"request-TargetTypeName-missing": {
2223+
server: NewGRPCProviderServer(&Provider{}),
2224+
request: &tfprotov5.MoveResourceStateRequest{
2225+
TargetTypeName: "test_resource",
2226+
},
2227+
expected: &tfprotov5.MoveResourceStateResponse{
2228+
Diagnostics: []*tfprotov5.Diagnostic{
2229+
{
2230+
Severity: tfprotov5.DiagnosticSeverityError,
2231+
Summary: "Unknown Resource Type",
2232+
Detail: "The \"test_resource\" resource type is not supported by this provider.",
2233+
},
2234+
},
2235+
},
2236+
},
2237+
"request-TargetTypeName": {
2238+
server: NewGRPCProviderServer(&Provider{
2239+
ResourcesMap: map[string]*Resource{
2240+
"test_resource": {},
2241+
},
2242+
}),
2243+
request: &tfprotov5.MoveResourceStateRequest{
2244+
TargetTypeName: "test_resource",
2245+
},
2246+
expected: &tfprotov5.MoveResourceStateResponse{
2247+
Diagnostics: []*tfprotov5.Diagnostic{
2248+
{
2249+
Severity: tfprotov5.DiagnosticSeverityError,
2250+
Summary: "Move Resource State Not Supported",
2251+
Detail: "The \"test_resource\" resource type does not support moving resource state across resource types.",
2252+
},
2253+
},
2254+
},
2255+
},
2256+
}
2257+
2258+
for name, testCase := range testCases {
2259+
name, testCase := name, testCase
2260+
2261+
t.Run(name, func(t *testing.T) {
2262+
t.Parallel()
2263+
2264+
resp, err := testCase.server.MoveResourceState(context.Background(), testCase.request)
2265+
2266+
if testCase.request != nil && err != nil {
2267+
t.Fatalf("unexpected error: %s", err)
2268+
}
2269+
2270+
if diff := cmp.Diff(resp, testCase.expected); diff != "" {
2271+
t.Errorf("unexpected difference: %s", diff)
2272+
}
2273+
})
2274+
}
2275+
}
2276+
22122277
func TestUpgradeState_jsonState(t *testing.T) {
22132278
r := &Resource{
22142279
SchemaVersion: 2,

0 commit comments

Comments
 (0)