@@ -3,12 +3,82 @@ package tfprotov5server
33import (
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+
1282type server struct {
1383 downstream tfprotov5.ProviderServer
1484 tfplugin5.UnimplementedProviderServer
0 commit comments