github.com/hugorut/terraform@v1.1.3/src/plugin/serve.go (about) 1 package plugin 2 3 import ( 4 "github.com/hashicorp/go-plugin" 5 proto "github.com/hugorut/terraform/src/tfplugin5" 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 ProvisionerPluginName = "provisioner" 13 14 // DefaultProtocolVersion is the protocol version assumed for legacy clients that don't specify 15 // a particular version during their handshake. This is the version used when Terraform 0.10 16 // and 0.11 launch plugins that were built with support for both versions 4 and 5, and must 17 // stay unchanged at 4 until we intentionally build plugins that are not compatible with 0.10 and 18 // 0.11. 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. This should be bumped whenever a change happens in 26 // one or the other that makes it so that they can't safely communicate. 27 // This could be adding a new interface value, it could be how 28 // helper/schema computes diffs, etc. 29 ProtocolVersion: DefaultProtocolVersion, 30 31 // The magic cookie values should NEVER be changed. 32 MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE", 33 MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2", 34 } 35 36 type GRPCProviderFunc func() proto.ProviderServer 37 type GRPCProvisionerFunc func() proto.ProvisionerServer 38 39 // ServeOpts are the configurations to serve a plugin. 40 type ServeOpts struct { 41 // Wrapped versions of the above plugins will automatically shimmed and 42 // added to the GRPC functions when possible. 43 GRPCProviderFunc GRPCProviderFunc 44 GRPCProvisionerFunc GRPCProvisionerFunc 45 } 46 47 // Serve serves a plugin. This function never returns and should be the final 48 // function called in the main function of the plugin. 49 func Serve(opts *ServeOpts) { 50 plugin.Serve(&plugin.ServeConfig{ 51 HandshakeConfig: Handshake, 52 VersionedPlugins: pluginSet(opts), 53 GRPCServer: plugin.DefaultGRPCServer, 54 }) 55 } 56 57 func pluginSet(opts *ServeOpts) map[int]plugin.PluginSet { 58 plugins := map[int]plugin.PluginSet{} 59 60 // add the new protocol versions if they're configured 61 if opts.GRPCProviderFunc != nil || opts.GRPCProvisionerFunc != nil { 62 plugins[5] = plugin.PluginSet{} 63 if opts.GRPCProviderFunc != nil { 64 plugins[5]["provider"] = &GRPCProviderPlugin{ 65 GRPCProvider: opts.GRPCProviderFunc, 66 } 67 } 68 if opts.GRPCProvisionerFunc != nil { 69 plugins[5]["provisioner"] = &GRPCProvisionerPlugin{ 70 GRPCProvisioner: opts.GRPCProvisionerFunc, 71 } 72 } 73 } 74 return plugins 75 }