From 9b4f15c261100764503804504e20f60ab8af2582 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 14 Apr 2017 17:24:08 -0700 Subject: [PATCH] plugin: move Client function into plugin, from plugin/discovery Having this as a method of PluginMeta felt most natural, but unfortunately that means that discovery must depend on plugin and plugin in turn depends on core Terraform, thus making the discovery package hard to use without creating dependency cycles. To resolve this, we invert the dependency and make the plugin package be responsible for instantiating clients given a meta, using a top-level function. --- command/plugins.go | 4 ++-- plugin/client.go | 24 ++++++++++++++++++++++++ plugin/discovery/meta.go | 19 ------------------- 3 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 plugin/client.go diff --git a/command/plugins.go b/command/plugins.go index 113378db00..fa4e48ab4a 100644 --- a/command/plugins.go +++ b/command/plugins.go @@ -50,7 +50,7 @@ func (m *Meta) providerFactories() map[string]terraform.ResourceProviderFactory // by name, we're guaranteed that the metas in our set all have // valid versions and that there's at least one meta. newest := metas.Newest() - client := newest.Client() + client := tfplugin.Client(newest) factories[name] = providerFactory(client) } @@ -99,7 +99,7 @@ func (m *Meta) provisionerFactories() map[string]terraform.ResourceProvisionerFa // by name, we're guaranteed that the metas in our set all have // valid versions and that there's at least one meta. newest := metas.Newest() - client := newest.Client() + client := tfplugin.Client(newest) factories[name] = provisionerFactory(client) } diff --git a/plugin/client.go b/plugin/client.go new file mode 100644 index 0000000000..3a5cb7af05 --- /dev/null +++ b/plugin/client.go @@ -0,0 +1,24 @@ +package plugin + +import ( + "os/exec" + + plugin "github.com/hashicorp/go-plugin" + "github.com/hashicorp/terraform/plugin/discovery" +) + +// ClientConfig returns a configuration object that can be used to instantiate +// a client for the plugin described by the given metadata. +func ClientConfig(m discovery.PluginMeta) *plugin.ClientConfig { + return &plugin.ClientConfig{ + Cmd: exec.Command(m.Path), + HandshakeConfig: Handshake, + Managed: true, + Plugins: PluginMap, + } +} + +// Client returns a plugin client for the plugin described by the given metadata. +func Client(m discovery.PluginMeta) *plugin.Client { + return plugin.NewClient(ClientConfig(m)) +} diff --git a/plugin/discovery/meta.go b/plugin/discovery/meta.go index 0c3a1ff4af..d93296cfc6 100644 --- a/plugin/discovery/meta.go +++ b/plugin/discovery/meta.go @@ -4,11 +4,8 @@ import ( "crypto/sha256" "io" "os" - "os/exec" "github.com/blang/semver" - plugin "github.com/hashicorp/go-plugin" - tfplugin "github.com/hashicorp/terraform/plugin" ) // PluginMeta is metadata about a plugin, useful for launching the plugin @@ -51,19 +48,3 @@ func (m PluginMeta) SHA256() ([]byte, error) { return h.Sum(nil), nil } - -// ClientConfig returns a configuration object that can be used to instantiate -// a client for the referenced plugin. -func (m PluginMeta) ClientConfig() *plugin.ClientConfig { - return &plugin.ClientConfig{ - Cmd: exec.Command(m.Path), - HandshakeConfig: tfplugin.Handshake, - Managed: true, - Plugins: tfplugin.PluginMap, - } -} - -// Client returns a plugin client for the referenced plugin. -func (m PluginMeta) Client() *plugin.Client { - return plugin.NewClient(m.ClientConfig()) -}