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