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