blob: 9435dca72d5aa4d2f2e2b75eb1d3fe997437eaeb [file] [log] [blame]
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02001package volumes
2
3import (
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02004 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
5 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/mitchellh/mapstructure"
8)
9
Jamie Hannaford878cce02014-10-23 16:58:43 +020010// Volume wraps an Openstack volume
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020011type Volume os.Volume
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020012
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020013// CreateResult represents the result of a create operation
14type CreateResult struct {
15 os.CreateResult
16}
17
18// GetResult represents the result of a get operation
19type GetResult struct {
20 os.GetResult
21}
22
23// UpdateResult represents the result of an update operation
24type UpdateResult struct {
25 os.UpdateResult
26}
27
Jamie Hannafordf6398642014-10-27 11:40:15 +010028// DeleteResult represents the result of a delete operation
29type DeleteResult struct {
30 os.DeleteResult
31}
32
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020033func commonExtract(resp interface{}, err error) (*Volume, error) {
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020034 if err != nil {
35 return nil, err
36 }
37
38 var respStruct struct {
39 Volume *Volume `json:"volume"`
40 }
41
42 err = mapstructure.Decode(resp, &respStruct)
43
44 return respStruct.Volume, err
45}
46
47// Extract will get the Volume object out of the GetResult object.
48func (r GetResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020049 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020050}
51
52// Extract will get the Volume object out of the CreateResult object.
53func (r CreateResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020054 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020055}
56
57// Extract will get the Volume object out of the UpdateResult object.
58func (r UpdateResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020059 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020060}
61
Jamie Hannaford878cce02014-10-23 16:58:43 +020062// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020063func ExtractVolumes(page pagination.Page) ([]Volume, error) {
64 var response struct {
65 Volumes []Volume `json:"volumes"`
66 }
67
68 err := mapstructure.Decode(page.(os.ListResult).Body, &response)
69
70 return response.Volumes, err
71}