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