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 | |
| 11 | type Volume struct { |
| 12 | os.Volume |
| 13 | } |
| 14 | |
| 15 | type commonResult struct { |
| 16 | gophercloud.CommonResult |
| 17 | } |
| 18 | |
| 19 | // CreateResult represents the result of a create operation |
| 20 | type CreateResult struct { |
| 21 | os.CreateResult |
| 22 | } |
| 23 | |
| 24 | // GetResult represents the result of a get operation |
| 25 | type GetResult struct { |
| 26 | os.GetResult |
| 27 | } |
| 28 | |
| 29 | // UpdateResult represents the result of an update operation |
| 30 | type UpdateResult struct { |
| 31 | os.UpdateResult |
| 32 | } |
| 33 | |
| 34 | func commonExtract(resp map[string]interface{}, err error) (*Volume, error) { |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | var respStruct struct { |
| 40 | Volume *Volume `json:"volume"` |
| 41 | } |
| 42 | |
| 43 | err = mapstructure.Decode(resp, &respStruct) |
| 44 | |
| 45 | return respStruct.Volume, err |
| 46 | } |
| 47 | |
| 48 | // Extract will get the Volume object out of the GetResult object. |
| 49 | func (r GetResult) Extract() (*Volume, error) { |
| 50 | return commonExtract(r.Resp, r.Err) |
| 51 | } |
| 52 | |
| 53 | // Extract will get the Volume object out of the CreateResult object. |
| 54 | func (r CreateResult) Extract() (*Volume, error) { |
| 55 | return commonExtract(r.Resp, r.Err) |
| 56 | } |
| 57 | |
| 58 | // Extract will get the Volume object out of the UpdateResult object. |
| 59 | func (r UpdateResult) Extract() (*Volume, error) { |
| 60 | return commonExtract(r.Resp, r.Err) |
| 61 | } |
| 62 | |
| 63 | // ExtractSnapshots extracts and returns Volumes. It is used while iterating over a volumes.List call. |
| 64 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { |
| 65 | var response struct { |
| 66 | Volumes []Volume `json:"volumes"` |
| 67 | } |
| 68 | |
| 69 | err := mapstructure.Decode(page.(os.ListResult).Body, &response) |
| 70 | |
| 71 | return response.Volumes, err |
| 72 | } |