aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/gce/gce.go
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2024-01-23 11:12:16 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-01-23 11:46:45 +0000
commitbf3b32eb43d4b6b620b3c2b9f2f54aeadc8f17f6 (patch)
tree6f9448268d2f483a22fac65d379f3050f6dff1ee /pkg/gce/gce.go
parent135863f822c9f4b7c4f15126c66b713e8c11b4c2 (diff)
vm/gce: use regional serial port connections
It's now the recommended approach. Co-authored-by: Alex Tyler <alextyler@google.com> Co-authored-by: Aleksandr Nogikh <nogikh@google.com>
Diffstat (limited to 'pkg/gce/gce.go')
-rw-r--r--pkg/gce/gce.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/pkg/gce/gce.go b/pkg/gce/gce.go
index 5e45cbce2..0072bd21f 100644
--- a/pkg/gce/gce.go
+++ b/pkg/gce/gce.go
@@ -17,6 +17,7 @@ import (
"io"
"math/rand"
"net/http"
+ "regexp"
"strings"
"time"
@@ -30,6 +31,7 @@ import (
type Context struct {
ProjectID string
ZoneID string
+ RegionID string
Instance string
InternalIP string
ExternalIP string
@@ -82,6 +84,13 @@ func NewContext(customZoneID string) (*Context, error) {
} else {
ctx.ZoneID = myZoneID
}
+ if !validateZone(ctx.ZoneID) {
+ return nil, fmt.Errorf("%q is not a valid zone name", ctx.ZoneID)
+ }
+ ctx.RegionID = zoneToRegion(ctx.ZoneID)
+ if ctx.RegionID == "" {
+ return nil, fmt.Errorf("failed to extract region id from %s", ctx.ZoneID)
+ }
ctx.Instance, err = ctx.getMeta("instance/name")
if err != nil {
return nil, fmt.Errorf("failed to query gce instance name: %w", err)
@@ -373,3 +382,15 @@ func (ctx *Context) apiCall(fn func() error) error {
return err
}
}
+
+var zoneNameRe = regexp.MustCompile("^[a-zA-Z0-9]*-[a-zA-Z0-9]*[-][a-zA-Z0-9]*$")
+
+func validateZone(zone string) bool {
+ return zoneNameRe.MatchString(zone)
+}
+
+var regionNameRe = regexp.MustCompile("^[a-zA-Z0-9]*-[a-zA-Z0-9]*")
+
+func zoneToRegion(zone string) string {
+ return regionNameRe.FindString(zone)
+}