2017-05-04 13:03:57 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
|
|
|
)
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
// mockProviderInstaller is a discovery.PluginInstaller implementation that
|
|
|
|
// is a mock for discovery.ProviderInstaller.
|
|
|
|
type mockProviderInstaller struct {
|
2017-05-04 13:03:57 -05:00
|
|
|
// A map of provider names to available versions.
|
|
|
|
// The tests expect the versions to be in order from newest to oldest.
|
|
|
|
Providers map[string][]string
|
2017-06-12 20:22:47 -05:00
|
|
|
|
|
|
|
Dir string
|
|
|
|
PurgeUnusedCalled bool
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
func (i *mockProviderInstaller) FileName(provider, version string) string {
|
2017-06-09 15:24:25 -05:00
|
|
|
return fmt.Sprintf("terraform-provider-%s_v%s_x4", provider, version)
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
|
|
|
|
noMeta := discovery.PluginMeta{}
|
|
|
|
versions := i.Providers[provider]
|
2017-05-04 13:03:57 -05:00
|
|
|
if len(versions) == 0 {
|
2017-06-12 20:22:47 -05:00
|
|
|
return noMeta, fmt.Errorf("provider %q not found", provider)
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
err := os.MkdirAll(i.Dir, 0755)
|
2017-05-04 13:03:57 -05:00
|
|
|
if err != nil {
|
2017-06-12 20:22:47 -05:00
|
|
|
return noMeta, fmt.Errorf("error creating plugins directory: %s", err)
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range versions {
|
|
|
|
version, err := discovery.VersionStr(v).Parse()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:53:02 -05:00
|
|
|
if req.Allows(version) {
|
2017-05-04 13:03:57 -05:00
|
|
|
// provider filename
|
2017-06-12 20:22:47 -05:00
|
|
|
name := i.FileName(provider, v)
|
|
|
|
path := filepath.Join(i.Dir, name)
|
2017-05-04 13:03:57 -05:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
2017-06-12 20:22:47 -05:00
|
|
|
return noMeta, fmt.Errorf("error fetching provider: %s", err)
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
f.Close()
|
2017-06-12 20:22:47 -05:00
|
|
|
return discovery.PluginMeta{
|
|
|
|
Name: provider,
|
|
|
|
Version: discovery.VersionStr(v),
|
|
|
|
Path: path,
|
|
|
|
}, nil
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 20:22:47 -05:00
|
|
|
return noMeta, fmt.Errorf("no suitable version for provider %q found with constraints %s", provider, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *mockProviderInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) {
|
|
|
|
i.PurgeUnusedCalled = true
|
|
|
|
ret := make(discovery.PluginMetaSet)
|
|
|
|
ret.Add(discovery.PluginMeta{
|
|
|
|
Name: "test",
|
|
|
|
Version: "0.0.0",
|
|
|
|
Path: "mock-test",
|
|
|
|
})
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, error)
|
|
|
|
|
|
|
|
func (cb callbackPluginInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, error) {
|
|
|
|
return cb(provider, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cb callbackPluginInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) {
|
|
|
|
// does nothing
|
|
|
|
return make(discovery.PluginMetaSet), nil
|
2017-05-04 13:03:57 -05:00
|
|
|
}
|