1- package tfprotov5server
1+ package tf5server
22
33import (
44 "context"
@@ -11,10 +11,16 @@ import (
1111 "github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto"
1212)
1313
14+ // ServeOpt is an interface for defining options that can be passed to the
15+ // Serve function. Each implementation modifies the ServeConfig being
16+ // generated. A slice of ServeOpts then, cumulatively applied, render a full
17+ // ServeConfig.
1418type ServeOpt interface {
1519 ApplyServeOpt (* ServeConfig ) error
1620}
1721
22+ // ServeConfig contains the configured options for how a provider should be
23+ // served.
1824type ServeConfig struct {
1925 logger hclog.Logger
2026 debugCtx context.Context
@@ -28,6 +34,8 @@ func (s serveConfigFunc) ApplyServeOpt(in *ServeConfig) error {
2834 return s (in )
2935}
3036
37+ // WithDebug returns a ServeOpt that will set the server into debug mode, using
38+ // the passed options to populate the go-plugin ServeTestConfig.
3139func WithDebug (ctx context.Context , config chan * plugin.ReattachConfig , closeCh chan struct {}) ServeOpt {
3240 return serveConfigFunc (func (in * ServeConfig ) error {
3341 in .debugCtx = ctx
@@ -37,13 +45,24 @@ func WithDebug(ctx context.Context, config chan *plugin.ReattachConfig, closeCh
3745 })
3846}
3947
48+ // WithGoPluginLogger returns a ServeOpt that will set the logger that
49+ // go-plugin should use to log messages.
4050func WithGoPluginLogger (logger hclog.Logger ) ServeOpt {
4151 return serveConfigFunc (func (in * ServeConfig ) error {
4252 in .logger = logger
4353 return nil
4454 })
4555}
4656
57+ // Serve starts a tfprotov5.ProviderServer serving, ready for Terraform to
58+ // connect to it. The name passed in should be the fully qualified name that
59+ // users will enter in the source field of the required_providers block, like
60+ // "registry.terraform.io/hashicorp/time".
61+ //
62+ // Zero or more options to configure the server may also be passed. The default
63+ // invocation is sufficient, but if the provider wants to run in debug mode or
64+ // modify the logger that go-plugin is using, ServeOpts can be specified to
65+ // support that.
4766func Serve (name string , serverFactory func () tfprotov5.ProviderServer , opts ... ServeOpt ) error {
4867 var conf ServeConfig
4968 for _ , opt := range opts {
@@ -84,6 +103,8 @@ type server struct {
84103 tfplugin5.UnimplementedProviderServer
85104}
86105
106+ // New converts a tfprotov5.ProviderServer into a server capable of handling
107+ // Terraform protocol requests and issuing responses using the gRPC types.
87108func New (serve tfprotov5.ProviderServer ) tfplugin5.ProviderServer {
88109 return server {
89110 downstream : serve ,
0 commit comments