Skip to content

Commit

Permalink
Allow the use of region selflinks in provider configs. (#1022)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and paddycarver committed Aug 12, 2019
1 parent 9ad3f85 commit 73c8ee8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions google-beta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ func (c *Config) LoadAndValidate() error {
c.clientHealthcare.UserAgent = userAgent
c.clientHealthcare.BasePath = healthcareClientBasePath

c.Region = GetRegionFromRegionSelfLink(c.Region)
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions google-beta/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions google-beta/self_link_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions google-beta/self_link_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 73c8ee8

Please sign in to comment.