github.com/pulumi/terraform@v1.4.0/pkg/plugin6/serve.go (about)

     1  package plugin6
     2  
     3  import (
     4  	"github.com/hashicorp/go-plugin"
     5  	proto "github.com/pulumi/terraform/pkg/tfplugin6"
     6  )
     7  
     8  const (
     9  	// The constants below are the names of the plugins that can be dispensed
    10  	// from the plugin server.
    11  	ProviderPluginName = "provider"
    12  
    13  	// DefaultProtocolVersion is the protocol version assumed for legacy clients
    14  	// that don't specify a particular version during their handshake. Since we
    15  	// explicitly set VersionedPlugins in Serve, this number does not need to
    16  	// change with the protocol version and can effectively stay 4 forever
    17  	// (unless we need the "biggest hammer" approach to break all provider
    18  	// compatibility).
    19  	DefaultProtocolVersion = 4
    20  )
    21  
    22  // Handshake is the HandshakeConfig used to configure clients and servers.
    23  var Handshake = plugin.HandshakeConfig{
    24  	// The ProtocolVersion is the version that must match between TF core
    25  	// and TF plugins.
    26  	ProtocolVersion: DefaultProtocolVersion,
    27  
    28  	// The magic cookie values should NEVER be changed.
    29  	MagicCookieKey:   "TF_PLUGIN_MAGIC_COOKIE",
    30  	MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
    31  }
    32  
    33  type GRPCProviderFunc func() proto.ProviderServer
    34  
    35  // ServeOpts are the configurations to serve a plugin.
    36  type ServeOpts struct {
    37  	GRPCProviderFunc GRPCProviderFunc
    38  }
    39  
    40  // Serve serves a plugin. This function never returns and should be the final
    41  // function called in the main function of the plugin.
    42  func Serve(opts *ServeOpts) {
    43  	plugin.Serve(&plugin.ServeConfig{
    44  		HandshakeConfig:  Handshake,
    45  		VersionedPlugins: pluginSet(opts),
    46  		GRPCServer:       plugin.DefaultGRPCServer,
    47  	})
    48  }
    49  
    50  func pluginSet(opts *ServeOpts) map[int]plugin.PluginSet {
    51  	plugins := map[int]plugin.PluginSet{}
    52  
    53  	// add the new protocol versions if they're configured
    54  	if opts.GRPCProviderFunc != nil {
    55  		plugins[6] = plugin.PluginSet{}
    56  		if opts.GRPCProviderFunc != nil {
    57  			plugins[6]["provider"] = &GRPCProviderPlugin{
    58  				GRPCProvider: opts.GRPCProviderFunc,
    59  			}
    60  		}
    61  	}
    62  	return plugins
    63  }