Use registry alias to fetch providers

This commit is contained in:
findkim 2018-11-28 10:26:16 -06:00
parent 65080b9ce3
commit 5e06e39fcc
5 changed files with 83 additions and 8 deletions

View File

@ -134,6 +134,7 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, e
if len(allVersions.Versions) == 0 { if len(allVersions.Versions) == 0 {
return PluginMeta{}, ErrorNoSuitableVersion return PluginMeta{}, ErrorNoSuitableVersion
} }
providerSource := allVersions.ID
// Filter the list of plugin versions to those which meet the version constraints // Filter the list of plugin versions to those which meet the version constraints
versions := allowedVersions(allVersions, req) versions := allowedVersions(allVersions, req)
@ -175,7 +176,7 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, e
return PluginMeta{}, ErrorNoVersionCompatibleWithPlatform return PluginMeta{}, ErrorNoVersionCompatibleWithPlatform
} }
downloadURLs, err := i.listProviderDownloadURLs(provider, versionMeta.Version) downloadURLs, err := i.listProviderDownloadURLs(providerSource, versionMeta.Version)
providerURL := downloadURLs.DownloadURL providerURL := downloadURLs.DownloadURL
i.Ui.Info(fmt.Sprintf("- Downloading plugin for provider %q (%s)...", provider, versionMeta.Version)) i.Ui.Info(fmt.Sprintf("- Downloading plugin for provider %q (%s)...", provider, versionMeta.Version))
@ -193,6 +194,9 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, e
} }
} }
printedProviderName := fmt.Sprintf("%s (%s)", provider, providerSource)
i.Ui.Info(fmt.Sprintf("- Downloading plugin for provider %q (%s)...", printedProviderName, versionMeta.Version))
log.Printf("[DEBUG] getting provider %q version %q", printedProviderName, versionMeta.Version)
err = i.install(provider, v, providerURL) err = i.install(provider, v, providerURL)
if err != nil { if err != nil {
return PluginMeta{}, err return PluginMeta{}, err

View File

@ -130,6 +130,7 @@ func testHandler(w http.ResponseWriter, r *http.Request) {
func testReleaseServer() *httptest.Server { func testReleaseServer() *httptest.Server {
handler := http.NewServeMux() handler := http.NewServeMux()
handler.HandleFunc("/v1/providers/-/", testHandler)
handler.HandleFunc("/v1/providers/terraform-providers/", testHandler) handler.HandleFunc("/v1/providers/terraform-providers/", testHandler)
handler.HandleFunc("/terraform-provider-template/", testChecksumHandler) handler.HandleFunc("/terraform-provider-template/", testChecksumHandler)
handler.HandleFunc("/terraform-provider-badsig/", testChecksumHandler) handler.HandleFunc("/terraform-provider-badsig/", testChecksumHandler)
@ -479,7 +480,7 @@ func Disco(s *httptest.Server) *disco.Disco {
} }
var versionList = response.TerraformProvider{ var versionList = response.TerraformProvider{
ID: "test", ID: "terraform-providers/test",
Versions: []*response.TerraformProviderVersion{ Versions: []*response.TerraformProviderVersion{
{Version: "1.2.1"}, {Version: "1.2.1"},
{Version: "1.2.3"}, {Version: "1.2.3"},

View File

@ -3,6 +3,7 @@ package regsrc
import ( import (
"fmt" "fmt"
"runtime" "runtime"
"strings"
"github.com/hashicorp/terraform/svchost" "github.com/hashicorp/terraform/svchost"
) )
@ -10,7 +11,7 @@ import (
var ( var (
// DefaultProviderNamespace represents the namespace for canonical // DefaultProviderNamespace represents the namespace for canonical
// HashiCorp-controlled providers. // HashiCorp-controlled providers.
DefaultProviderNamespace = "terraform-providers" DefaultProviderNamespace = "-"
) )
// TerraformProvider describes a Terraform Registry Provider source. // TerraformProvider describes a Terraform Registry Provider source.
@ -31,9 +32,14 @@ func NewTerraformProvider(name, os, arch string) *TerraformProvider {
arch = runtime.GOARCH arch = runtime.GOARCH
} }
// separate namespace if included
namespace := DefaultProviderNamespace
if names := strings.SplitN(name, "/", 2); len(names) == 2 {
namespace, name = names[0], names[1]
}
p := &TerraformProvider{ p := &TerraformProvider{
RawHost: PublicRegistryHost, RawHost: PublicRegistryHost,
RawNamespace: DefaultProviderNamespace, RawNamespace: namespace,
RawName: name, RawName: name,
OS: os, OS: os,
Arch: arch, Arch: arch,

View File

@ -0,0 +1,48 @@
package regsrc
import (
"testing"
)
func TestNewTerraformProviderNamespace(t *testing.T) {
tests := []struct {
name string
provider string
expectedNamespace string
expectedName string
}{
{
name: "default",
provider: "null",
expectedNamespace: "-",
expectedName: "null",
}, {
name: "explicit",
provider: "terraform-providers/null",
expectedNamespace: "terraform-providers",
expectedName: "null",
}, {
name: "community",
provider: "community-providers/null",
expectedNamespace: "community-providers",
expectedName: "null",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := NewTerraformProvider(tt.provider, "", "")
if actual == nil {
t.Fatal("NewTerraformProvider() unexpectedly returned nil provider")
}
if v := actual.RawNamespace; v != tt.expectedNamespace {
t.Fatalf("RawNamespace = %v, wanted %v", v, tt.expectedNamespace)
}
if v := actual.RawName; v != tt.expectedName {
t.Fatalf("RawName = %v, wanted %v", v, tt.expectedName)
}
})
}
}

View File

@ -100,14 +100,14 @@ var testMods = map[string][]testMod{
} }
var testProviders = map[string][]testProvider{ var testProviders = map[string][]testProvider{
"terraform-providers/foo": { "-/foo": {
{ {
version: "0.2.3", version: "0.2.3",
url: "https://releases.hashicorp.com/terraform-provider-foo/0.2.3/terraform-provider-foo.zip", url: "https://releases.hashicorp.com/terraform-provider-foo/0.2.3/terraform-provider-foo.zip",
}, },
{version: "0.3.0"}, {version: "0.3.0"},
}, },
"terraform-providers/bar": { "-/bar": {
{ {
version: "0.1.1", version: "0.1.1",
url: "https://releases.hashicorp.com/terraform-provider-bar/0.1.1/terraform-provider-bar.zip", url: "https://releases.hashicorp.com/terraform-provider-bar/0.1.1/terraform-provider-bar.zip",
@ -116,6 +116,22 @@ var testProviders = map[string][]testProvider{
}, },
} }
func providerAlias(provider string) string {
re := regexp.MustCompile("^-/")
if re.MatchString(provider) {
return re.ReplaceAllString(provider, "terraform-providers/")
}
return provider
}
func init() {
// Add provider aliases
for provider, info := range testProviders {
alias := providerAlias(provider)
testProviders[alias] = info
}
}
func latestVersion(versions []string) string { func latestVersion(versions []string) string {
var col version.Collection var col version.Collection
for _, v := range versions { for _, v := range versions {
@ -287,7 +303,7 @@ func mockRegHandler() http.Handler {
} }
} }
name := fmt.Sprintf("%s", matches[1]) name := providerAlias(fmt.Sprintf("%s", matches[1]))
versions, ok := testProviders[name] versions, ok := testProviders[name]
if !ok { if !ok {
http.NotFound(w, r) http.NotFound(w, r)
@ -295,7 +311,7 @@ func mockRegHandler() http.Handler {
} }
// only adding the single requested provider for now // only adding the single requested provider for now
// this is the minimal that any regisry is epected to support // this is the minimal that any registry is expected to support
pvs := &response.TerraformProviderVersions{ pvs := &response.TerraformProviderVersions{
ID: name, ID: name,
} }