Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes |
| 2 | |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 3 | import ( |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 4 | "fmt" |
| 5 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
| 10 | ) |
| 11 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 12 | // Volume contains all the information associated with an OpenStack Volume. |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 13 | type Volume struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 14 | Status string `mapstructure:"status"` // current status of the Volume |
| 15 | Name string `mapstructure:"display_name"` // display name |
| 16 | Attachments []string `mapstructure:"attachments"` // instances onto which the Volume is attached |
| 17 | AvailabilityZone string `mapstructure:"availability_zone"` // logical group |
| 18 | Bootable string `mapstructure:"bootable"` // is the volume bootable |
| 19 | CreatedAt string `mapstructure:"created_at"` // date created |
| 20 | Description string `mapstructure:"display_discription"` // display description |
| 21 | VolumeType string `mapstructure:"volume_type"` // see VolumeType object for more information |
| 22 | SnapshotID string `mapstructure:"snapshot_id"` // ID of the Snapshot from which the Volume was created |
| 23 | SourceVolID string `mapstructure:"source_volid"` // ID of the Volume from which the Volume was created |
| 24 | Metadata map[string]string `mapstructure:"metadata"` // user-defined key-value pairs |
| 25 | ID string `mapstructure:"id"` // unique identifier |
| 26 | Size int `mapstructure:"size"` // size of the Volume, in GB |
Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 27 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 28 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 29 | // CreateResult contains the response body and error from a Create request. |
| 30 | type CreateResult struct { |
| 31 | commonResult |
| 32 | } |
| 33 | |
| 34 | // GetResult contains the response body and error from a Get request. |
| 35 | type GetResult struct { |
| 36 | commonResult |
| 37 | } |
| 38 | |
| 39 | // ListResult is a pagination.pager that is returned from a call to the List function. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 40 | type ListResult struct { |
| 41 | pagination.SinglePageBase |
| 42 | } |
| 43 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 44 | // IsEmpty returns true if a ListResult contains no Volumes. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 45 | func (r ListResult) IsEmpty() (bool, error) { |
| 46 | volumes, err := ExtractVolumes(r) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 47 | if err != nil { |
| 48 | return true, err |
| 49 | } |
| 50 | return len(volumes) == 0, nil |
| 51 | } |
| 52 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 53 | // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 54 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 55 | var response struct { |
| 56 | Volumes []Volume `json:"volumes"` |
| 57 | } |
| 58 | |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 59 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 60 | return response.Volumes, err |
| 61 | } |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 62 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 63 | // UpdateResult contains the response body and error from an Update request. |
| 64 | type UpdateResult struct { |
| 65 | commonResult |
| 66 | } |
| 67 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 68 | type commonResult struct { |
| 69 | gophercloud.CommonResult |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 70 | } |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 71 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 72 | // Extract will get the Volume object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 73 | func (r commonResult) Extract() (*Volume, error) { |
| 74 | if r.Err != nil { |
| 75 | return nil, r.Err |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 78 | var res struct { |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 79 | Volume *Volume `json:"volume"` |
| 80 | } |
| 81 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | err := mapstructure.Decode(r.Resp, &res) |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 83 | if err != nil { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 84 | return nil, fmt.Errorf("volumes: Error decoding volumes.commonResult: %v", err) |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 85 | } |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 86 | return res.Volume, nil |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 87 | } |