Skip to content

Commit

Permalink
Allow the use of region selflinks in provider configs. (#2156)
Browse files Browse the repository at this point in the history
Merged PR #2156.
  • Loading branch information
paddycarver authored and modular-magician committed Aug 12, 2019
1 parent 480803d commit ca5f20e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
1 change: 1 addition & 0 deletions third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ func (c *Config) LoadAndValidate() error {
c.clientHealthcare.BasePath = healthcareClientBasePath
<% end -%>

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

Expand Down
12 changes: 12 additions & 0 deletions third_party/terraform/utils/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 third_party/terraform/utils/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 ca5f20e

Please sign in to comment.