Skip to content

Commit

Permalink
all: Remove type assertions for Function and MoveResourceState RPC im…
Browse files Browse the repository at this point in the history
…plementations (#238)

* all: Remove FunctionServer type assertions

* all: Remove ResourceServerWithMoveResourceState type assertions
  • Loading branch information
austinvalle authored May 8, 2024
1 parent 7469931 commit 23e467a
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 477 deletions.
9 changes: 0 additions & 9 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ 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
19 changes: 1 addition & 18 deletions tf5muxserver/mux_server_CallFunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,7 @@ func (s *muxServer) CallFunction(ctx context.Context, req *tfprotov5.CallFunctio
}

ctx = logging.Tfprotov5ProviderServerContext(ctx, server)

// Remove and call server.CallFunction below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := server.(tfprotov5.FunctionServer)

if !ok {
resp := &tfprotov5.CallFunctionResponse{
Error: &tfprotov5.FunctionError{
Text: "Provider Functions Not Implemented: A provider-defined function call was received by the provider, however the provider does not implement functions. " +
"Either upgrade the provider to a version that implements provider-defined functions or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
}

return resp, nil
}

logging.MuxTrace(ctx, "calling downstream server")

// return server.CallFunction(ctx, req)
return functionServer.CallFunction(ctx, req)
return server.CallFunction(ctx, req)
}
13 changes: 2 additions & 11 deletions tf5muxserver/mux_server_CallFunction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ func TestMuxServerCallFunction(t *testing.T) {
t.Fatalf("unexpected error setting up factory: %s", err)
}

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Fatal("muxServer should implement tfprotov5.FunctionServer")
}

// _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, err = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function1",
})

Expand All @@ -63,8 +55,7 @@ func TestMuxServerCallFunction(t *testing.T) {
t.Errorf("unexpected test_function1 CallFunction called on server2")
}

// _, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, err = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, err = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function2",
})

Expand Down
11 changes: 1 addition & 10 deletions tf5muxserver/mux_server_GetFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,9 @@ func (s *muxServer) GetFunctions(ctx context.Context, req *tfprotov5.GetFunction
for _, server := range s.servers {
ctx := logging.Tfprotov5ProviderServerContext(ctx, server)

// Remove and call server.GetFunctions below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := server.(tfprotov5.FunctionServer)

if !ok {
continue
}

logging.MuxTrace(ctx, "calling downstream server")

// serverResp, err := server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{})
serverResp, err := functionServer.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{})
serverResp, err := server.GetFunctions(ctx, &tfprotov5.GetFunctionsRequest{})

if err != nil {
return resp, fmt.Errorf("error calling GetFunctions for %T: %w", server, err)
Expand Down
10 changes: 1 addition & 9 deletions tf5muxserver/mux_server_GetFunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,7 @@ func TestMuxServerGetFunctions(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Fatal("muxServer should implement tfprotov5.FunctionServer")
}

// resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{})
resp, err := functionServer.GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{})
resp, err := muxServer.ProviderServer().GetFunctions(context.Background(), &tfprotov5.GetFunctionsRequest{})

if err != nil {
t.Fatalf("unexpected error: %s", err)
Expand Down
24 changes: 1 addition & 23 deletions tf5muxserver/mux_server_MoveResourceState.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,7 @@ func (s *muxServer) MoveResourceState(ctx context.Context, req *tfprotov5.MoveRe
}

ctx = logging.Tfprotov5ProviderServerContext(ctx, server)

// Remove and call server.MoveResourceState below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/219
//nolint:staticcheck // Intentionally verifying interface implementation
resourceServer, ok := server.(tfprotov5.ResourceServerWithMoveResourceState)

if !ok {
resp := &tfprotov5.MoveResourceStateResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "MoveResourceState Not Implemented",
Detail: "A MoveResourceState call was received by the provider, however the provider does not implement MoveResourceState. " +
"Either upgrade the provider to a version that implements MoveResourceState or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
},
}

return resp, nil
}

logging.MuxTrace(ctx, "calling downstream server")

// return server.MoveResourceState(ctx, req)
return resourceServer.MoveResourceState(ctx, req)
return server.MoveResourceState(ctx, req)
}
14 changes: 2 additions & 12 deletions tf5muxserver/mux_server_MoveResourceState_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,7 @@ func TestMuxServerMoveResourceState(t *testing.T) {
t.Fatalf("unexpected error setting up factory: %s", err)
}

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/219
//nolint:staticcheck // Intentionally verifying interface implementation
resourceServer, ok := muxServer.ProviderServer().(tfprotov5.ResourceServerWithMoveResourceState)

if !ok {
t.Fatal("muxServer should implement tfprotov5.ResourceServerWithMoveResourceState")
}

// _, err = muxServer.ProviderServer().MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
_, err = resourceServer.MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
_, err = muxServer.ProviderServer().MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
TargetTypeName: "test_resource1",
})

Expand All @@ -64,8 +55,7 @@ func TestMuxServerMoveResourceState(t *testing.T) {
t.Errorf("unexpected test_resource1 MoveResourceState called on server2")
}

// _, err = muxServer.ProviderServer().MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
_, err = resourceServer.MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
_, err = muxServer.ProviderServer().MoveResourceState(ctx, &tfprotov5.MoveResourceStateRequest{
TargetTypeName: "test_resource2",
})

