Skip to content

Commit 9f88ccc

Browse files
committed
fix optional interface implentation bug
ensure struct implements the two interfaces of go-plugin remove embedded interface and explicitly implement methods return errors for all methods other than grpc server method.
1 parent 8b0040c commit 9f88ccc

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

plugin/grpc_provider.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,37 @@ package plugin
22

33
import (
44
"context"
5+
"errors"
6+
"net/rpc"
57

68
plugin "github.com/hashicorp/go-plugin"
79
"google.golang.org/grpc"
810

911
proto "github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfplugin5"
1012
)
1113

12-
// GRPCProviderPlugin implements plugin.GRPCPlugin for the go-plugin package.
14+
var (
15+
_ plugin.GRPCPlugin = (*gRPCProviderPlugin)(nil)
16+
_ plugin.Plugin = (*gRPCProviderPlugin)(nil)
17+
)
18+
19+
// gRPCProviderPlugin implements plugin.GRPCPlugin and plugin.Plugin for the go-plugin package.
20+
// the only real implementation is GRPCSServer, the other methods are only satisfied
21+
// for compatibility with go-plugin
1322
type gRPCProviderPlugin struct {
14-
plugin.Plugin
1523
GRPCProvider func() proto.ProviderServer
1624
}
1725

18-
// this exists only to satisfy the go-plugin.GRPCPlugin interface
19-
// that interface should likely be split
20-
func (p *gRPCProviderPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
21-
return nil, nil
26+
func (p *gRPCProviderPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
27+
return nil, errors.New("terraform-plugin-sdk only implements grpc servers")
28+
}
29+
30+
func (p *gRPCProviderPlugin) Client(*plugin.MuxBroker, *rpc.Client) (interface{}, error) {
31+
return nil, errors.New("terraform-plugin-sdk only implements grpc servers")
32+
}
33+
34+
func (p *gRPCProviderPlugin) GRPCClient(context.Context, *plugin.GRPCBroker, *grpc.ClientConn) (interface{}, error) {
35+
return nil, errors.New("terraform-plugin-sdk only implements grpc servers")
2236
}
2337

2438
func (p *gRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {

plugin/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Serve(opts *ServeOpts) {
5353
HandshakeConfig: Handshake,
5454
VersionedPlugins: map[int]plugin.PluginSet{
5555
5: {
56-
ProviderPluginName: gRPCProviderPlugin{
56+
ProviderPluginName: &gRPCProviderPlugin{
5757
GRPCProvider: func() proto.ProviderServer {
5858
return provider
5959
},

0 commit comments

Comments
 (0)