Skip to content

Commit

Permalink
Adjust implementation to using the meta field from the godo response
Browse files Browse the repository at this point in the history
Also update the fake response to return a meta field.
  • Loading branch information
Timo Reimann committed Dec 16, 2019
1 parent fe4864b commit 5522c0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
55 changes: 23 additions & 32 deletions driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package driver

import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -1014,40 +1015,30 @@ func (d *Driver) checkLimit(ctx context.Context) (*limitDetails, error) {
return nil, nil // hail to the king!
}

opt := &godo.ListOptions{
Page: 1,
PerPage: 50,
// The API returns the limit for *all* regions, so passing the region
// down as a parameter doesn't change the response. Nevertheless, this
// is something we should be aware of.
_, resp, err := d.storage.ListVolumes(ctx, &godo.ListVolumeParams{
Region: d.region,
ListOptions: &godo.ListOptions{
Page: 1,
PerPage: 1,
},
})
if err != nil {
return nil, fmt.Errorf("failed to list volumes: %s", err)
}
var numVolumes int
for {
// The API returns the limit for *all* regions, so passing the region
// down as a parameter doesn't change the response. Nevertheless, this
// is something we should be aware of.
volumes, resp, err := d.storage.ListVolumes(ctx, &godo.ListVolumeParams{
Region: d.region,
ListOptions: opt,
})
if err != nil {
return nil, fmt.Errorf("failed to list volumes at page %d: %s", opt.Page, err)
}

numVolumes += len(volumes)
if account.VolumeLimit <= numVolumes {
return &limitDetails{
limit: account.VolumeLimit,
numVolumes: numVolumes,
}, nil
}

if resp.Links == nil || resp.Links.IsLastPage() {
break
}

page, err := resp.Links.CurrentPage()
if err != nil {
return nil, fmt.Errorf("failed to get current page: %s", err)
}
opt.Page = page + 1
if resp.Meta == nil {
// This should really never happen.
return nil, errors.New("no meta field available in list volumes response")
}
numVolumes := resp.Meta.Total
if account.VolumeLimit <= numVolumes {
return &limitDetails{
limit: account.VolumeLimit,
numVolumes: numVolumes,
}, nil
}

return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"errors"
"net/http"
"strings"
"strconv"
"strings"
"testing"
"time"

Expand Down
19 changes: 13 additions & 6 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (f *fakeStorageDriver) ListVolumes(ctx context.Context, param *godo.ListVol
delete(f.volumes, vol.ID)
}

return vols, godoResponse(), nil
return vols, godoResponseWithMeta(len(volumes)), nil
}

if param.Name != "" {
Expand All @@ -143,10 +143,10 @@ func (f *fakeStorageDriver) ListVolumes(ctx context.Context, param *godo.ListVol
}
}

return filtered, godoResponse(), nil
return filtered, godoResponseWithMeta(len(filtered)), nil
}

return volumes, godoResponse(), nil
return volumes, godoResponseWithMeta(len(volumes)), nil
}

func (f *fakeStorageDriver) GetVolume(ctx context.Context, id string) (*godo.Volume, *godo.Response, error) {
Expand Down Expand Up @@ -191,7 +191,7 @@ func (f *fakeStorageDriver) ListSnapshots(ctx context.Context, volumeID string,
}
}

return snapshots, godoResponse(), nil
return snapshots, godoResponseWithMeta(len(snapshots)), nil
}

func (f *fakeStorageDriver) GetSnapshot(ctx context.Context, id string) (*godo.Snapshot, *godo.Response, error) {
Expand Down Expand Up @@ -255,7 +255,7 @@ func (f *fakeStorageActionsDriver) Get(ctx context.Context, volumeID string, act
}

func (f *fakeStorageActionsDriver) List(ctx context.Context, volumeID string, opt *godo.ListOptions) ([]godo.Action, *godo.Response, error) {
return nil, godoResponse(), nil
return nil, godoResponseWithMeta(0), nil
}

func (f *fakeStorageActionsDriver) Resize(ctx context.Context, volumeID string, sizeGigabytes int, regionSlug string) (*godo.Action, *godo.Response, error) {
Expand Down Expand Up @@ -340,7 +340,7 @@ func (f *fakeSnapshotsDriver) ListVolume(ctx context.Context, opts *godo.ListOpt
snapshots = append(snapshots, *snap)
}

return snapshots, godoResponse(), nil
return snapshots, godoResponseWithMeta(len(snapshots)), nil
}

func (f *fakeSnapshotsDriver) ListDroplet(context.Context, *godo.ListOptions) ([]godo.Snapshot, *godo.Response, error) {
Expand Down Expand Up @@ -407,9 +407,16 @@ func (f *fakeMounter) GetStatistics(volumePath string) (volumeStatistics, error)
}

func godoResponse() *godo.Response {
return godoResponseWithMeta(0)
}

func godoResponseWithMeta(total int) *godo.Response {
return &godo.Response{
Response: &http.Response{StatusCode: 200},
Rate: godo.Rate{Limit: 10, Remaining: 10},
Meta: &godo.Meta{
Total: total,
},
}
}

Expand Down

0 comments on commit 5522c0f

Please sign in to comment.