diff --git a/command/import.go b/command/import.go index 1f0e18602..ddb416d59 100644 --- a/command/import.go +++ b/command/import.go @@ -112,9 +112,16 @@ func (c *ImportCommand) Run(args []string) int { return 1 } + // Check for user-supplied plugin path + if c.pluginPath, err = c.loadPluginPath(); err != nil { + c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err)) + return 1 + } + // Load the backend b, err := c.Backend(&BackendOpts{ - Config: mod.Config(), + Config: mod.Config(), + ForceLocal: true, }) if err != nil { c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) diff --git a/command/import_test.go b/command/import_test.go index da31fe03d..057ab560b 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -2,9 +2,14 @@ package command import ( "fmt" + "io/ioutil" + "os" "strings" "testing" + "github.com/hashicorp/terraform/helper/copy" + "github.com/hashicorp/terraform/plugin" + "github.com/hashicorp/terraform/plugin/discovery" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -466,6 +471,72 @@ func TestImport_targetIsModule(t *testing.T) { } } +// make sure we search the full plugin path during import +func TestImport_pluginDir(t *testing.T) { + td := tempDir(t) + copy.CopyDir(testFixturePath("import-provider"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // make a fake provider in a custom plugin directory + if err := os.Mkdir("plugins", 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile("plugins/terraform-provider-test_v1.1.1_x4", []byte("invalid binary"), 0755); err != nil { + t.Fatal(err) + } + + ui := new(cli.MockUi) + c := &ImportCommand{ + Meta: Meta{ + Ui: ui, + }, + } + + // store our custom plugin path, which would normally happen during init + if err := c.storePluginPath([]string{"./plugins"}); err != nil { + t.Fatal(err) + } + + // Now we need to go through some plugin init. + // This discovers our fake plugin and writes the lock file. + initCmd := &InitCommand{ + Meta: Meta{ + pluginPath: []string{"./plugins"}, + Ui: new(cli.MockUi), + }, + providerInstaller: &discovery.ProviderInstaller{ + PluginProtocolVersion: plugin.Handshake.ProtocolVersion, + }, + } + if err := initCmd.getProviders(".", nil, false); err != nil { + t.Fatal(err) + } + + args := []string{ + "test_instance.foo", + "bar", + } + if code := c.Run(args); code == 0 { + t.Fatalf("expected error, got: %s", ui.OutputWriter) + } + + outMsg := ui.OutputWriter.String() + // if we were missing a plugin, the output will have some explanation + // about requirements. If discovery starts verifying binary compatibility, + // we will need to write a dummy provider above. + if strings.Contains(outMsg, "requirements") { + t.Fatal("unexpected output:", outMsg) + } + + // We wanted a plugin execution error, rather than a requirement error. + // Looking for "exec" in the error should suffice for now. + errMsg := ui.ErrorWriter.String() + if !strings.Contains(errMsg, "exec") { + t.Fatal("unexpected error:", errMsg) + } +} + const testImportStr = ` test_instance.foo: ID = yay diff --git a/plugin/discovery/find.go b/plugin/discovery/find.go index 51839ccad..127670080 100644 --- a/plugin/discovery/find.go +++ b/plugin/discovery/find.go @@ -53,13 +53,13 @@ func findPluginPaths(kind string, dirs []string) []string { continue } - log.Printf("[DEBUG] checking for plugins in %q", dir) + log.Printf("[DEBUG] checking for %s in %q", kind, dir) for _, item := range items { fullName := item.Name() if !strings.HasPrefix(fullName, prefix) { - log.Printf("[DEBUG] skipping %q, not a plugin", fullName) + log.Printf("[DEBUG] skipping %q, not a %s", fullName, kind) continue } @@ -71,7 +71,7 @@ func findPluginPaths(kind string, dirs []string) []string { continue } - log.Printf("[DEBUG] found plugin %q", fullName) + log.Printf("[DEBUG] found %s %q", kind, fullName) ret = append(ret, filepath.Clean(absPath)) continue } @@ -83,7 +83,7 @@ func findPluginPaths(kind string, dirs []string) []string { continue } - log.Printf("[WARNING] found legacy plugin %q", fullName) + log.Printf("[WARNING] found legacy %s %q", kind, fullName) ret = append(ret, filepath.Clean(absPath)) }