Jon Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 1 | package volumes |
| 2 | |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 3 | import ( |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
| 8 | ) |
| 9 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 10 | // Volume contains all the information associated with an OpenStack Volume. |
Jon Perritt | e77b9b2 | 2014-05-01 13:11:12 -0500 | [diff] [blame] | 11 | type Volume struct { |
Jamie Hannaford | c35ae76 | 2014-10-20 18:42:53 +0200 | [diff] [blame] | 12 | // 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 Hannaford | 2e4d7d1 | 2014-10-23 14:47:24 +0200 | [diff] [blame] | 25 | Bootable string `mapstructure:"bootable"` |
Jamie Hannaford | c35ae76 | 2014-10-20 18:42:53 +0200 | [diff] [blame] | 26 | |
| 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 Perritt | ee6074f | 2014-04-30 18:42:32 -0500 | [diff] [blame] | 50 | } |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 51 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 52 | // CreateResult contains the response body and error from a Create request. |
| 53 | type CreateResult struct { |
| 54 | commonResult |
| 55 | } |
| 56 | |
| 57 | // GetResult contains the response body and error from a Get request. |
| 58 | type GetResult struct { |
| 59 | commonResult |
| 60 | } |
| 61 | |
Jamie Hannaford | ce9f908 | 2014-10-27 11:27:12 +0100 | [diff] [blame^] | 62 | // DeleteResult contains the response body and error from a Delete request. |
| 63 | type DeleteResult struct { |
| 64 | commonResult |
| 65 | } |
| 66 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 67 | // ListResult is a pagination.pager that is returned from a call to the List function. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 68 | type ListResult struct { |
| 69 | pagination.SinglePageBase |
| 70 | } |
| 71 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 72 | // IsEmpty returns true if a ListResult contains no Volumes. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 73 | func (r ListResult) IsEmpty() (bool, error) { |
| 74 | volumes, err := ExtractVolumes(r) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 75 | if err != nil { |
| 76 | return true, err |
| 77 | } |
| 78 | return len(volumes) == 0, nil |
| 79 | } |
| 80 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 81 | // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call. |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 82 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 83 | var response struct { |
| 84 | Volumes []Volume `json:"volumes"` |
| 85 | } |
| 86 | |
Jon Perritt | e03b35c | 2014-09-17 18:15:34 -0500 | [diff] [blame] | 87 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
Jon Perritt | d1d6a74 | 2014-09-17 01:10:59 -0500 | [diff] [blame] | 88 | return response.Volumes, err |
| 89 | } |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 90 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 91 | // UpdateResult contains the response body and error from an Update request. |
| 92 | type UpdateResult struct { |
| 93 | commonResult |
| 94 | } |
| 95 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 96 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 97 | gophercloud.Result |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 98 | } |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 99 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 100 | // Extract will get the Volume object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 101 | func (r commonResult) Extract() (*Volume, error) { |
| 102 | if r.Err != nil { |
| 103 | return nil, r.Err |
Jon Perritt | 03cb46d | 2014-09-22 20:46:20 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 106 | var res struct { |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 107 | Volume *Volume `json:"volume"` |
| 108 | } |
| 109 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 110 | err := mapstructure.Decode(r.Body, &res) |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 111 | |
| 112 | return res.Volume, err |
Jon Perritt | 9b2bf7d | 2014-09-18 18:47:51 -0500 | [diff] [blame] | 113 | } |