blob: cec41e026b1b8785b81421693a5aaab1222c1cb1 [file] [log] [blame]
Jamie Hannaforddfc1be72014-10-20 18:58:20 +02001package volumes
2
3import (
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 Hannaford0b7d0402014-10-21 10:24:35 +020011type Volume os.Volume
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020012
13type commonResult struct {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020014 gophercloud.Result
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020015}
16
17// CreateResult represents the result of a create operation
18type CreateResult struct {
19 os.CreateResult
20}
21
22// GetResult represents the result of a get operation
23type GetResult struct {
24 os.GetResult
25}
26
27// UpdateResult represents the result of an update operation
28type UpdateResult struct {
29 os.UpdateResult
30}
31
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020032func commonExtract(resp interface{}, err error) (*Volume, error) {
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020033 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.
47func (r GetResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020048 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020049}
50
51// Extract will get the Volume object out of the CreateResult object.
52func (r CreateResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020053 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020054}
55
56// Extract will get the Volume object out of the UpdateResult object.
57func (r UpdateResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020058 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020059}
60
61// ExtractSnapshots extracts and returns Volumes. It is used while iterating over a volumes.List call.
62func 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}