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)
internal := c.internalProviders()
var errs error
if c.getPlugins {
@ -316,12 +315,6 @@ func (c *InitCommand) getProviders(path string, state *terraform.State, upgrade
}
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)
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
// fail with an error instructing the user to re-run this command.
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{}
for name, meta := range chosen {
digest, err := meta.SHA256()

View File

@ -1063,3 +1063,27 @@ func TestInit_pluginDirProvidersDoesNotGet(t *testing.T) {
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))
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":
pluginFunc, found := InternalProvisioners[pluginName]
if !found {

View File

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

View File

@ -2,15 +2,26 @@ package command
import "testing"
// providers are all external for now
//func TestInternalPlugin_InternalProviders(t *testing.T) {
// // Note this is a randomish sample and does not check for all plugins
// for _, name := range []string{"atlas", "consul", "docker", "template"} {
// if _, ok := InternalProviders[name]; !ok {
// t.Errorf("Expected to find %s in InternalProviders", name)
// }
// }
//}
func TestInternalPlugin_InternalProviders(t *testing.T) {
m := new(Meta)
providers := m.internalProviders()
// terraform is the only provider moved back to internal
for _, name := range []string{"terraform"} {
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) {
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 {
missing := make(discovery.PluginRequirements)
for n, r := range reqd {
log.Printf("[DEBUG] plugin requirements: %q=%q", n, r.Versions)
}
candidates := avail.ConstrainVersions(reqd)
internal := m.internalProviders()
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 {
missing[name] = versionSet
}

View File

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