Skip to content

Commit daac2c6

Browse files
authored
Merge pull request #1910 from hanz-vora/get-region-from-location-tpc-fix
Add handling for TPC to GetRegionFromZones
2 parents ba89880 + 47b6ecb commit daac2c6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Diff for: pkg/common/utils.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,22 @@ func NodeIDToZoneAndName(id string) (string, string, error) {
184184
}
185185

186186
func GetRegionFromZones(zones []string) (string, error) {
187+
const tpcPrefix = "u"
187188
regions := sets.String{}
188189
if len(zones) < 1 {
189190
return "", fmt.Errorf("no zones specified")
190191
}
191192
for _, zone := range zones {
192193
// Zone expected format {locale}-{region}-{zone}
194+
// TPC zone expected format is u-{}-{}-{}, e.g. u-europe-central2-a. See go/tpc-region-naming.
193195
splitZone := strings.Split(zone, "-")
194-
if len(splitZone) != 3 {
195-
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone}, got: %v", zone)
196+
if len(splitZone) == 3 {
197+
regions.Insert(strings.Join(splitZone[0:2], "-"))
198+
} else if len(splitZone) == 4 && splitZone[0] == tpcPrefix {
199+
regions.Insert(strings.Join(splitZone[0:3], "-"))
200+
} else {
201+
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone} or u-{locale}-{region}-{zone}, got: %v", zone)
196202
}
197-
regions.Insert(strings.Join(splitZone[0:2], "-"))
198203
}
199204
if regions.Len() != 1 {
200205
return "", fmt.Errorf("multiple or no regions gotten from zones, got: %v", regions)

Diff for: pkg/common/utils_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ func TestGetRegionFromZones(t *testing.T) {
293293
zones: []string{"blah/blooh"},
294294
expErr: true,
295295
},
296+
{
297+
name: "tpc zone",
298+
zones: []string{"u-europe-central2-a"},
299+
expRegion: "u-europe-central2",
300+
},
301+
{
302+
name: "malformed tpc zone",
303+
zones: []string{"us-europe-central2-a"},
304+
expErr: true,
305+
},
296306
}
297307
for _, tc := range testCases {
298308
t.Logf("test case: %s", tc.name)

0 commit comments

Comments
 (0)