mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-28 09:55:36 +01:00
d5dddb0953
This will simplify examining the returned errors such as httpserver.ErrorWithStatusCode . See https://blog.golang.org/go1.13-errors for details.
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package gce
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
func getZonesForProject(client *http.Client, project, filter 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, filter, 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
|
|
}
|