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.
This commit is contained in:
Martin Atkins 2017-04-13 14:06:49 -07:00
parent 0a08214a74
commit 551bc9c4a4
1 changed files with 19 additions and 0 deletions

View File

@ -4,8 +4,11 @@ import (
"crypto/sha256" "crypto/sha256"
"io" "io"
"os" "os"
"os/exec"
"github.com/blang/semver" "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 // 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 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())
}