2016-01-24 20:10:52 -06:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/go-plugin"
|
2018-11-19 11:39:16 -06:00
|
|
|
proto "github.com/hashicorp/terraform/internal/tfplugin5"
|
2016-01-24 20:10:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-16 13:00:08 -06:00
|
|
|
// The constants below are the names of the plugins that can be dispensed
|
|
|
|
// from the plugin server.
|
2016-01-24 20:10:52 -06:00
|
|
|
ProviderPluginName = "provider"
|
|
|
|
ProvisionerPluginName = "provisioner"
|
2019-01-16 13:00:08 -06:00
|
|
|
|
|
|
|
// DefaultProtocolVersion is the protocol version assumed for legacy clients that don't specify
|
|
|
|
// a particular version during their handshake. This is the version used when Terraform 0.10
|
|
|
|
// and 0.11 launch plugins that were built with support for both versions 4 and 5, and must
|
|
|
|
// stay unchanged at 4 until we intentionally build plugins that are not compatible with 0.10 and
|
|
|
|
// 0.11.
|
|
|
|
DefaultProtocolVersion = 4
|
2016-01-24 20:10:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handshake is the HandshakeConfig used to configure clients and servers.
|
|
|
|
var Handshake = plugin.HandshakeConfig{
|
2016-11-17 12:03:09 -06:00
|
|
|
// The ProtocolVersion is the version that must match between TF core
|
|
|
|
// and TF plugins. This should be bumped whenever a change happens in
|
|
|
|
// one or the other that makes it so that they can't safely communicate.
|
|
|
|
// This could be adding a new interface value, it could be how
|
|
|
|
// helper/schema computes diffs, etc.
|
2019-01-16 13:00:08 -06:00
|
|
|
ProtocolVersion: DefaultProtocolVersion,
|
2016-11-17 12:03:09 -06:00
|
|
|
|
|
|
|
// The magic cookie values should NEVER be changed.
|
2016-01-24 20:10:52 -06:00
|
|
|
MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE",
|
|
|
|
MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:55:20 -05:00
|
|
|
type GRPCProviderFunc func() proto.ProviderServer
|
|
|
|
type GRPCProvisionerFunc func() proto.ProvisionerServer
|
2016-01-24 20:10:52 -06:00
|
|
|
|
|
|
|
// ServeOpts are the configurations to serve a plugin.
|
|
|
|
type ServeOpts struct {
|
2018-08-14 15:55:20 -05:00
|
|
|
// Wrapped versions of the above plugins will automatically shimmed and
|
|
|
|
// added to the GRPC functions when possible.
|
|
|
|
GRPCProviderFunc GRPCProviderFunc
|
|
|
|
GRPCProvisionerFunc GRPCProvisionerFunc
|
2016-01-24 20:10:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve serves a plugin. This function never returns and should be the final
|
|
|
|
// function called in the main function of the plugin.
|
|
|
|
func Serve(opts *ServeOpts) {
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
2018-08-14 15:55:20 -05:00
|
|
|
HandshakeConfig: Handshake,
|
|
|
|
VersionedPlugins: pluginSet(opts),
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
2016-01-24 20:10:52 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:55:20 -05:00
|
|
|
func pluginSet(opts *ServeOpts) map[int]plugin.PluginSet {
|
2020-11-27 09:05:59 -06:00
|
|
|
plugins := map[int]plugin.PluginSet{}
|
2018-08-14 15:55:20 -05:00
|
|
|
|
|
|
|
// add the new protocol versions if they're configured
|
|
|
|
if opts.GRPCProviderFunc != nil || opts.GRPCProvisionerFunc != nil {
|
2018-08-24 18:53:05 -05:00
|
|
|
plugins[5] = plugin.PluginSet{}
|
|
|
|
if opts.GRPCProviderFunc != nil {
|
|
|
|
plugins[5]["provider"] = &GRPCProviderPlugin{
|
2018-08-14 15:55:20 -05:00
|
|
|
GRPCProvider: opts.GRPCProviderFunc,
|
2018-08-24 18:53:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if opts.GRPCProvisionerFunc != nil {
|
|
|
|
plugins[5]["provisioner"] = &GRPCProvisionerPlugin{
|
2018-08-14 15:55:20 -05:00
|
|
|
GRPCProvisioner: opts.GRPCProvisionerFunc,
|
2018-08-24 18:53:05 -05:00
|
|
|
}
|
2018-08-14 15:55:20 -05:00
|
|
|
}
|
2016-01-24 20:10:52 -06:00
|
|
|
}
|
2018-08-14 15:55:20 -05:00
|
|
|
return plugins
|
2016-01-24 20:10:52 -06:00
|
|
|
}
|