Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20240126-143840.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'all: Upgrade protocol versions to support the `MoveResourceState` RPC'
time: 2024-01-26T14:38:40.092759-05:00
custom:
Issue: "220"
11 changes: 11 additions & 0 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type TestServer struct {

ImportResourceStateCalled map[string]bool

MoveResourceStateCalled map[string]bool

PlanResourceChangeCalled map[string]bool

PrepareProviderConfigCalled bool
Expand Down Expand Up @@ -126,6 +128,15 @@ func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov5.Impor
return nil, nil
}

func (s *TestServer) MoveResourceState(_ context.Context, req *tfprotov5.MoveResourceStateRequest) (*tfprotov5.MoveResourceStateResponse, error) {
if s.MoveResourceStateCalled == nil {
s.MoveResourceStateCalled = make(map[string]bool)
}

s.MoveResourceStateCalled[req.TargetTypeName] = true
return nil, nil
}

func (s *TestServer) PlanResourceChange(_ context.Context, req *tfprotov5.PlanResourceChangeRequest) (*tfprotov5.PlanResourceChangeResponse, error) {
if s.PlanResourceChangeCalled == nil {
s.PlanResourceChangeCalled = make(map[string]bool)
Expand Down
11 changes: 11 additions & 0 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type TestServer struct {

ImportResourceStateCalled map[string]bool

MoveResourceStateCalled map[string]bool

PlanResourceChangeCalled map[string]bool

ReadDataSourceCalled map[string]bool
Expand Down Expand Up @@ -126,6 +128,15 @@ func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov6.Impor
return nil, nil
}

func (s *TestServer) MoveResourceState(_ context.Context, req *tfprotov6.MoveResourceStateRequest) (*tfprotov6.MoveResourceStateResponse, error) {
if s.MoveResourceStateCalled == nil {
s.MoveResourceStateCalled = make(map[string]bool)
}

s.MoveResourceStateCalled[req.TargetTypeName] = true
return nil, nil
}

func (s *TestServer) PlanResourceChange(_ context.Context, req *tfprotov6.PlanResourceChangeRequest) (*tfprotov6.PlanResourceChangeResponse, error) {
if s.PlanResourceChangeCalled == nil {
s.PlanResourceChangeCalled = make(map[string]bool)
Expand Down
27 changes: 27 additions & 0 deletions internal/tfprotov5tov6/tfprotov5tov6.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ func ImportedResources(in []*tfprotov5.ImportedResource) []*tfprotov6.ImportedRe
return res
}

func MoveResourceStateRequest(in *tfprotov5.MoveResourceStateRequest) *tfprotov6.MoveResourceStateRequest {
if in == nil {
return nil
}

return &tfprotov6.MoveResourceStateRequest{
SourcePrivate: in.SourcePrivate,
SourceProviderAddress: in.SourceProviderAddress,
SourceSchemaVersion: in.SourceSchemaVersion,
SourceState: RawState(in.SourceState),
SourceTypeName: in.SourceTypeName,
TargetTypeName: in.TargetTypeName,
}
}

func MoveResourceStateResponse(in *tfprotov5.MoveResourceStateResponse) *tfprotov6.MoveResourceStateResponse {
if in == nil {
return nil
}

return &tfprotov6.MoveResourceStateResponse{
Diagnostics: Diagnostics(in.Diagnostics),
TargetPrivate: in.TargetPrivate,
TargetState: DynamicValue(in.TargetState),
}
}

func PlanResourceChangeRequest(in *tfprotov5.PlanResourceChangeRequest) *tfprotov6.PlanResourceChangeRequest {
if in == nil {
return nil
Expand Down
90 changes: 90 additions & 0 deletions internal/tfprotov5tov6/tfprotov5tov6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,96 @@ func TestImportedResources(t *testing.T) {
}
}

func TestMoveResourceStateRequest(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfprotov5.MoveResourceStateRequest
expected *tfprotov6.MoveResourceStateRequest
}{
"nil": {
in: nil,
expected: nil,
},
"all-valid-fields": {
in: &tfprotov5.MoveResourceStateRequest{
SourcePrivate: testBytes,
SourceProviderAddress: "example.com/namespace/test",
SourceSchemaVersion: 1,
SourceState: &tfprotov5.RawState{
JSON: testBytes,
},
SourceTypeName: "test_source",
TargetTypeName: "test_target",
},
expected: &tfprotov6.MoveResourceStateRequest{
SourcePrivate: testBytes,
SourceProviderAddress: "example.com/namespace/test",
SourceSchemaVersion: 1,
SourceState: &tfprotov6.RawState{
JSON: testBytes,
},
SourceTypeName: "test_source",
TargetTypeName: "test_target",
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := tfprotov5tov6.MoveResourceStateRequest(testCase.in)

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

func TestMoveResourceStateResponse(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfprotov5.MoveResourceStateResponse
expected *tfprotov6.MoveResourceStateResponse
}{
"nil": {
in: nil,
expected: nil,
},
"all-valid-fields": {
in: &tfprotov5.MoveResourceStateResponse{
Diagnostics: testTfprotov5Diagnostics,
TargetPrivate: testBytes,
TargetState: &testTfprotov5DynamicValue,
},
expected: &tfprotov6.MoveResourceStateResponse{
Diagnostics: testTfprotov6Diagnostics,
TargetState: &testTfprotov6DynamicValue,
TargetPrivate: testBytes,
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := tfprotov5tov6.MoveResourceStateResponse(testCase.in)

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

func TestPlanResourceChangeRequest(t *testing.T) {
t.Parallel()

Expand Down
27 changes: 27 additions & 0 deletions internal/tfprotov6tov5/tfprotov6tov5.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,33 @@ func ImportedResources(in []*tfprotov6.ImportedResource) []*tfprotov5.ImportedRe
return res
}

func MoveResourceStateRequest(in *tfprotov6.MoveResourceStateRequest) *tfprotov5.MoveResourceStateRequest {
if in == nil {
return nil
}

return &tfprotov5.MoveResourceStateRequest{
SourcePrivate: in.SourcePrivate,
SourceProviderAddress: in.SourceProviderAddress,
SourceSchemaVersion: in.SourceSchemaVersion,
SourceState: RawState(in.SourceState),
SourceTypeName: in.SourceTypeName,
TargetTypeName: in.TargetTypeName,
}
}

func MoveResourceStateResponse(in *tfprotov6.MoveResourceStateResponse) *tfprotov5.MoveResourceStateResponse {
if in == nil {
return nil
}

return &tfprotov5.MoveResourceStateResponse{
Diagnostics: Diagnostics(in.Diagnostics),
TargetPrivate: in.TargetPrivate,
TargetState: DynamicValue(in.TargetState),
}
}

func PlanResourceChangeRequest(in *tfprotov6.PlanResourceChangeRequest) *tfprotov5.PlanResourceChangeRequest {
if in == nil {
return nil
Expand Down
90 changes: 90 additions & 0 deletions internal/tfprotov6tov5/tfprotov6tov5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,96 @@ func TestImportedResources(t *testing.T) {
}
}

func TestMoveResourceStateRequest(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfprotov6.MoveResourceStateRequest
expected *tfprotov5.MoveResourceStateRequest
}{
"nil": {
in: nil,
expected: nil,
},
"all-valid-fields": {
in: &tfprotov6.MoveResourceStateRequest{
SourcePrivate: testBytes,
SourceProviderAddress: "example.com/namespace/test",
SourceSchemaVersion: 1,
SourceState: &tfprotov6.RawState{
JSON: testBytes,
},
SourceTypeName: "test_source",
TargetTypeName: "test_target",
},
expected: &tfprotov5.MoveResourceStateRequest{
SourcePrivate: testBytes,
SourceProviderAddress: "example.com/namespace/test",
SourceSchemaVersion: 1,
SourceState: &tfprotov5.RawState{
JSON: testBytes,
},
SourceTypeName: "test_source",
TargetTypeName: "test_target",
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := tfprotov6tov5.MoveResourceStateRequest(testCase.in)

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

func TestMoveResourceStateResponse(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in *tfprotov6.MoveResourceStateResponse
expected *tfprotov5.MoveResourceStateResponse
}{
"nil": {
in: nil,
expected: nil,
},
"all-valid-fields": {
in: &tfprotov6.MoveResourceStateResponse{
Diagnostics: testTfprotov6Diagnostics,
TargetPrivate: testBytes,
TargetState: &testTfprotov6DynamicValue,
},
expected: &tfprotov5.MoveResourceStateResponse{
Diagnostics: testTfprotov5Diagnostics,
TargetState: &testTfprotov5DynamicValue,
TargetPrivate: testBytes,
},
},
}

for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()

got := tfprotov6tov5.MoveResourceStateResponse(testCase.in)

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

func TestPlanResourceChangeRequest(t *testing.T) {
t.Parallel()

Expand Down
9 changes: 9 additions & 0 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (

var _ tfprotov5.ProviderServer = &muxServer{}

// Temporarily verify that v5tov6Server implements new RPCs correctly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/219
var (
_ tfprotov5.FunctionServer = &muxServer{}
//nolint:staticcheck // Intentional verification of interface implementation.
_ tfprotov5.ResourceServerWithMoveResourceState = &muxServer{}
)

// muxServer is a gRPC server implementation that stands in front of other
// gRPC servers, routing requests to them as if they were a single server. It
// should always be instantiated by calling NewMuxServer().
Expand Down
Loading