Skip to content

Commit

Permalink
Deserialize meta for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo Reimann committed Dec 10, 2019
1 parent 4c7f4b7 commit b64ee97
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
3 changes: 3 additions & 0 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Response struct {
// request body and not the header.
Links *Links

// Meta describes generic information about the response.
Meta *Meta

// Monitoring URI
Monitor string

Expand Down
6 changes: 6 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package godo

// Meta describes generic information about the response.
type Meta struct {
Total int `json:"total"`
}
1 change: 1 addition & 0 deletions snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type snapshotRoot struct {
type snapshotsRoot struct {
Snapshots []Snapshot `json:"snapshots"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
}

type listSnapshotOptions struct {
Expand Down
7 changes: 7 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (f Volume) URN() string {
type storageVolumesRoot struct {
Volumes []Volume `json:"volumes"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

type storageVolumeRoot struct {
Expand Down Expand Up @@ -122,6 +123,9 @@ func (svc *StorageServiceOp) ListVolumes(ctx context.Context, params *ListVolume
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Volumes, resp, nil
}
Expand Down Expand Up @@ -203,6 +207,9 @@ func (svc *StorageServiceOp) ListSnapshots(ctx context.Context, volumeID string,
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Snapshots, resp, nil
}
Expand Down
75 changes: 55 additions & 20 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func TestStorageVolumes_ListStorageVolumes(t *testing.T) {
fmt.Fprint(w, jBlob)
})

volumes, _, err := client.Storage.ListVolumes(ctx, nil)
volumes, resp, err := client.Storage.ListVolumes(ctx, nil)
if err != nil {
t.Errorf("Storage.ListVolumes returned error: %v", err)
}

expected := []Volume{
expectedVolume := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Expand All @@ -86,8 +86,15 @@ func TestStorageVolumes_ListStorageVolumes(t *testing.T) {
Tags: []string{},
},
}
if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumes returned %+v, expected %+v", volumes, expected)
if !reflect.DeepEqual(volumes, expectedVolume) {
t.Errorf("Storage.ListVolumes returned volumes %+v, expected %+v", volumes, expectedVolume)
}

expectedMeta := &Meta{
Total: 28,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumes returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down Expand Up @@ -169,7 +176,7 @@ func TestStorageVolumes_ListVolumesByName(t *testing.T) {
}
}`

expected := []Volume{
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Expand All @@ -193,13 +200,20 @@ func TestStorageVolumes_ListVolumesByName(t *testing.T) {
options := &ListVolumeParams{
Name: "myvolume",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}

expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down Expand Up @@ -229,7 +243,7 @@ func TestStorageVolumes_ListVolumesByRegion(t *testing.T) {
}
}`

expected := []Volume{
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Expand All @@ -253,13 +267,20 @@ func TestStorageVolumes_ListVolumesByRegion(t *testing.T) {
options := &ListVolumeParams{
Region: "nyc3",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}

expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down Expand Up @@ -289,7 +310,7 @@ func TestStorageVolumes_ListVolumesByNameAndRegion(t *testing.T) {
}
}`

expected := []Volume{
expectedVolumes := []Volume{
{
Region: &Region{Slug: "nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Expand All @@ -314,13 +335,20 @@ func TestStorageVolumes_ListVolumesByNameAndRegion(t *testing.T) {
Region: "nyc3",
Name: "myvolume",
}
volumes, _, err := client.Storage.ListVolumes(ctx, options)
volumes, resp, err := client.Storage.ListVolumes(ctx, options)
if err != nil {
t.Errorf("Storage.ListVolumeByName returned error: %v", err)
}

if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListVolumeByName returned %+v, expected %+v", volumes, expected)
if !reflect.DeepEqual(volumes, expectedVolumes) {
t.Errorf("Storage.ListVolumeByName returned volumes %+v, expected %+v", volumes, expectedVolumes)
}

expectedMeta := &Meta{
Total: 1,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListVolumeByName returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down Expand Up @@ -554,12 +582,12 @@ func TestStorageSnapshots_ListStorageSnapshots(t *testing.T) {
fmt.Fprint(w, jBlob)
})

volumes, _, err := client.Storage.ListSnapshots(ctx, "98d414c6-295e-4e3a-ac58-eb9456c1e1d1", nil)
volumes, resp, err := client.Storage.ListSnapshots(ctx, "98d414c6-295e-4e3a-ac58-eb9456c1e1d1", nil)
if err != nil {
t.Errorf("Storage.ListSnapshots returned error: %v", err)
}

expected := []Snapshot{
expectedSnapshots := []Snapshot{
{
Regions: []string{"nyc3"},
ID: "80d414c6-295e-4e3a-ac58-eb9456c1e1d1",
Expand All @@ -575,8 +603,15 @@ func TestStorageSnapshots_ListStorageSnapshots(t *testing.T) {
Created: "2012-10-03T15:00:01.05Z",
},
}
if !reflect.DeepEqual(volumes, expected) {
t.Errorf("Storage.ListSnapshots returned %+v, expected %+v", volumes, expected)
if !reflect.DeepEqual(volumes, expectedSnapshots) {
t.Errorf("Storage.ListSnapshots returned snapshots %+v, expected %+v", volumes, expectedSnapshots)
}

expectedMeta := &Meta{
Total: 28,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Storage.ListSnapshots returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down

0 comments on commit b64ee97

Please sign in to comment.