Skip to content

Commit 7863beb

Browse files
Fix the bug when validating subnetwork project for compute instance (#13823) (#22571)
[upstream:62565ce9dcdb4d13344e26cc63ff2427183c6461] Signed-off-by: Modular Magician <[email protected]>
1 parent 596b810 commit 7863beb

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

.changelog/13823.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed the bug when validating the subnetwork project in `google_compute_instance` resource
3+
```

google/services/compute/resource_compute_instance.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ func ValidateSubnetworkProjectFunc(d tpgresource.TerraformResourceDiff) error {
136136
return nil
137137
}
138138

139-
if tpgresource.GetProjectFromRegionalSelfLink(subnetwork.(string)) != subnetworkProject.(string) {
140-
return fmt.Errorf("project in subnetwork's self_link %q must match subnetwork_project %q", subnetwork, subnetworkProject)
139+
project := tpgresource.GetProjectFromRegionalSelfLink(subnetwork.(string))
140+
if project != subnetworkProject.(string) {
141+
return fmt.Errorf("project %s in subnetwork's self_link %q must match subnetwork_project %q", project, subnetwork, subnetworkProject)
141142
}
142143
}
143144
return nil

google/tpgresource/self_link_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func GetRegionFromRegionalSelfLink(selfLink string) string {
191191
}
192192

193193
func GetProjectFromRegionalSelfLink(selfLink string) string {
194-
re := regexp.MustCompile("projects/([a-zA-Z0-9-:]*)/(?:locations|regions)/[a-zA-Z0-9-:]*")
194+
re := regexp.MustCompile("projects/([a-zA-Z0-9-:.]*)/(?:locations|regions)/[a-zA-Z0-9-:]*")
195195
switch {
196196
case re.MatchString(selfLink):
197197
if res := re.FindStringSubmatch(selfLink); len(res) == 2 && res[1] != "" {

google/tpgresource/self_link_helpers_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ func TestGetRegionFromRegionalSelfLink(t *testing.T) {
191191

192192
func TestGetProjectFromRegionalSelfLink(t *testing.T) {
193193
cases := map[string]string{
194-
"projects/foo/locations/europe-north1/datasets/bar/operations/foobar": "foo",
195-
"projects/REDACTED/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "REDACTED",
196-
"projects/REDA:CT-ED09/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "REDA:CT-ED09",
194+
"projects/foo/locations/europe-north1/datasets/bar/operations/foobar": "foo",
195+
"projects/REDACTED/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "REDACTED",
196+
"projects/REDA:CT-ED09/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "REDA:CT-ED09",
197+
"projects/REDA.com:CT-ED09/regions/europe-north1/subnetworks/tf-test-net-xbwhsmlfm8": "REDA.com:CT-ED09",
197198
}
198199
for input, expected := range cases {
199200
if result := GetProjectFromRegionalSelfLink(input); result != expected {

0 commit comments

Comments
 (0)