Merge pull request #17044 from hashicorp/jbardin/plugin-dir-internal

-plugin-dir short-circuits discovery of internal plugins
This commit is contained in:
James Bardin 2018-01-05 16:54:28 -05:00 committed by GitHub
commit dec2da1dda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 33 deletions

View File

@ -306,7 +306,6 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
)) ))
missing := c.missingPlugins(available, requirements) missing := c.missingPlugins(available, requirements)
internal := c.internalProviders()
var errs error var errs error
if c.getPlugins { if c.getPlugins {
@ -316,12 +315,6 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
} }
for provider, reqd := range missing { for provider, reqd := range missing {
if _, isInternal := internal[provider]; isInternal {
// Ignore internal providers; they are not eligible for
// installation.
continue
}
_, err := c.providerInstaller.Get(provider, reqd.Versions) _, err := c.providerInstaller.Get(provider, reqd.Versions)
if err != nil { if err != nil {
@ -379,7 +372,10 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
// again. If anything changes, other commands that use providers will // again. If anything changes, other commands that use providers will
// fail with an error instructing the user to re-run this command. // fail with an error instructing the user to re-run this command.
available = c.providerPluginSet() // re-discover to see newly-installed plugins available = c.providerPluginSet() // re-discover to see newly-installed plugins
chosen := choosePlugins(available, internal, requirements)
// internal providers were already filtered out, since we don't need to get them.
chosen := choosePlugins(available, nil, requirements)
digests := map[string][]byte{} digests := map[string][]byte{}
for name, meta := range chosen { for name, meta := range chosen {
digest, err := meta.SHA256() digest, err := meta.SHA256()

View File

@ -1063,3 +1063,27 @@ func TestInit_pluginDirProvidersDoesNotGet(t *testing.T) {
t.Fatalf("bad: \n%s", ui.OutputWriter) t.Fatalf("bad: \n%s", ui.OutputWriter)
} }
} }
// Verify that plugin-dir doesn't prevent discovery of internal providers
func TestInit_pluginWithInternal(t *testing.T) {
td := tempDir(t)
copy.CopyDir(testFixturePath("init-internal"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
ui := new(cli.MockUi)
m := Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
}
c := &InitCommand{
Meta: m,
}
args := []string{"-plugin-dir", "./"}
//args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("error: %s", ui.ErrorWriter)
}
}

View File

@ -45,16 +45,6 @@ func (c *InternalPluginCommand) Run(args []string) int {
log.SetPrefix(fmt.Sprintf("%s-%s (internal) ", pluginName, pluginType)) log.SetPrefix(fmt.Sprintf("%s-%s (internal) ", pluginName, pluginType))
switch pluginType { switch pluginType {
case "provider":
pluginFunc, found := InternalProviders[pluginName]
if !found {
log.Printf("[ERROR] Could not load provider: %s", pluginName)
return 1
}
log.Printf("[INFO] Starting provider plugin %s", pluginName)
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: pluginFunc,
})
case "provisioner": case "provisioner":
pluginFunc, found := InternalProvisioners[pluginName] pluginFunc, found := InternalProvisioners[pluginName]
if !found { if !found {

View File

@ -14,8 +14,6 @@ import (
"github.com/hashicorp/terraform/plugin" "github.com/hashicorp/terraform/plugin"
) )
var InternalProviders = map[string]plugin.ProviderFunc{}
var InternalProvisioners = map[string]plugin.ProvisionerFunc{ var InternalProvisioners = map[string]plugin.ProvisionerFunc{
"chef": chefprovisioner.Provisioner, "chef": chefprovisioner.Provisioner,
"file": fileprovisioner.Provisioner, "file": fileprovisioner.Provisioner,

View File

@ -2,15 +2,26 @@ package command
import "testing" import "testing"
// providers are all external for now func TestInternalPlugin_InternalProviders(t *testing.T) {
//func TestInternalPlugin_InternalProviders(t *testing.T) { m := new(Meta)
// // Note this is a randomish sample and does not check for all plugins providers := m.internalProviders()
// for _, name := range []string{"atlas", "consul", "docker", "template"} { // terraform is the only provider moved back to internal
// if _, ok := InternalProviders[name]; !ok { for _, name := range []string{"terraform"} {
// t.Errorf("Expected to find %s in InternalProviders", name) pf, ok := providers[name]
// } if !ok {
// } t.Errorf("Expected to find %s in InternalProviders", name)
//} }
provider, err := pf()
if err != nil {
t.Fatal(err)
}
if provider == nil {
t.Fatal("provider factory returned a nil provider")
}
}
}
func TestInternalPlugin_InternalProvisioners(t *testing.T) { func TestInternalPlugin_InternalProvisioners(t *testing.T) {
for _, name := range []string{"chef", "file", "local-exec", "remote-exec", "salt-masterless"} { for _, name := range []string{"chef", "file", "local-exec", "remote-exec", "salt-masterless"} {

View File

@ -279,13 +279,16 @@ func (m *Meta) internalProviders() map[string]terraform.ResourceProviderFactory
func (m *Meta) missingPlugins(avail discovery.PluginMetaSet, reqd discovery.PluginRequirements) discovery.PluginRequirements { func (m *Meta) missingPlugins(avail discovery.PluginMetaSet, reqd discovery.PluginRequirements) discovery.PluginRequirements {
missing := make(discovery.PluginRequirements) missing := make(discovery.PluginRequirements)
for n, r := range reqd {
log.Printf("[DEBUG] plugin requirements: %q=%q", n, r.Versions)
}
candidates := avail.ConstrainVersions(reqd) candidates := avail.ConstrainVersions(reqd)
internal := m.internalProviders()
for name, versionSet := range reqd { for name, versionSet := range reqd {
// internal providers can't be missing
if _, ok := internal[name]; ok {
continue
}
log.Printf("[DEBUG] plugin requirements: %q=%q", name, versionSet.Versions)
if metas := candidates[name]; metas.Count() == 0 { if metas := candidates[name]; metas.Count() == 0 {
missing[name] = versionSet missing[name] = versionSet
} }

View File

@ -0,0 +1 @@
provider "terraform" {}