Switch GCP list calls to paginated calls.

Three of the GCP list calls have paginated versions.
Switching those usages to the paginated versions.
Fixed go format.
This commit is contained in:
Walter Fender 2020-11-05 11:26:53 -08:00
parent 7b3c67b1c7
commit 1066d60630
2 changed files with 25 additions and 12 deletions

View File

@ -873,17 +873,20 @@ func getZonesForRegion(svc *compute.Service, projectID, region string) ([]string
// (tested in https://cloud.google.com/compute/docs/reference/latest/zones/list)
// listCall = listCall.Filter("region eq " + region)
res, err := listCall.Do()
var zones []string
var accumulator = func(response *compute.ZoneList) error {
for _, zone := range response.Items {
regionName := lastComponent(zone.Region)
if regionName == region {
zones = append(zones, zone.Name)
}
}
return nil
}
err := listCall.Pages(context.TODO(), accumulator)
if err != nil {
return nil, fmt.Errorf("unexpected response listing zones: %v", err)
}
zones := []string{}
for _, zone := range res.Items {
regionName := lastComponent(zone.Region)
if regionName == region {
zones = append(zones, zone.Name)
}
}
return zones, nil
}

View File

@ -126,22 +126,32 @@ func (g *Cloud) ListTPUs(ctx context.Context, zone string) ([]*tpuapi.Node, erro
mc := newTPUMetricContext("list", zone)
parent := getTPUParentName(g.projectID, zone)
response, err := g.tpuService.projects.Locations.Nodes.List(parent).Do()
var nodes []*tpuapi.Node
var accumulator = func(response *tpuapi.ListNodesResponse) error {
nodes = append(nodes, response.Nodes...)
return nil
}
err := g.tpuService.projects.Locations.Nodes.List(parent).Pages(ctx, accumulator)
if err != nil {
return nil, mc.Observe(err)
}
return response.Nodes, mc.Observe(nil)
return nodes, mc.Observe(nil)
}
// ListLocations returns the zones where Cloud TPUs are available.
func (g *Cloud) ListLocations(ctx context.Context) ([]*tpuapi.Location, error) {
mc := newTPUMetricContext("list_locations", "")
parent := getTPUProjectURL(g.projectID)
response, err := g.tpuService.projects.Locations.List(parent).Do()
var locations []*tpuapi.Location
var accumulator = func(response *tpuapi.ListLocationsResponse) error {
locations = append(locations, response.Locations...)
return nil
}
err := g.tpuService.projects.Locations.List(parent).Pages(ctx, accumulator)
if err != nil {
return nil, mc.Observe(err)
}
return response.Locations, mc.Observe(nil)
return locations, mc.Observe(nil)
}
// waitForTPUOp checks whether the op is done every 30 seconds before the ctx