plugin/discovery: use new addrs.ProviderType in place of a provider typeName string (#22724)

This is a relatively small change meant to lay the foundation for
future enhancements to providers' address.
This commit is contained in:
Kristin Laemmert 2019-09-09 16:59:50 -04:00 committed by GitHub
parent dcad6239c5
commit 120bb0a66c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 29 deletions

7
addrs/provider_type.go Normal file
View File

@ -0,0 +1,7 @@
package addrs
// ProviderType encapsulates a single provider type. In the future this will be
// extended to include additional fields including Namespace and SourceHost
type ProviderType struct {
Name string
}

View File

@ -13,6 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend"
backendInit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/configs"
@ -516,7 +517,8 @@ func (c *InitCommand) getProviders(earlyConfig *earlyconfig.Config, state *state
}
for provider, reqd := range missing {
_, providerDiags, err := c.providerInstaller.Get(provider, reqd.Versions)
pty := addrs.ProviderType{Name: provider}
_, providerDiags, err := c.providerInstaller.Get(pty, reqd.Versions)
diags = diags.Append(providerDiags)
if err != nil {

View File

@ -7,6 +7,7 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/providers"
@ -147,10 +148,10 @@ func (i *mockProviderInstaller) FileName(provider, version string) string {
return fmt.Sprintf("terraform-provider-%s_v%s_x4", provider, version)
}
func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) {
func (i *mockProviderInstaller) Get(provider addrs.ProviderType, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) {
var diags tfdiags.Diagnostics
noMeta := discovery.PluginMeta{}
versions := i.Providers[provider]
versions := i.Providers[provider.Name]
if len(versions) == 0 {
return noMeta, diags, fmt.Errorf("provider %q not found", provider)
}
@ -168,7 +169,7 @@ func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints)
if req.Allows(version) {
// provider filename
name := i.FileName(provider, v)
name := i.FileName(provider.Name, v)
path := filepath.Join(i.Dir, name)
f, err := os.Create(path)
if err != nil {
@ -176,7 +177,7 @@ func (i *mockProviderInstaller) Get(provider string, req discovery.Constraints)
}
f.Close()
return discovery.PluginMeta{
Name: provider,
Name: provider.Name,
Version: discovery.VersionStr(v),
Path: path,
}, diags, nil
@ -199,8 +200,8 @@ func (i *mockProviderInstaller) PurgeUnused(map[string]discovery.PluginMeta) (di
type callbackPluginInstaller func(provider string, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error)
func (cb callbackPluginInstaller) Get(provider string, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) {
return cb(provider, req)
func (cb callbackPluginInstaller) Get(provider addrs.ProviderType, req discovery.Constraints) (discovery.PluginMeta, tfdiags.Diagnostics, error) {
return cb(provider.Name, req)
}
func (cb callbackPluginInstaller) PurgeUnused(map[string]discovery.PluginMeta) (discovery.PluginMetaSet, error) {

View File

@ -16,6 +16,7 @@ import (
"github.com/hashicorp/errwrap"
getter "github.com/hashicorp/go-getter"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/httpclient"
"github.com/hashicorp/terraform/registry"
"github.com/hashicorp/terraform/registry/regsrc"
@ -49,7 +50,7 @@ func init() {
// An Installer maintains a local cache of plugins by downloading plugins
// from an online repository.
type Installer interface {
Get(name string, req Constraints) (PluginMeta, tfdiags.Diagnostics, error)
Get(provider addrs.ProviderType, req Constraints) (PluginMeta, tfdiags.Diagnostics, error)
PurgeUnused(used map[string]PluginMeta) (removed PluginMetaSet, err error)
}
@ -106,7 +107,7 @@ type ProviderInstaller struct {
// are produced under the assumption that if presented to the user they will
// be presented alongside context about what is being installed, and thus the
// error messages do not redundantly include such information.
func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, tfdiags.Diagnostics, error) {
func (i *ProviderInstaller) Get(provider addrs.ProviderType, req Constraints) (PluginMeta, tfdiags.Diagnostics, error) {
var diags tfdiags.Diagnostics
// a little bit of initialization.
@ -231,7 +232,7 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, t
}
}
printedProviderName := fmt.Sprintf("%q (%s)", provider, providerSource)
printedProviderName := fmt.Sprintf("%q (%s)", provider.Name, providerSource)
i.Ui.Info(fmt.Sprintf("- Downloading plugin for provider %s %s...", printedProviderName, versionMeta.Version))
log.Printf("[DEBUG] getting provider %s version %q", printedProviderName, versionMeta.Version)
err = i.install(provider, v, providerURL)
@ -243,11 +244,11 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, t
// (This is weird, because go-getter doesn't directly return
// information about what was extracted, and we just extracted
// the archive directly into a shared dir here.)
log.Printf("[DEBUG] looking for the %s %s plugin we just installed", provider, versionMeta.Version)
log.Printf("[DEBUG] looking for the %s %s plugin we just installed", provider.Name, versionMeta.Version)
metas := FindPlugins("provider", []string{i.Dir})
log.Printf("[DEBUG] all plugins found %#v", metas)
metas, _ = metas.ValidateVersions()
metas = metas.WithName(provider).WithVersion(v)
metas = metas.WithName(provider.Name).WithVersion(v)
log.Printf("[DEBUG] filtered plugins %#v", metas)
if metas.Count() == 0 {
// This should never happen. Suggests that the release archive
@ -275,18 +276,18 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, t
return metas.Newest(), diags, nil
}
func (i *ProviderInstaller) install(provider string, version Version, url string) error {
func (i *ProviderInstaller) install(provider addrs.ProviderType, version Version, url string) error {
if i.Cache != nil {
log.Printf("[DEBUG] looking for provider %s %s in plugin cache", provider, version)
cached := i.Cache.CachedPluginPath("provider", provider, version)
log.Printf("[DEBUG] looking for provider %s %s in plugin cache", provider.Name, version)
cached := i.Cache.CachedPluginPath("provider", provider.Name, version)
if cached == "" {
log.Printf("[DEBUG] %s %s not yet in cache, so downloading %s", provider, version, url)
log.Printf("[DEBUG] %s %s not yet in cache, so downloading %s", provider.Name, version, url)
err := getter.Get(i.Cache.InstallDir(), url)
if err != nil {
return err
}
// should now be in cache
cached = i.Cache.CachedPluginPath("provider", provider, version)
cached = i.Cache.CachedPluginPath("provider", provider.Name, version)
if cached == "" {
// should never happen if the getter is behaving properly
// and the plugins are packaged properly.
@ -307,7 +308,7 @@ func (i *ProviderInstaller) install(provider string, version Version, url string
return err
}
log.Printf("[DEBUG] installing %s %s to %s from local cache %s", provider, version, targetPath, cached)
log.Printf("[DEBUG] installing %s %s to %s from local cache %s", provider.Name, version, targetPath, cached)
// Delete if we can. If there's nothing there already then no harm done.
// This is important because we can't create a link if there's
@ -365,7 +366,7 @@ func (i *ProviderInstaller) install(provider string, version Version, url string
// One way or another, by the time we get here we should have either
// a link or a copy of the cached plugin within i.Dir, as expected.
} else {
log.Printf("[DEBUG] plugin cache is disabled, so downloading %s %s from %s", provider, version, url)
log.Printf("[DEBUG] plugin cache is disabled, so downloading %s %s from %s", provider.Name, version, url)
err := getter.Get(i.Dir, url)
if err != nil {
return err
@ -471,9 +472,9 @@ func (i *ProviderInstaller) hostname() (string, error) {
}
// list all versions available for the named provider
func (i *ProviderInstaller) listProviderVersions(name string) (*response.TerraformProviderVersions, error) {
provider := regsrc.NewTerraformProvider(name, i.OS, i.Arch)
versions, err := i.registry.TerraformProviderVersions(provider)
func (i *ProviderInstaller) listProviderVersions(provider addrs.ProviderType) (*response.TerraformProviderVersions, error) {
req := regsrc.NewTerraformProvider(provider.Name, i.OS, i.Arch)
versions, err := i.registry.TerraformProviderVersions(req)
return versions, err
}

View File

@ -17,6 +17,7 @@ import (
"strings"
"testing"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/registry"
"github.com/hashicorp/terraform/registry/response"
"github.com/hashicorp/terraform/svchost"
@ -149,7 +150,7 @@ func TestVersionListing(t *testing.T) {
i := newProviderInstaller(server)
allVersions, err := i.listProviderVersions("test")
allVersions, err := i.listProviderVersions(addrs.ProviderType{Name: "test"})
if err != nil {
t.Fatal(err)
@ -415,7 +416,8 @@ func TestProviderInstallerGet(t *testing.T) {
Ui: cli.NewMockUi(),
registry: registry.NewClient(Disco(server), nil),
}
_, _, err = i.Get("test", AllVersions)
_, _, err = i.Get(addrs.ProviderType{Name: "test"}, AllVersions)
if err != ErrorNoVersionCompatibleWithPlatform {
t.Fatal("want error for incompatible version")
@ -432,20 +434,21 @@ func TestProviderInstallerGet(t *testing.T) {
}
{
_, _, err := i.Get("test", ConstraintStr(">9.0.0").MustParse())
_, _, err := i.Get(addrs.ProviderType{Name: "test"}, ConstraintStr(">9.0.0").MustParse())
if err != ErrorNoSuitableVersion {
t.Fatal("want error for mismatching constraints")
}
}
{
_, _, err := i.Get("nonexist", AllVersions)
provider := addrs.ProviderType{Name: "nonexist"}
_, _, err := i.Get(provider, AllVersions)
if err != ErrorNoSuchProvider {
t.Fatal("want error for no such provider")
}
}
gotMeta, _, err := i.Get("test", AllVersions)
gotMeta, _, err := i.Get(addrs.ProviderType{Name: "test"}, AllVersions)
if err != nil {
t.Fatal(err)
}
@ -503,7 +506,7 @@ func TestProviderInstallerGet_cache(t *testing.T) {
Arch: "mockarch",
}
gotMeta, _, err := i.Get("test", AllVersions)
gotMeta, _, err := i.Get(addrs.ProviderType{Name: "test"}, AllVersions)
if err != nil {
t.Fatal(err)
}

View File

@ -14,6 +14,7 @@ import (
"io"
getter "github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform/addrs"
discovery "github.com/hashicorp/terraform/plugin/discovery"
"github.com/mitchellh/cli"
)
@ -181,7 +182,7 @@ func (c *PackageCommand) Run(args []string) int {
} else { //attempt to get from the public registry if not found locally
c.ui.Output(fmt.Sprintf("- Checking for provider plugin on %s...",
releaseHost))
_, _, err := installer.Get(name, constraint)
_, _, err := installer.Get(addrs.ProviderType{Name: name}, constraint)
if err != nil {
c.ui.Error(fmt.Sprintf("- Failed to resolve %s provider %s: %s", name, constraint, err))
return 1