Skip to content

Commit 826bf1f

Browse files
author
Paddy Carver
committed
Document the tf5server package.
Also, rename it to tf5server, which is better than tfprotov5server.
1 parent 38435fb commit 826bf1f

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

tfprotov5/server/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Package tf5server implementations a server implementation for running
2+
// tfprotov5.ProviderServers as gRPC servers.
3+
//
4+
// Providers will likely be calling tf5server.Serve from their main function to
5+
// start the server so Terraform can connect to it.
6+
package tf5server

tfprotov5/server/plugin.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tfprotov5server
1+
package tf5server
22

33
import (
44
"context"
@@ -11,22 +11,34 @@ import (
1111
"google.golang.org/grpc"
1212
)
1313

14+
// GRPCProviderPlugin is an implementation of the
15+
// github.com/hashicorp/go-plugin#Plugin and
16+
// github.com/hashicorp/go-plugin#GRPCPlugin interfaces, indicating how to
17+
// serve tfprotov5.ProviderServers as gRPC plugins for go-plugin.
1418
type GRPCProviderPlugin struct {
1519
GRPCProvider func() tfprotov5.ProviderServer
1620
}
1721

22+
// Server always returns an error; we're only implementing the GRPCPlugin
23+
// interface, not the Plugin interface.
1824
func (p *GRPCProviderPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
1925
return nil, errors.New("terraform-plugin-go only implements gRPC servers")
2026
}
2127

28+
// Client always returns an error; we're only implementing the GRPCPlugin
29+
// interface, not the Plugin interface.
2230
func (p *GRPCProviderPlugin) Client(*plugin.MuxBroker, *rpc.Client) (interface{}, error) {
2331
return nil, errors.New("terraform-plugin-go only implements gRPC servers")
2432
}
2533

34+
// GRPCClient always returns an error; we're only implementing the server half
35+
// of the interface.
2636
func (p *GRPCProviderPlugin) GRPCClient(context.Context, *plugin.GRPCBroker, *grpc.ClientConn) (interface{}, error) {
2737
return nil, errors.New("terraform-plugin-go only implements gRPC servers")
2838
}
2939

40+
// GRPCServer registers the gRPC provider server with the gRPC server that
41+
// go-plugin is standing up.
3042
func (p *GRPCProviderPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
3143
tfplugin5.RegisterProviderServer(s, New(p.GRPCProvider()))
3244
return nil

tfprotov5/server/server.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tfprotov5server
1+
package tf5server
22

33
import (
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.
1418
type ServeOpt interface {
1519
ApplyServeOpt(*ServeConfig) error
1620
}
1721

22+
// ServeConfig contains the configured options for how a provider should be
23+
// served.
1824
type 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.
3139
func 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.
4050
func 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.
4766
func 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.
87108
func New(serve tfprotov5.ProviderServer) tfplugin5.ProviderServer {
88109
return server{
89110
downstream: serve,

0 commit comments

Comments
 (0)