blob: 7bf83a569fa2e7439c3255d0f3daf20863dac535 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
Jon Perrittd1d6a742014-09-17 01:10:59 -05003import (
Jon Perritt9b2bf7d2014-09-18 18:47:51 -05004 "fmt"
5
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud"
Jon Perrittd1d6a742014-09-17 01:10:59 -05007 "github.com/rackspace/gophercloud/pagination"
8
9 "github.com/mitchellh/mapstructure"
10)
11
Jon Perritte77b9b22014-05-01 13:11:12 -050012type Volume struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050013 Status string `mapstructure:"status"`
14 Name string `mapstructure:"display_name"`
15 Attachments []string `mapstructure:"attachments"`
16 AvailabilityZone string `mapstructure:"availability_zone"`
17 Bootable string `mapstructure:"bootable"`
18 CreatedAt string `mapstructure:"created_at"`
19 Description string `mapstructure:"display_discription"`
20 VolumeType string `mapstructure:"volume_type"`
21 SnapshotID string `mapstructure:"snapshot_id"`
22 SourceVolID string `mapstructure:"source_volid"`
23 Metadata map[string]string `mapstructure:"metadata"`
24 ID string `mapstructure:"id"`
25 Size int `mapstructure:"size"`
Jon Perrittee6074f2014-04-30 18:42:32 -050026}
Jon Perrittd1d6a742014-09-17 01:10:59 -050027
Jon Perritte03b35c2014-09-17 18:15:34 -050028// ListResult is a *http.Response that is returned from a call to the List function.
29type ListResult struct {
30 pagination.SinglePageBase
31}
32
33// IsEmpty returns true if a ListResult contains no container names.
34func (r ListResult) IsEmpty() (bool, error) {
35 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050036 if err != nil {
37 return true, err
38 }
39 return len(volumes) == 0, nil
40}
41
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050042// ExtractVolumes extracts and returns the Volumes from a 'List' request.
Jon Perritte03b35c2014-09-17 18:15:34 -050043func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050044 var response struct {
45 Volumes []Volume `json:"volumes"`
46 }
47
Jon Perritte03b35c2014-09-17 18:15:34 -050048 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050049 return response.Volumes, err
50}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050051
Jon Perritt6d5561b2014-10-01 21:42:15 -050052type commonResult struct {
53 gophercloud.CommonResult
Jon Perritt03cb46d2014-09-22 20:46:20 -050054}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050055
56// ExtractVolume extracts and returns the Volume from a 'Get' request.
Jon Perritt6d5561b2014-10-01 21:42:15 -050057func (r commonResult) Extract() (*Volume, error) {
58 if r.Err != nil {
59 return nil, r.Err
Jon Perritt03cb46d2014-09-22 20:46:20 -050060 }
61
Jon Perritt6d5561b2014-10-01 21:42:15 -050062 var res struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050063 Volume *Volume `json:"volume"`
64 }
65
Jon Perritt6d5561b2014-10-01 21:42:15 -050066 err := mapstructure.Decode(r.Resp, &res)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050067 if err != nil {
Jon Perritt6d5561b2014-10-01 21:42:15 -050068 return nil, fmt.Errorf("volumes: Error decoding volumes.commonResult: %v", err)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050069 }
Jon Perritt6d5561b2014-10-01 21:42:15 -050070 return res.Volume, nil
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050071}
Jon Perritt6d5561b2014-10-01 21:42:15 -050072
73type GetResult struct {
74 commonResult
75}
76
77type CreateResult struct {
78 commonResult
79}
80type UpdateResult struct {
81 commonResult
82}