Expand Down
60 changes: 6 additions & 54 deletions tf5muxserver/mux_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,15 +435,7 @@ func TestMuxServerGetFunctionServer_GetProviderSchema(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function1",
})
}
Expand Down Expand Up @@ -502,15 +494,7 @@ func TestMuxServerGetFunctionServer_GetProviderSchema_Duplicate(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function",
})

Expand Down Expand Up @@ -574,15 +558,7 @@ func TestMuxServerGetFunctionServer_GetMetadata(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function1",
})
}
Expand Down Expand Up @@ -645,15 +621,7 @@ func TestMuxServerGetFunctionServer_GetMetadata_Duplicate(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function",
})

Expand Down Expand Up @@ -715,15 +683,7 @@ func TestMuxServerGetFunctionServer_GetMetadata_Partial(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// _, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
_, _ = muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function1",
})
}
Expand Down Expand Up @@ -785,15 +745,7 @@ func TestMuxServerGetFunctionServer_Missing(t *testing.T) {
terraformOp := func() {
defer wg.Done()

// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := muxServer.ProviderServer().(tfprotov5.FunctionServer)

if !ok {
t.Error("muxServer should implement tfprotov5.FunctionServer")
}

// resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := functionServer.CallFunction(ctx, &tfprotov5.CallFunctionRequest{
resp, _ := muxServer.ProviderServer().CallFunction(ctx, &tfprotov5.CallFunctionRequest{
Name: "test_function_nonexistent",
})

Expand Down
68 changes: 3 additions & 65 deletions tf5to6server/tf5to6server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ func UpgradeServer(_ context.Context, v5server func() tfprotov5.ProviderServer)

var _ tfprotov6.ProviderServer = v5tov6Server{}

// 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 (
_ tfprotov6.FunctionServer = v5tov6Server{}
//nolint:staticcheck // Intentional verification of interface implementation.
_ tfprotov6.ResourceServerWithMoveResourceState = v5tov6Server{}
)

type v5tov6Server struct {
v5Server tfprotov5.ProviderServer
}
Expand All @@ -54,26 +45,9 @@ func (s v5tov6Server) ApplyResourceChange(ctx context.Context, req *tfprotov6.Ap
}

func (s v5tov6Server) CallFunction(ctx context.Context, req *tfprotov6.CallFunctionRequest) (*tfprotov6.CallFunctionResponse, error) {
// Remove and call s.v5Server.CallFunction below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := s.v5Server.(tfprotov5.FunctionServer)

if !ok {
v6Resp := &tfprotov6.CallFunctionResponse{
Error: &tfprotov6.FunctionError{
Text: "Provider Functions Not Implemented: A provider-defined function call was received by the provider, however the provider does not implement functions. " +
"Either upgrade the provider to a version that implements provider-defined functions or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
}

return v6Resp, nil
}

v5Req := tfprotov6tov5.CallFunctionRequest(req)
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
// v5Resp, err := s.v5Server.CallFunction(ctx, v5Req)
v5Resp, err := functionServer.CallFunction(ctx, v5Req)

v5Resp, err := s.v5Server.CallFunction(ctx, v5Req)
if err != nil {
return nil, err
}
Expand All @@ -93,23 +67,9 @@ func (s v5tov6Server) ConfigureProvider(ctx context.Context, req *tfprotov6.Conf
}

func (s v5tov6Server) GetFunctions(ctx context.Context, req *tfprotov6.GetFunctionsRequest) (*tfprotov6.GetFunctionsResponse, error) {
// Remove and call s.v5Server.GetFunctions below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
functionServer, ok := s.v5Server.(tfprotov5.FunctionServer)

if !ok {
v6Resp := &tfprotov6.GetFunctionsResponse{
Functions: map[string]*tfprotov6.Function{},
}

return v6Resp, nil
}

v5Req := tfprotov6tov5.GetFunctionsRequest(req)
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/210
// v5Resp, err := s.v5Server.GetFunctions(ctx, v5Req)
v5Resp, err := functionServer.GetFunctions(ctx, v5Req)

v5Resp, err := s.v5Server.GetFunctions(ctx, v5Req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -151,31 +111,9 @@ func (s v5tov6Server) ImportResourceState(ctx context.Context, req *tfprotov6.Im
}

func (s v5tov6Server) MoveResourceState(ctx context.Context, req *tfprotov6.MoveResourceStateRequest) (*tfprotov6.MoveResourceStateResponse, error) {
// Remove and call s.v5Server.MoveResourceState below directly.
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/219
//nolint:staticcheck // Intentional verification of interface implementation.
resourceServer, ok := s.v5Server.(tfprotov5.ResourceServerWithMoveResourceState)

if !ok {
v6Resp := &tfprotov6.MoveResourceStateResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "MoveResourceState Not Implemented",
Detail: "A MoveResourceState call was received by the provider, however the provider does not implement the RPC. " +
"Either upgrade the provider to a version that implements MoveResourceState or this is a bug in Terraform that should be reported to the Terraform maintainers.",
},
},
}

return v6Resp, nil
}

v5Req := tfprotov6tov5.MoveResourceStateRequest(req)
// Reference: https://github.com/hashicorp/terraform-plugin-mux/issues/219
// v5Resp, err := s.v5Server.MoveResourceState(ctx, v5Req)
v5Resp, err := resourceServer.MoveResourceState(ctx, v5Req)

v5Resp, err := s.v5Server.MoveResourceState(ctx, v5Req)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 23e467a

Please sign in to comment.