-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
zone.go
51 lines (46 loc) · 1.31 KB
/
zone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package gce
import (
"encoding/json"
"fmt"
"net/http"
)
func getZonesForProject(client *http.Client, project string) ([]string, error) {
// See https://cloud.google.com/compute/docs/reference/rest/v1/zones
zonesURL := fmt.Sprintf("https://compute.googleapis.com/compute/v1/projects/%s/zones", project)
var zones []string
pageToken := ""
for {
data, err := getAPIResponse(client, zonesURL, "", pageToken)
if err != nil {
return nil, fmt.Errorf("cannot obtain zones: %w", err)
}
zl, err := parseZoneList(data)
if err != nil {
return nil, fmt.Errorf("cannot parse zone list from %q: %w", zonesURL, err)
}
for _, z := range zl.Items {
zones = append(zones, z.Name)
}
if len(zl.NextPageToken) == 0 {
return zones, nil
}
pageToken = zl.NextPageToken
}
}
// ZoneList is response to https://cloud.google.com/compute/docs/reference/rest/v1/zones/list
type ZoneList struct {
Items []Zone
NextPageToken string
}
// Zone is zone from https://cloud.google.com/compute/docs/reference/rest/v1/zones/list
type Zone struct {
Name string
}
// parseZoneList parses ZoneList from data.
func parseZoneList(data []byte) (*ZoneList, error) {
var zl ZoneList
if err := json.Unmarshal(data, &zl); err != nil {
return nil, fmt.Errorf("cannot unmarshal ZoneList from %q: %w", data, err)
}
return &zl, nil
}