From 320fcf4942fed56cefed94887b6c45f79a200cb0 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Thu, 23 Apr 2020 14:50:47 -0400 Subject: [PATCH] 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. --- internal/getproviders/multi_source.go | 22 ++++++++++++---------- internal/getproviders/multi_source_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/getproviders/multi_source.go b/internal/getproviders/multi_source.go index edd54786a..7b0334dfc 100644 --- a/internal/getproviders/multi_source.go +++ b/internal/getproviders/multi_source.go @@ -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) } diff --git a/internal/getproviders/multi_source_test.go b/internal/getproviders/multi_source_test.go index 23549de70..01dcba93b 100644 --- a/internal/getproviders/multi_source_test.go +++ b/internal/getproviders/multi_source_test.go @@ -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": {