Skip to content

Commit c1e799d

Browse files
committed
Add handling for TPC to GetRegionFromZones
1 parent 9090993 commit c1e799d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pkg/common/utils.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,22 @@ func NodeIDToZoneAndName(id string) (string, string, error) {
180180
}
181181

182182
func GetRegionFromZones(zones []string) (string, error) {
183+
const tpcPrefix = "u"
183184
regions := sets.String{}
184185
if len(zones) < 1 {
185186
return "", fmt.Errorf("no zones specified")
186187
}
187188
for _, zone := range zones {
188189
// Zone expected format {locale}-{region}-{zone}
190+
// TPC zone expected format is u-{}-{}-{}, e.g. u-europe-central2-a. See go/tpc-region-naming.
189191
splitZone := strings.Split(zone, "-")
190-
if len(splitZone) != 3 {
191-
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone}, got: %v", zone)
192+
if len(splitZone) == 3 {
193+
regions.Insert(strings.Join(splitZone[0:2], "-"))
194+
} else if len(splitZone) == 4 && splitZone[0] == tpcPrefix {
195+
regions.Insert(strings.Join(splitZone[0:3], "-"))
196+
} else {
197+
return "", fmt.Errorf("zone in unexpected format, expected: {locale}-{region}-{zone} or u-{locale}-{region}-{zone}, got: %v", zone)
192198
}
193-
regions.Insert(strings.Join(splitZone[0:2], "-"))
194199
}
195200
if regions.Len() != 1 {
196201
return "", fmt.Errorf("multiple or no regions gotten from zones, got: %v", regions)

pkg/common/utils_test.go

Lines changed: 10 additions & 0 deletions
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)