From 551bc9c4a4fdc193f7d4a641d525d1fc6f7e1fb3 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 13 Apr 2017 14:06:49 -0700 Subject: [PATCH] plugin/discovery: build plugin clients These new methods ClientConfig and Client provide the bridge into the main plugin infrastructure by configuring and instantiating (respectively) a client object for the referenced plugin. This stops short of getting the proxy object from the client since that then requires referencing the interface for the plugin kind, which would then create a dependency on the main terraform package which we'd rather avoid here. It'll be the responsibility of the caller in the command package to do the final wiring to get a provider instance out of a provider plugin client. --- plugin/discovery/meta.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugin/discovery/meta.go b/plugin/discovery/meta.go index d93296cfc..0c3a1ff4a 100644 --- a/plugin/discovery/meta.go +++ b/plugin/discovery/meta.go @@ -4,8 +4,11 @@ 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 @@ -48,3 +51,19 @@ 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()) +}