aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/plugin/serve.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/plugin/serve.go')
-rw-r--r--vendor/github.com/hashicorp/terraform/plugin/serve.go87
1 files changed, 77 insertions, 10 deletions
diff --git a/vendor/github.com/hashicorp/terraform/plugin/serve.go b/vendor/github.com/hashicorp/terraform/plugin/serve.go
index 2028a61..8d056c5 100644
--- a/vendor/github.com/hashicorp/terraform/plugin/serve.go
+++ b/vendor/github.com/hashicorp/terraform/plugin/serve.go
@@ -2,14 +2,23 @@ package plugin
2 2
3import ( 3import (
4 "github.com/hashicorp/go-plugin" 4 "github.com/hashicorp/go-plugin"
5 grpcplugin "github.com/hashicorp/terraform/helper/plugin"
6 proto "github.com/hashicorp/terraform/internal/tfplugin5"
5 "github.com/hashicorp/terraform/terraform" 7 "github.com/hashicorp/terraform/terraform"
6) 8)
7 9
8// The constants below are the names of the plugins that can be dispensed
9// from the plugin server.
10const ( 10const (
11 // The constants below are the names of the plugins that can be dispensed
12 // from the plugin server.
11 ProviderPluginName = "provider" 13 ProviderPluginName = "provider"
12 ProvisionerPluginName = "provisioner" 14 ProvisionerPluginName = "provisioner"
15
16 // DefaultProtocolVersion is the protocol version assumed for legacy clients that don't specify
17 // a particular version during their handshake. This is the version used when Terraform 0.10
18 // and 0.11 launch plugins that were built with support for both versions 4 and 5, and must
19 // stay unchanged at 4 until we intentionally build plugins that are not compatible with 0.10 and
20 // 0.11.
21 DefaultProtocolVersion = 4
13) 22)
14 23
15// Handshake is the HandshakeConfig used to configure clients and servers. 24// Handshake is the HandshakeConfig used to configure clients and servers.
@@ -19,7 +28,7 @@ var Handshake = plugin.HandshakeConfig{
19 // one or the other that makes it so that they can't safely communicate. 28 // one or the other that makes it so that they can't safely communicate.
20 // This could be adding a new interface value, it could be how 29 // This could be adding a new interface value, it could be how
21 // helper/schema computes diffs, etc. 30 // helper/schema computes diffs, etc.
22 ProtocolVersion: 4, 31 ProtocolVersion: DefaultProtocolVersion,
23 32
24 // The magic cookie values should NEVER be changed. 33 // The magic cookie values should NEVER be changed.
25 MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE", 34 MagicCookieKey: "TF_PLUGIN_MAGIC_COOKIE",
@@ -28,27 +37,85 @@ var Handshake = plugin.HandshakeConfig{
28 37
29type ProviderFunc func() terraform.ResourceProvider 38type ProviderFunc func() terraform.ResourceProvider
30type ProvisionerFunc func() terraform.ResourceProvisioner 39type ProvisionerFunc func() terraform.ResourceProvisioner
40type GRPCProviderFunc func() proto.ProviderServer
41type GRPCProvisionerFunc func() proto.ProvisionerServer
31 42
32// ServeOpts are the configurations to serve a plugin. 43// ServeOpts are the configurations to serve a plugin.
33type ServeOpts struct { 44type ServeOpts struct {
34 ProviderFunc ProviderFunc 45 ProviderFunc ProviderFunc
35 ProvisionerFunc ProvisionerFunc 46 ProvisionerFunc ProvisionerFunc
47
48 // Wrapped versions of the above plugins will automatically shimmed and
49 // added to the GRPC functions when possible.
50 GRPCProviderFunc GRPCProviderFunc
51 GRPCProvisionerFunc GRPCProvisionerFunc
36} 52}
37 53
38// Serve serves a plugin. This function never returns and should be the final 54// Serve serves a plugin. This function never returns and should be the final
39// function called in the main function of the plugin. 55// function called in the main function of the plugin.
40func Serve(opts *ServeOpts) { 56func Serve(opts *ServeOpts) {
57 // since the plugins may not yet be aware of the new protocol, we
58 // automatically wrap the plugins in the grpc shims.
59 if opts.GRPCProviderFunc == nil && opts.ProviderFunc != nil {
60 provider := grpcplugin.NewGRPCProviderServerShim(opts.ProviderFunc())
61 // this is almost always going to be a *schema.Provider, but check that
62 // we got back a valid provider just in case.
63 if provider != nil {
64 opts.GRPCProviderFunc = func() proto.ProviderServer {
65 return provider
66 }
67 }
68 }
69 if opts.GRPCProvisionerFunc == nil && opts.ProvisionerFunc != nil {
70 provisioner := grpcplugin.NewGRPCProvisionerServerShim(opts.ProvisionerFunc())
71 if provisioner != nil {
72 opts.GRPCProvisionerFunc = func() proto.ProvisionerServer {
73 return provisioner
74 }
75 }
76 }
77
41 plugin.Serve(&plugin.ServeConfig{ 78 plugin.Serve(&plugin.ServeConfig{
42 HandshakeConfig: Handshake, 79 HandshakeConfig: Handshake,
43 Plugins: pluginMap(opts), 80 VersionedPlugins: pluginSet(opts),
81 GRPCServer: plugin.DefaultGRPCServer,
44 }) 82 })
45} 83}
46 84
47// pluginMap returns the map[string]plugin.Plugin to use for configuring a plugin 85// pluginMap returns the legacy map[string]plugin.Plugin to use for configuring
48// server or client. 86// a plugin server or client.
49func pluginMap(opts *ServeOpts) map[string]plugin.Plugin { 87func legacyPluginMap(opts *ServeOpts) map[string]plugin.Plugin {
50 return map[string]plugin.Plugin{ 88 return map[string]plugin.Plugin{
51 "provider": &ResourceProviderPlugin{F: opts.ProviderFunc}, 89 "provider": &ResourceProviderPlugin{
52 "provisioner": &ResourceProvisionerPlugin{F: opts.ProvisionerFunc}, 90 ResourceProvider: opts.ProviderFunc,
91 },
92 "provisioner": &ResourceProvisionerPlugin{
93 ResourceProvisioner: opts.ProvisionerFunc,
94 },
95 }
96}
97
98func pluginSet(opts *ServeOpts) map[int]plugin.PluginSet {
99 // Set the legacy netrpc plugins at version 4.
100 // The oldest version is returned in when executed by a legacy go-plugin
101 // client.
102 plugins := map[int]plugin.PluginSet{
103 4: legacyPluginMap(opts),
104 }
105
106 // add the new protocol versions if they're configured
107 if opts.GRPCProviderFunc != nil || opts.GRPCProvisionerFunc != nil {
108 plugins[5] = plugin.PluginSet{}
109 if opts.GRPCProviderFunc != nil {
110 plugins[5]["provider"] = &GRPCProviderPlugin{
111 GRPCProvider: opts.GRPCProviderFunc,
112 }
113 }
114 if opts.GRPCProvisionerFunc != nil {
115 plugins[5]["provisioner"] = &GRPCProvisionerPlugin{
116 GRPCProvisioner: opts.GRPCProvisionerFunc,
117 }
118 }
53 } 119 }
120 return plugins
54} 121}