Skip to content

Commit a6de1a8

Browse files
author
Paddy Carver
committed
Add serve helper function.
Add a helper that will stand up the go-plugin server for us. I made it require a name for the provider because I think having that information will be useful to us down the road for things like logging. It takes a `func() tfprotov5.ProviderServer`, which it will use to instantiate a tfprotov5.ProviderServer, which it will then serve. Factories only this time. Finally, I made it use the variadic options pattern so we can add more options later and offer a clearer indication of what's required when. I added two sample implementations of providing your own logger to go-plugin and of using the test mode of go-plugin that allows for reattaching to running provider processes.
1 parent 49daeca commit a6de1a8

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/golang/protobuf v1.4.2
7+
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd
78
github.com/hashicorp/go-plugin v1.3.0
89
github.com/kr/pretty v0.1.0 // indirect
910
github.com/vmihailenco/msgpack v4.0.4+incompatible

tfprotov5/server/server.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,82 @@ package tfprotov5server
33
import (
44
"context"
55

6+
"github.com/hashicorp/go-hclog"
7+
"github.com/hashicorp/go-plugin"
68
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
79
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/fromproto"
810
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
911
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto"
1012
)
1113

14+
type ServeOpt interface {
15+
ApplyServeOpt(*ServeConfig) error
16+
}
17+
18+
type ServeConfig struct {
19+
logger hclog.Logger
20+
debugCtx context.Context
21+
debugCh chan *plugin.ReattachConfig
22+
debugCloseCh chan struct{}
23+
}
24+
25+
type serveConfigFunc func(*ServeConfig) error
26+
27+
func (s serveConfigFunc) ApplyServeOpt(in *ServeConfig) error {
28+
return s(in)
29+
}
30+
31+
func WithDebug(ctx context.Context, config chan *plugin.ReattachConfig, closeCh chan struct{}) ServeOpt {
32+
return serveConfigFunc(func(in *ServeConfig) error {
33+
in.debugCtx = ctx
34+
in.debugCh = config
35+
in.debugCloseCh = closeCh
36+
return nil
37+
})
38+
}
39+
40+
func WithGoPluginLogger(logger hclog.Logger) ServeOpt {
41+
return serveConfigFunc(func(in *ServeConfig) error {
42+
in.logger = logger
43+
return nil
44+
})
45+
}
46+
47+
func Serve(name string, serverFactory func() tfprotov5.ProviderServer, opts ...ServeOpt) error {
48+
var conf ServeConfig
49+
for _, opt := range opts {
50+
err := opt.ApplyServeOpt(&conf)
51+
if err != nil {
52+
return err
53+
}
54+
}
55+
serveConfig := &plugin.ServeConfig{
56+
HandshakeConfig: plugin.HandshakeConfig{
57+
ProtocolVersion: 5,
58+
MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE",
59+
MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
60+
},
61+
Plugins: plugin.PluginSet{
62+
"provider": &GRPCProviderPlugin{
63+
GRPCProvider: serverFactory,
64+
},
65+
},
66+
GRPCServer: plugin.DefaultGRPCServer,
67+
}
68+
if conf.logger != nil {
69+
serveConfig.Logger = conf.logger
70+
}
71+
if conf.debugCh != nil {
72+
serveConfig.Test = &plugin.ServeTestConfig{
73+
Context: conf.debugCtx,
74+
ReattachConfigCh: conf.debugCh,
75+
CloseCh: conf.debugCloseCh,
76+
}
77+
}
78+
plugin.Serve(serveConfig)
79+
return nil
80+
}
81+
1282
type server struct {
1383
downstream tfprotov5.ProviderServer
1484
tfplugin5.UnimplementedProviderServer

0 commit comments

Comments
 (0)