blob: 421e4ca5f7a6c2ca581b44ef8f7e3cfd9d215895 [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 {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050012 Status string `mapstructure:"status"` // current status of the Volume
13 Name string `mapstructure:"display_name"` // display name
14 Attachments []string `mapstructure:"attachments"` // instances onto which the Volume is attached
15 AvailabilityZone string `mapstructure:"availability_zone"` // logical group
16 Bootable string `mapstructure:"bootable"` // is the volume bootable
17 CreatedAt string `mapstructure:"created_at"` // date created
18 Description string `mapstructure:"display_discription"` // display description
19 VolumeType string `mapstructure:"volume_type"` // see VolumeType object for more information
20 SnapshotID string `mapstructure:"snapshot_id"` // ID of the Snapshot from which the Volume was created
21 SourceVolID string `mapstructure:"source_volid"` // ID of the Volume from which the Volume was created
22 Metadata map[string]string `mapstructure:"metadata"` // user-defined key-value pairs
23 ID string `mapstructure:"id"` // unique identifier
24 Size int `mapstructure:"size"` // size of the Volume, in GB
Jon Perrittee6074f2014-04-30 18:42:32 -050025}
Jon Perrittd1d6a742014-09-17 01:10:59 -050026
Jon Perritt42b3a2a2014-10-02 23:06:07 -050027// CreateResult contains the response body and error from a Create request.
28type CreateResult struct {
29 commonResult
30}
31
32// GetResult contains the response body and error from a Get request.
33type GetResult struct {
34 commonResult
35}
36
37// ListResult is a pagination.pager that is returned from a call to the List function.
Jon Perritte03b35c2014-09-17 18:15:34 -050038type ListResult struct {
39 pagination.SinglePageBase
40}
41
Jon Perritt42b3a2a2014-10-02 23:06:07 -050042// IsEmpty returns true if a ListResult contains no Volumes.
Jon Perritte03b35c2014-09-17 18:15:34 -050043func (r ListResult) IsEmpty() (bool, error) {
44 volumes, err := ExtractVolumes(r)
Jon Perrittd1d6a742014-09-17 01:10:59 -050045 if err != nil {
46 return true, err
47 }
48 return len(volumes) == 0, nil
49}
50
Jon Perritt42b3a2a2014-10-02 23:06:07 -050051// ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
Jon Perritte03b35c2014-09-17 18:15:34 -050052func ExtractVolumes(page pagination.Page) ([]Volume, error) {
Jon Perrittd1d6a742014-09-17 01:10:59 -050053 var response struct {
54 Volumes []Volume `json:"volumes"`
55 }
56
Jon Perritte03b35c2014-09-17 18:15:34 -050057 err := mapstructure.Decode(page.(ListResult).Body, &response)
Jon Perrittd1d6a742014-09-17 01:10:59 -050058 return response.Volumes, err
59}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050060
Jon Perritt42b3a2a2014-10-02 23:06:07 -050061// UpdateResult contains the response body and error from an Update request.
62type UpdateResult struct {
63 commonResult
64}
65
Jon Perritt6d5561b2014-10-01 21:42:15 -050066type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040067 gophercloud.Result
Jon Perritt03cb46d2014-09-22 20:46:20 -050068}
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050069
Jon Perritt42b3a2a2014-10-02 23:06:07 -050070// Extract will get the Volume object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050071func (r commonResult) Extract() (*Volume, error) {
72 if r.Err != nil {
73 return nil, r.Err
Jon Perritt03cb46d2014-09-22 20:46:20 -050074 }
75
Jon Perritt6d5561b2014-10-01 21:42:15 -050076 var res struct {
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050077 Volume *Volume `json:"volume"`
78 }
79
Jon Perritt6d5561b2014-10-01 21:42:15 -050080 err := mapstructure.Decode(r.Resp, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +020081
82 return res.Volume, err
Jon Perritt9b2bf7d2014-09-18 18:47:51 -050083}