blob: 2e94288ae17a4666c0568929ee73e16d47f1c750 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
Jon Perrittd1d6a742014-09-17 01:10:59 -05003import (
Jon Perritt6d5561b2014-10-01 21:42:15 -05004 "github.com/rackspace/gophercloud"
Jon Perrittd1d6a742014-09-17 01:10:59 -05005 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/mitchellh/mapstructure"
8)
9
Jon Perritt42b3a2a2014-10-02 23:06:07 -050010// Volume contains all the information associated with an OpenStack Volume.
Jon Perritte77b9b22014-05-01 13:11:12 -050011type Volume struct {
Jamie Hannafordc35ae762014-10-20 18:42:53 +020012 // Current status of the volume.
13 Status string `mapstructure:"status"`
14
15 // Human-readable display name for the volume.
16 Name string `mapstructure:"display_name"`
17
18 // Instances onto which the volume is attached.
19 Attachments []string `mapstructure:"attachments"`
20
21 // This parameter is no longer used.
22 AvailabilityZone string `mapstructure:"availability_zone"`
23
24 // Indicates whether this is a bootable volume.
Jamie Hannaford2e4d7d12014-10-23 14:47:24 +020025 Bootable string `mapstructure:"bootable"`
Jamie Hannafordc35ae762014-10-20 18:42:53 +020026
27 // The date when this volume was created.
28 CreatedAt string `mapstructure:"created_at"`
29
30 // Human-readable description for the volume.
31 Description string `mapstructure:"display_discription"`
32
33 // The type of volume to create, either SATA or SSD.
34 VolumeType string `mapstructure:"volume_type"`
35
36 // The ID of the snapshot from which the volume was created
37 SnapshotID string `mapstructure:"snapshot_id"`
38
39 // The ID of another block storage volume from which the current volume was created
40 SourceVolID string `mapstructure:"source_volid"`
41
42 // Arbitrary key-value pairs defined by the user.
43 Metadata map[string]string `mapstructure:"metadata"`
44
45 // Unique identifier for the volume.
46 ID string `mapstructure:"id"`
47
48 // Size of the volume in GB.
49 Size int `mapstructure:"size"`
Jon Perrittee6074f2014-04-30 18:42:32 -050050}
Jon Perrittd1d6a742014-09-17 01:10:59 -050051
Jon Perritt42b3a2a2014-10-02 23:06:07 -050052// CreateResult contains the response body and error from a Create request.
53type CreateResult struct {
54 commonResult
55}
56
57// GetResult contains the response body and error from a Get request.
58type GetResult struct {
59 commonResult
60}
61
Jamie Hannafordce9f9082014-10-27 11:27:12 +010062// DeleteResult contains the response body and error from a Delete request.
63type DeleteResult struct {
64 commonResult
65}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// ListResult is a pagination.pager that is returned from a call to the List function.
Jon Perritte03b35c2014-09-17 18:15:34 -050068type ListResult struct {
69 pagination.SinglePageBase
70}
71
Jon Perritt42b3a2a2014-10-02 23:06:07 -050072// IsEmpty returns true if a ListResult contains no Volumes.
Jon Perritte03b35c2014-09-17 18:15:34 -050073func (r ListResult) IsEmpty() (bool, error) {
74 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050075 if err != nil {
76 return true, err
77 }
78 return len(volumes) == 0, nil
79}
80
Jon Perritt42b3a2a2014-10-02 23:06:07 -050081// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jon Perritte03b35c2014-09-17 18:15:34 -050082func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050083 var response struct {
84 Volumes []Volume `json:"volumes"`
85 }
86
Jon Perritte03b35c2014-09-17 18:15:34 -050087 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050088 return response.Volumes, err
89}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050090
Jon Perritt42b3a2a2014-10-02 23:06:07 -050091// UpdateResult contains the response body and error from an Update request.
92type UpdateResult struct {
93 commonResult
94}
95
Jon Perritt6d5561b2014-10-01 21:42:15 -050096type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040097 gophercloud.Result
Jon Perritt03cb46d2014-09-22 20:46:20 -050098}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050099
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500100// Extract will get the Volume object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500101func (r commonResult) Extract() (*Volume, error) {
102 if r.Err != nil {
103 return nil, r.Err
Jon Perritt03cb46d2014-09-22 20:46:20 -0500104 }
105
Jon Perritt6d5561b2014-10-01 21:42:15 -0500106 var res struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -0500107 Volume *Volume `json:"volume"`
108 }
109
Ash Wilsond3dc2542014-10-20 10:10:48 -0400110 err := mapstructure.Decode(r.Body, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +0200111
112 return res.Volume, err
Jon Perritt9b2bf7d2014-09-18 18:47:51 -0500113}