registry/client: added a specific error if the registry client does not

support the requested service.
This commit is contained in:
Kristin Laemmert 2018-08-08 15:04:51 -07:00 committed by Martin Atkins
parent 63dcdbe948
commit bdaf8290b4
5 changed files with 32 additions and 12 deletions

View File

@ -121,6 +121,9 @@ func (i *ProviderInstaller) Get(provider string, req Constraints) (PluginMeta, e
// TODO: return multiple errors // TODO: return multiple errors
if err != nil { if err != nil {
if registry.IsServiceNotProvided(err) {
return PluginMeta{}, err
}
return PluginMeta{}, ErrorNoSuchProvider return PluginMeta{}, ErrorNoSuchProvider
} }
if len(allVersions.Versions) == 0 { if len(allVersions.Versions) == 0 {

View File

@ -58,7 +58,7 @@ func NewClient(services *disco.Disco, client *http.Client) *Client {
} }
} }
// Discover qeuries the host, and returns the url for the registry. // Discover queries the host, and returns the url for the registry.
func (c *Client) Discover(host svchost.Hostname, serviceID string) *url.URL { func (c *Client) Discover(host svchost.Hostname, serviceID string) *url.URL {
service := c.services.DiscoverServiceURL(host, serviceID) service := c.services.DiscoverServiceURL(host, serviceID)
if service == nil { if service == nil {
@ -79,7 +79,7 @@ func (c *Client) ModuleVersions(module *regsrc.Module) (*response.ModuleVersions
service := c.Discover(host, modulesServiceID) service := c.Discover(host, modulesServiceID)
if service == nil { if service == nil {
return nil, fmt.Errorf("host %s does not provide Terraform modules", host) return nil, &errServiceNotProvided{host: host.ForDisplay(), service: "modules"}
} }
p, err := url.Parse(path.Join(module.Module(), "versions")) p, err := url.Parse(path.Join(module.Module(), "versions"))
@ -152,7 +152,7 @@ func (c *Client) ModuleLocation(module *regsrc.Module, version string) (string,
service := c.Discover(host, modulesServiceID) service := c.Discover(host, modulesServiceID)
if service == nil { if service == nil {
return "", fmt.Errorf("host %s does not provide Terraform modules", host.ForDisplay()) return "", &errServiceNotProvided{host: host.ForDisplay(), service: "modules"}
} }
var p *url.URL var p *url.URL
@ -236,7 +236,7 @@ func (c *Client) TerraformProviderVersions(provider *regsrc.TerraformProvider) (
service := c.Discover(host, providersServiceID) service := c.Discover(host, providersServiceID)
if service == nil { if service == nil {
return nil, fmt.Errorf("host %s does not provide Terraform providers", host) return nil, &errServiceNotProvided{host: host.ForDisplay(), service: "providers"}
} }
p, err := url.Parse(path.Join(provider.TerraformProvider(), "versions")) p, err := url.Parse(path.Join(provider.TerraformProvider(), "versions"))
@ -290,7 +290,7 @@ func (c *Client) TerraformProviderLocation(provider *regsrc.TerraformProvider, v
service := c.Discover(host, providersServiceID) service := c.Discover(host, providersServiceID)
if service == nil { if service == nil {
return nil, fmt.Errorf("host %s does not provide Terraform providers", host) return nil, &errServiceNotProvided{host: host.ForDisplay(), service: "providers"}
} }
p, err := url.Parse(path.Join( p, err := url.Parse(path.Join(

View File

@ -214,7 +214,7 @@ func TestLookupProviderVersions(t *testing.T) {
{"bar"}, {"bar"},
} }
for _, tt := range tests { for _, tt := range tests {
provider, err := regsrc.NewTerraformProvider(tt.name, "", "") provider := regsrc.NewTerraformProvider(tt.name, "", "")
resp, err := client.TerraformProviderVersions(provider) resp, err := client.TerraformProviderVersions(provider)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -267,10 +267,8 @@ func TestLookupProviderLocation(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
// FIXME: the tests are set up to succeed - os/arch is not being validated at this time // FIXME: the tests are set up to succeed - os/arch is not being validated at this time
p, err := regsrc.NewTerraformProvider(tt.Name, "linux", "amd64") p := regsrc.NewTerraformProvider(tt.Name, "linux", "amd64")
if err != nil {
t.Fatal(err)
}
locationMetadata, err := client.TerraformProviderLocation(p, tt.Version) locationMetadata, err := client.TerraformProviderLocation(p, tt.Version)
if tt.Err { if tt.Err {
if err == nil { if err == nil {

View File

@ -37,3 +37,20 @@ func IsProviderNotFound(err error) bool {
_, ok := err.(*errProviderNotFound) _, ok := err.(*errProviderNotFound)
return ok return ok
} }
// IsServiceNotProvided returns true only if the given error is a "service not provided"
// error. This allows callers to recognize this particular error condition
// as distinct from operational errors such as poor network connectivity.
func IsServiceNotProvided(err error) bool {
_, ok := err.(*errServiceNotProvided)
return ok
}
type errServiceNotProvided struct {
host string
service string
}
func (e *errServiceNotProvided) Error() string {
return fmt.Sprintf("host %s does not provide %s", e.host, e.service)
}

View File

@ -19,6 +19,8 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
var releaseHost = "https://releases.hashicorp.com"
type PackageCommand struct { type PackageCommand struct {
ui cli.Ui ui cli.Ui
} }
@ -177,7 +179,7 @@ func (c *PackageCommand) Run(args []string) int {
CopyFile(plugin.Path, workDir+"/terraform-provider-"+plugin.Name+"_v"+plugin.Version.MustParse().String()) //put into temp dir CopyFile(plugin.Path, workDir+"/terraform-provider-"+plugin.Name+"_v"+plugin.Version.MustParse().String()) //put into temp dir
} else { //attempt to get from the public registry if not found locally } else { //attempt to get from the public registry if not found locally
c.ui.Output(fmt.Sprintf("- Checking for provider plugin on %s...", c.ui.Output(fmt.Sprintf("- Checking for provider plugin on %s...",
discovery.GetReleaseHost())) releaseHost))
_, err := installer.Get(name, constraint) _, err := installer.Get(name, constraint)
if err != nil { if err != nil {
c.ui.Error(fmt.Sprintf("- Failed to resolve %s provider %s: %s", name, constraint, err)) c.ui.Error(fmt.Sprintf("- Failed to resolve %s provider %s: %s", name, constraint, err))
@ -265,7 +267,7 @@ func (c *PackageCommand) bundleFilename(version discovery.VersionStr, time time.
func (c *PackageCommand) coreURL(version discovery.VersionStr, osName, archName string) string { func (c *PackageCommand) coreURL(version discovery.VersionStr, osName, archName string) string {
return fmt.Sprintf( return fmt.Sprintf(
"%s/terraform/%s/terraform_%s_%s_%s.zip", "%s/terraform/%s/terraform_%s_%s_%s.zip",
discovery.GetReleaseHost(), version, version, osName, archName, releaseHost, version, version, osName, archName,
) )
} }