blob: 78c863f42c4fcfc7ef54b575d25bc48b0754bfc3 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
Jon Perrittd1d6a742014-09-17 01:10:59 -05003import (
Jon Perritt9b2bf7d2014-09-18 18:47:51 -05004 "fmt"
5
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud"
Jon Perrittd1d6a742014-09-17 01:10:59 -05007 "github.com/rackspace/gophercloud/pagination"
8
9 "github.com/mitchellh/mapstructure"
10)
11
Jon Perritt42b3a2a2014-10-02 23:06:07 -050012// Volume contains all the information associated with an OpenStack Volume.
Jon Perritte77b9b22014-05-01 13:11:12 -050013type Volume struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014 Status string `mapstructure:"status"` // current status of the Volume
15 Name string `mapstructure:"display_name"` // display name
16 Attachments []string `mapstructure:"attachments"` // instances onto which the Volume is attached
17 AvailabilityZone string `mapstructure:"availability_zone"` // logical group
18 Bootable string `mapstructure:"bootable"` // is the volume bootable
19 CreatedAt string `mapstructure:"created_at"` // date created
20 Description string `mapstructure:"display_discription"` // display description
21 VolumeType string `mapstructure:"volume_type"` // see VolumeType object for more information
22 SnapshotID string `mapstructure:"snapshot_id"` // ID of the Snapshot from which the Volume was created
23 SourceVolID string `mapstructure:"source_volid"` // ID of the Volume from which the Volume was created
24 Metadata map[string]string `mapstructure:"metadata"` // user-defined key-value pairs
25 ID string `mapstructure:"id"` // unique identifier
26 Size int `mapstructure:"size"` // size of the Volume, in GB
Jon Perrittee6074f2014-04-30 18:42:32 -050027}
Jon Perrittd1d6a742014-09-17 01:10:59 -050028
Jon Perritt42b3a2a2014-10-02 23:06:07 -050029// CreateResult contains the response body and error from a Create request.
30type CreateResult struct {
31 commonResult
32}
33
34// GetResult contains the response body and error from a Get request.
35type GetResult struct {
36 commonResult
37}
38
39// ListResult is a pagination.pager that is returned from a call to the List function.
Jon Perritte03b35c2014-09-17 18:15:34 -050040type ListResult struct {
41 pagination.SinglePageBase
42}
43
Jon Perritt42b3a2a2014-10-02 23:06:07 -050044// IsEmpty returns true if a ListResult contains no Volumes.
Jon Perritte03b35c2014-09-17 18:15:34 -050045func (r ListResult) IsEmpty() (bool, error) {
46 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050047 if err != nil {
48 return true, err
49 }
50 return len(volumes) == 0, nil
51}
52
Jon Perritt42b3a2a2014-10-02 23:06:07 -050053// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jon Perritte03b35c2014-09-17 18:15:34 -050054func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050055 var response struct {
56 Volumes []Volume `json:"volumes"`
57 }
58
Jon Perritte03b35c2014-09-17 18:15:34 -050059 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050060 return response.Volumes, err
61}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050062
Jon Perritt42b3a2a2014-10-02 23:06:07 -050063// UpdateResult contains the response body and error from an Update request.
64type UpdateResult struct {
65 commonResult
66}
67
Jon Perritt6d5561b2014-10-01 21:42:15 -050068type commonResult struct {
69 gophercloud.CommonResult
Jon Perritt03cb46d2014-09-22 20:46:20 -050070}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050071
Jon Perritt42b3a2a2014-10-02 23:06:07 -050072// Extract will get the Volume object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050073func (r commonResult) Extract() (*Volume, error) {
74 if r.Err != nil {
75 return nil, r.Err
Jon Perritt03cb46d2014-09-22 20:46:20 -050076 }
77
Jon Perritt6d5561b2014-10-01 21:42:15 -050078 var res struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050079 Volume *Volume `json:"volume"`
80 }
81
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 err := mapstructure.Decode(r.Resp, &res)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050083 if err != nil {
Jon Perritt6d5561b2014-10-01 21:42:15 -050084 return nil, fmt.Errorf("volumes: Error decoding volumes.commonResult: %v", err)
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050085 }
Jon Perritt6d5561b2014-10-01 21:42:15 -050086 return res.Volume, nil
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050087}