Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 4 | os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
| 8 | ) |
| 9 | |
Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 10 | // Volume wraps an Openstack volume |
Jamie Hannaford | 0b7d040 | 2014-10-21 10:24:35 +0200 | [diff] [blame] | 11 | type Volume os.Volume |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 12 | |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 13 | // CreateResult represents the result of a create operation |
| 14 | type CreateResult struct { |
| 15 | os.CreateResult |
| 16 | } |
| 17 | |
| 18 | // GetResult represents the result of a get operation |
| 19 | type GetResult struct { |
| 20 | os.GetResult |
| 21 | } |
| 22 | |
| 23 | // UpdateResult represents the result of an update operation |
| 24 | type UpdateResult struct { |
| 25 | os.UpdateResult |
| 26 | } |
| 27 | |
Jamie Hannaford | 0b7d040 | 2014-10-21 10:24:35 +0200 | [diff] [blame] | 28 | func commonExtract(resp interface{}, err error) (*Volume, error) { |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | |
| 33 | var respStruct struct { |
| 34 | Volume *Volume `json:"volume"` |
| 35 | } |
| 36 | |
| 37 | err = mapstructure.Decode(resp, &respStruct) |
| 38 | |
| 39 | return respStruct.Volume, err |
| 40 | } |
| 41 | |
| 42 | // Extract will get the Volume object out of the GetResult object. |
| 43 | func (r GetResult) Extract() (*Volume, error) { |
Jamie Hannaford | 0b7d040 | 2014-10-21 10:24:35 +0200 | [diff] [blame] | 44 | return commonExtract(r.Body, r.Err) |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Extract will get the Volume object out of the CreateResult object. |
| 48 | func (r CreateResult) Extract() (*Volume, error) { |
Jamie Hannaford | 0b7d040 | 2014-10-21 10:24:35 +0200 | [diff] [blame] | 49 | return commonExtract(r.Body, r.Err) |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Extract will get the Volume object out of the UpdateResult object. |
| 53 | func (r UpdateResult) Extract() (*Volume, error) { |
Jamie Hannaford | 0b7d040 | 2014-10-21 10:24:35 +0200 | [diff] [blame] | 54 | return commonExtract(r.Body, r.Err) |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 57 | // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call. |
Jamie Hannaford | dfc1be7 | 2014-10-20 18:58:20 +0200 | [diff] [blame] | 58 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { |
| 59 | var response struct { |
| 60 | Volumes []Volume `json:"volumes"` |
| 61 | } |
| 62 | |
| 63 | err := mapstructure.Decode(page.(os.ListResult).Body, &response) |
| 64 | |
| 65 | return response.Volumes, err |
| 66 | } |