Skip to content

Commit

Permalink
Allow the use of region selflinks in provider configs.
Browse files Browse the repository at this point in the history
When configuring the provider, currently we only accept the name of a
region. As per hashicorp/terraform-provider-google#4133, being
able to use self_links for regions here would be helpful. I added a
utility function to pull the region's name from a self_link, tests for
that behavior, and then used it when setting up our Config object. It'll
either return what the user passed in, or if it recognizes it as a
region self_link, it'll just return the name part of the region. I also
left it open to expand on potentially more formats in the future.
  • Loading branch information
paddycarver committed Aug 1, 2019
1 parent bb4c89a commit 963d3f1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
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 963d3f1

Please sign in to comment.