Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the use of region selflinks in provider configs. #2156

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}