blob: c7c2cc498412db2969a96e9ceea70733a10f09e0 [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 Hannaford0b7d0402014-10-21 10:24:35 +020028func commonExtract(resp interface{}, err error) (*Volume, error) {
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020029 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.
43func (r GetResult) Extract() (*Volume, error) {
Jamie Hannaford0b7d0402014-10-21 10:24:35 +020044 return commonExtract(r.Body, r.Err)
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020045}
46
47// Extract will get the Volume object out of the CreateResult object.
48func (r CreateResult) 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 UpdateResult object.
53func (r UpdateResult) 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
Jamie Hannaford878cce02014-10-23 16:58:43 +020057// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jamie Hannaforddfc1be72014-10-20 18:58:20 +020058func 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}