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.
This commit is contained in:
Martin Atkins 2017-04-14 17:24:08 -07:00
parent 73fc9985b2
commit 9b4f15c261
3 changed files with 26 additions and 21 deletions

View File

@ -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)
}

24
plugin/client.go Normal file
View File

@ -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))
}

View File

@ -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())
}