move Svchost method to regsrc.Module

The level of abstraction that needs the "svchost" is the Module, not the
FriendlyHost. Us the new method in the module package for registry
interaction.
This commit is contained in:
James Bardin 2017-11-20 17:43:36 -05:00
parent bd576d780a
commit 8091bd627d
4 changed files with 30 additions and 18 deletions

View File

@ -44,8 +44,8 @@ func (e errModuleNotFound) Error() string {
return `module "` + string(e) + `" not found`
}
func (s *Storage) discoverRegURL(module *regsrc.Module) *url.URL {
regURL := s.Services.DiscoverServiceURL(svchost.Hostname(module.RawHost.Normalized()), serviceID)
func (s *Storage) discoverRegURL(host svchost.Hostname) *url.URL {
regURL := s.Services.DiscoverServiceURL(host, serviceID)
if regURL == nil {
return nil
}
@ -75,13 +75,14 @@ func (s *Storage) addRequestCreds(host svchost.Hostname, req *http.Request) {
// Lookup module versions in the registry.
func (s *Storage) lookupModuleVersions(module *regsrc.Module) (*response.ModuleVersions, error) {
if module.RawHost == nil {
module.RawHost = regsrc.NewFriendlyHost(defaultRegistry)
host, err := module.SvcHost()
if err != nil {
return nil, err
}
service := s.discoverRegURL(module)
service := s.discoverRegURL(host)
if service == nil {
return nil, fmt.Errorf("host %s does not provide Terraform modules", module.RawHost.Display())
return nil, fmt.Errorf("host %s does not provide Terraform modules", host)
}
p, err := url.Parse(path.Join(module.Module(), "versions"))
@ -98,7 +99,7 @@ func (s *Storage) lookupModuleVersions(module *regsrc.Module) (*response.ModuleV
return nil, err
}
s.addRequestCreds(svchost.Hostname(module.RawHost.Normalized()), req)
s.addRequestCreds(host, req)
req.Header.Set(xTerraformVersion, tfVersion)
resp, err := httpClient.Do(req)
@ -134,17 +135,17 @@ func (s *Storage) lookupModuleVersions(module *regsrc.Module) (*response.ModuleV
// lookup the location of a specific module version in the registry
func (s *Storage) lookupModuleLocation(module *regsrc.Module, version string) (string, error) {
if module.RawHost == nil {
module.RawHost = regsrc.NewFriendlyHost(defaultRegistry)
host, err := module.SvcHost()
if err != nil {
return "", err
}
service := s.discoverRegURL(module)
service := s.discoverRegURL(host)
if service == nil {
return "", fmt.Errorf("host %s does not provide Terraform modules", module.RawHost.Display())
return "", fmt.Errorf("host %s does not provide Terraform modules", host.ForDisplay())
}
var p *url.URL
var err error
if version == "" {
p, err = url.Parse(path.Join(module.Module(), "download"))
} else {
@ -162,7 +163,7 @@ func (s *Storage) lookupModuleLocation(module *regsrc.Module, version string) (s
return "", err
}
s.addRequestCreds(svchost.Hostname(module.RawHost.Normalized()), req)
s.addRequestCreds(host, req)
req.Header.Set(xTerraformVersion, tfVersion)
resp, err := httpClient.Do(req)

View File

@ -343,7 +343,9 @@ func (s Storage) findRegistryModule(mSource, constraint string) (moduleRecord, e
return rec, err
}
s.output(fmt.Sprintf(" Found version %s of %s on %s", rec.Version, mod.Module(), mod.RawHost.Display()))
// we've already validated this by now
host, _ := mod.SvcHost()
s.output(fmt.Sprintf(" Found version %s of %s on %s", rec.Version, mod.Module(), host.ForDisplay()))
}
return rec, nil

View File

@ -128,7 +128,3 @@ func (h *FriendlyHost) Equal(other *FriendlyHost) bool {
return h.Normalized() == other.Normalized()
}
func (h *FriendlyHost) SvcHost() (svchost.Hostname, error) {
return svchost.ForComparison(h.Raw)
}

View File

@ -5,6 +5,8 @@ import (
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform/svchost"
)
var (
@ -190,3 +192,14 @@ func (m *Module) formatWithPrefix(hostPrefix string, preserveCase bool) string {
func (m *Module) Module() string {
return fmt.Sprintf("%s/%s/%s", m.RawNamespace, m.RawName, m.RawProvider)
}
// SvcHost returns the svchost.Hostname for this module. Since FriendlyHost may
// contain an invalid hostname, this also returns an error indicating if it
// could be converted to a svchost.Hostname. If no host is specified, the
// default PublicRegistryHost is returned.
func (m *Module) SvcHost() (svchost.Hostname, error) {
if m.RawHost == nil {
return svchost.ForComparison(PublicRegistryHost.Raw)
}
return svchost.ForComparison(m.RawHost.Raw)
}