internal/getproviders: apply case normalizations in ParseMultiSourceMatchingPatterns (#24753)

* internal/getproviders: apply case normalizations in ParseMultiSourceMatchingPatterns

This is a very minor refactor which takes advantage of addrs.ParseProviderPart case normalization to normalize non-wildcard sources.
This commit is contained in:
Kristin Laemmert 2020-04-23 14:50:47 -04:00 committed by GitHub
parent 1ce3c60693
commit 320fcf4942
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 10 deletions

View File

@ -119,8 +119,9 @@ type MultiSourceSelector struct {
type MultiSourceMatchingPatterns []addrs.Provider
// ParseMultiSourceMatchingPatterns parses a slice of strings containing the
// string form of provider matching patterns and, if all the given strings
// are valid, returns the corresponding MultiSourceMatchingPatterns value.
// string form of provider matching patterns and, if all the given strings are
// valid, returns the corresponding, normalized, MultiSourceMatchingPatterns
// value.
func ParseMultiSourceMatchingPatterns(strs []string) (MultiSourceMatchingPatterns, error) {
if len(strs) == 0 {
return nil, nil
@ -151,17 +152,19 @@ func ParseMultiSourceMatchingPatterns(strs []string) (MultiSourceMatchingPattern
parts = parts[1:]
}
if !validProviderNameOrWildcard(parts[1]) {
pType, err := normalizeProviderNameOrWildcard(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid provider type %q in provider matching pattern %q: must either be the wildcard * or a provider type name", parts[1], str)
}
if !validProviderNameOrWildcard(parts[0]) {
namespace, err := normalizeProviderNameOrWildcard(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid registry namespace %q in provider matching pattern %q: must either be the wildcard * or a literal namespace", parts[1], str)
}
ret[i] = addrs.Provider{
Hostname: host,
Namespace: parts[0],
Type: parts[1],
Namespace: namespace,
Type: pType,
}
if ret[i].Hostname == svchost.Hostname(Wildcard) && !(ret[i].Namespace == Wildcard && ret[i].Type == Wildcard) {
@ -215,10 +218,9 @@ const Wildcard string = "*"
// by definition.
var defaultRegistryHost = addrs.DefaultRegistryHost
func validProviderNameOrWildcard(s string) bool {
func normalizeProviderNameOrWildcard(s string) (string, error) {
if s == Wildcard {
return true
return s, nil
}
_, err := addrs.ParseProviderPart(s)
return err == nil
return addrs.ParseProviderPart(s)
}

View File

@ -323,6 +323,14 @@ func TestMultiSourceSelector(t *testing.T) {
addrs.NewDefaultProvider("foo"),
true,
},
"default provider with non-normalized include constraint that matches it via type wildcard": {
MultiSourceSelector{
Source: emptySource,
Include: mustParseMultiSourceMatchingPatterns("HashiCorp/*"),
},
addrs.NewDefaultProvider("foo"),
true,
},
"built-in provider with exact include constraint that does not match it": {
MultiSourceSelector{
Source: emptySource,
@ -383,6 +391,14 @@ func TestMultiSourceSelector(t *testing.T) {
addrs.NewDefaultProvider("foo"),
true,
},
"default provider with non-normalized exclude constraint that matches it via type wildcard": {
MultiSourceSelector{
Source: emptySource,
Exclude: mustParseMultiSourceMatchingPatterns("HashiCorp/*"),
},
addrs.NewDefaultProvider("foo"),
false,
},
// Both include and exclude in a single selector
"default provider with exclude wildcard overriding include exact": {