Skip to content

Commit b8bc1c0

Browse files
Add regex to handle image link in universes (#14154) (#10129)
[upstream:b745b64db2ca68a03b980a4f991ee3894b8b0c13] Signed-off-by: Modular Magician <[email protected]>
1 parent 81a7d35 commit b8bc1c0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

google-beta/services/compute/image.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import (
2929
)
3030

3131
const (
32-
resolveImageFamilyRegex = "[-_a-zA-Z0-9]*"
33-
resolveImageImageRegex = "[-_a-zA-Z0-9]*"
32+
resolveImageFamilyRegex = "[-_a-zA-Z0-9]*"
33+
resolveImageImageRegex = "[-_a-zA-Z0-9]*"
34+
resolveImageUniverseRegex = "[-_a-zA-Z0-9.]*"
3435
)
3536

3637
var (
@@ -44,6 +45,7 @@ var (
4445
resolveImageFamily = regexp.MustCompile(fmt.Sprintf("^(%s)$", resolveImageFamilyRegex))
4546
resolveImageImage = regexp.MustCompile(fmt.Sprintf("^(%s)$", resolveImageImageRegex))
4647
resolveImageLink = regexp.MustCompile(fmt.Sprintf("^https://www.googleapis.com/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)", verify.ProjectRegex, resolveImageImageRegex))
48+
resolveImageUniverseLink = regexp.MustCompile(fmt.Sprintf("^https://compute.%s/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)", resolveImageUniverseRegex, verify.ProjectRegex, resolveImageImageRegex))
4749

4850
windowsSqlImage = regexp.MustCompile("^sql-(?:server-)?([0-9]{4})-([a-z]+)-windows-(?:server-)?([0-9]{4})(?:-r([0-9]+))?-dc-v[0-9]+$")
4951
canonicalUbuntuLtsImage = regexp.MustCompile("^ubuntu-(minimal-)?([0-9]+)(?:.*(arm64|amd64))?.*$")
@@ -122,12 +124,12 @@ func ResolveImage(c *transport_tpg.Config, project, name, userAgent string) (str
122124
break
123125
}
124126
}
125-
if c.UniverseDomain != "" && c.UniverseDomain != "googleapis.com" {
126-
resolveImageLink = regexp.MustCompile(fmt.Sprintf("^https://compute.%s/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)", c.UniverseDomain, verify.ProjectRegex, resolveImageImageRegex))
127-
}
127+
128128
switch {
129129
case resolveImageLink.MatchString(name): // https://www.googleapis.com/compute/v1/projects/xyz/global/images/xyz
130130
return name, nil
131+
case resolveImageUniverseLink.MatchString(name): // https://compute.xyz/compute/[a-z0-9]+/projects/xyz/global/images/xyz
132+
return name, nil
131133
case resolveImageProjectImage.MatchString(name): // projects/xyz/global/images/xyz
132134
res := resolveImageProjectImage.FindStringSubmatch(name)
133135
if err := sanityTestRegexMatches(2, res, "project image", name); err != nil {

google-beta/services/compute/resource_compute_disk.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ func IsDiskShrinkage(_ context.Context, old, new, _ interface{}) bool {
7878
return new.(int) < old.(int)
7979
}
8080

81+
func matchImageLink(old string) (string, string, bool) {
82+
// 'old' is read from the API.
83+
// In GCP It has the format 'https://www.googleapis.com/compute/v1/projects/(%s)/global/images/(%s)'
84+
matches := resolveImageLink.FindStringSubmatch(old)
85+
if matches == nil {
86+
// In alternate universes, it has the format https://compute.%s/compute/[a-z0-9]+/projects/(%s)/global/images/(%s)
87+
matches = resolveImageUniverseLink.FindStringSubmatch(old)
88+
if matches == nil {
89+
return "", "", false
90+
}
91+
}
92+
return matches[1], matches[2], true
93+
}
94+
8195
// We cannot suppress the diff for the case when family name is not part of the image name since we can't
8296
// make a network call in a DiffSuppressFunc.
8397
func DiskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
@@ -88,15 +102,10 @@ func DiskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
88102
// In order to keep this maintainable, we need to ensure that the positive and negative examples
89103
// in resource_compute_disk_test.go are as complete as possible.
90104

91-
// 'old' is read from the API.
92-
// It always has the format 'https://www.googleapis.com/compute/v1/projects/(%s)/global/images/(%s)'
93-
matches := resolveImageLink.FindStringSubmatch(old)
94-
if matches == nil {
95-
// Image read from the API doesn't have the expected format. In practice, it should never happen
105+
oldProject, oldName, matched := matchImageLink(old)
106+
if matched == false {
96107
return false
97108
}
98-
oldProject := matches[1]
99-
oldName := matches[2]
100109

101110
// Partial or full self link family
102111
if resolveImageProjectFamily.MatchString(new) {

0 commit comments

Comments
 (0)