From bc13f0a398bc300bc731aa4dc22b823ddabc451e Mon Sep 17 00:00:00 2001 From: Paddy Date: Mon, 12 Aug 2019 20:09:16 +0000 Subject: [PATCH] Allow the use of region selflinks in provider configs. Signed-off-by: Modular Magician --- google/config.go | 1 + google/iam.go | 5 +---- google/self_link_helpers.go | 12 ++++++++++++ google/self_link_helpers_test.go | 12 ++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/google/config.go b/google/config.go index b3e166ba2c7..e9201fdafc3 100644 --- a/google/config.go +++ b/google/config.go @@ -540,6 +540,7 @@ func (c *Config) LoadAndValidate() error { c.clientStorageTransfer.UserAgent = userAgent c.clientStorageTransfer.BasePath = storageTransferClientBasePath + c.Region = GetRegionFromRegionSelfLink(c.Region) return nil } diff --git a/google/iam.go b/google/iam.go index c9814e2f4ff..a667707e839 100644 --- a/google/iam.go +++ b/google/iam.go @@ -229,10 +229,7 @@ func mergeAuditConfigs(auditConfigs []*cloudresourcemanager.AuditConfig) []*clou // Flattens AuditConfigs so each role has a single Binding with combined members\ func removeAllAuditConfigsWithService(ac []*cloudresourcemanager.AuditConfig, service string) []*cloudresourcemanager.AuditConfig { acMap := createIamAuditConfigsMap(ac) - if _, ok := acMap[service]; ok { - delete(acMap, service) - } - + delete(acMap, service) return listFromIamAuditConfigMap(acMap) } diff --git a/google/self_link_helpers.go b/google/self_link_helpers.go index 1ae06e1fd1a..fff3e87f6f5 100644 --- a/google/self_link_helpers.go +++ b/google/self_link_helpers.go @@ -145,3 +145,15 @@ func GetLocationalResourcePropertiesFromSelfLinkString(selfLink string) (string, s := strings.Split(parsed.Path, "/") return s[4], s[6], s[8], nil } + +// return the region a selfLink is referring to +func GetRegionFromRegionSelfLink(selfLink string) string { + re := regexp.MustCompile("/compute/[a-zA-Z0-9]*/projects/[a-zA-Z0-9-]*/regions/([a-zA-Z0-9-]*)") + switch { + case re.MatchString(selfLink): + if res := re.FindStringSubmatch(selfLink); len(res) == 2 && res[1] != "" { + return res[1] + } + } + return selfLink +} diff --git a/google/self_link_helpers_test.go b/google/self_link_helpers_test.go index d6aed6a6937..a9b67434473 100644 --- a/google/self_link_helpers_test.go +++ b/google/self_link_helpers_test.go @@ -110,3 +110,15 @@ func TestSelfLinkNameHash(t *testing.T) { } } } + +func TestGetRegionFromRegionSelfLink(t *testing.T) { + cases := map[string]string{ + "https://www.googleapis.com/compute/v1/projects/test/regions/europe-west3": "europe-west3", + "europe-west3": "europe-west3", + } + for input, expected := range cases { + if result := GetRegionFromRegionSelfLink(input); result != expected { + t.Errorf("expected to get %q from %q, got %q", expected, input, result) + } + } +}