feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 1 | package volumes |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
| 8 | ) |
| 9 | |
| 10 | // Volume contains all the information associated with an OpenStack Volume. |
| 11 | type Volume struct { |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 12 | // Instances onto which the volume is attached. |
| 13 | Attachments []map[string]interface{} `mapstructure:"attachments"` |
| 14 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 15 | // AvailabilityZone is which availability zone the volume is in. |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 16 | AvailabilityZone string `mapstructure:"availability_zone"` |
| 17 | |
| 18 | // Indicates whether this is a bootable volume. |
| 19 | Bootable string `mapstructure:"bootable"` |
| 20 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 21 | // ConsistencyGroupID is the consistency group ID. |
| 22 | ConsistencyGroupID string `mapstructure:"consistencygroup_id"` |
| 23 | |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 24 | // The date when this volume was created. |
| 25 | CreatedAt string `mapstructure:"created_at"` |
| 26 | |
| 27 | // Human-readable description for the volume. |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 28 | Description string `mapstructure:"description"` |
| 29 | |
| 30 | // Encrypted denotes if the volume is encrypted. |
| 31 | Encrypted bool `mapstructure:"encrypted"` |
| 32 | |
| 33 | // Human-readable display name for the volume. |
| 34 | Name string `mapstructure:"name"` |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 35 | |
| 36 | // The type of volume to create, either SATA or SSD. |
| 37 | VolumeType string `mapstructure:"volume_type"` |
| 38 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 39 | // ReplicationDriverData contains data about the replication driver. |
| 40 | ReplicationDriverData string `mapstructure:"os-volume-replication:driver_data"` |
| 41 | |
| 42 | // ReplicationExtendedStatus contains extended status about replication. |
| 43 | ReplicationExtendedStatus string `mapstructure:"os-volume-replication:extended_status"` |
| 44 | |
| 45 | // ReplicationStatus is the status of replication. |
| 46 | ReplicationStatus string `mapstructure:"replication_status"` |
| 47 | |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 48 | // The ID of the snapshot from which the volume was created |
| 49 | SnapshotID string `mapstructure:"snapshot_id"` |
| 50 | |
| 51 | // The ID of another block storage volume from which the current volume was created |
| 52 | SourceVolID string `mapstructure:"source_volid"` |
| 53 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 54 | // Current status of the volume. |
| 55 | Status string `mapstructure:"status"` |
| 56 | |
| 57 | // TenantID is the id of the project that owns the volume. |
| 58 | TenantID string `mapstructure:"os-vol-tenant-attr:tenant_id"` |
| 59 | |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 60 | // Arbitrary key-value pairs defined by the user. |
| 61 | Metadata map[string]string `mapstructure:"metadata"` |
| 62 | |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 63 | // Multiattach denotes if the volume is multi-attach capable. |
| 64 | Multiattach bool `mapstructure:"multiattach"` |
| 65 | |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 66 | // Unique identifier for the volume. |
| 67 | ID string `mapstructure:"id"` |
| 68 | |
| 69 | // Size of the volume in GB. |
| 70 | Size int `mapstructure:"size"` |
feisky | cf0c7fe | 2015-11-05 22:06:17 +0800 | [diff] [blame^] | 71 | |
| 72 | // UserID is the id of the user who created the volume. |
| 73 | UserID string `mapstructure:"user_id"` |
feisky | da54614 | 2015-09-17 12:28:23 +0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // CreateResult contains the response body and error from a Create request. |
| 77 | type CreateResult struct { |
| 78 | commonResult |
| 79 | } |
| 80 | |
| 81 | // GetResult contains the response body and error from a Get request. |
| 82 | type GetResult struct { |
| 83 | commonResult |
| 84 | } |
| 85 | |
| 86 | // DeleteResult contains the response body and error from a Delete request. |
| 87 | type DeleteResult struct { |
| 88 | gophercloud.ErrResult |
| 89 | } |
| 90 | |
| 91 | // ListResult is a pagination.pager that is returned from a call to the List function. |
| 92 | type ListResult struct { |
| 93 | pagination.SinglePageBase |
| 94 | } |
| 95 | |
| 96 | // IsEmpty returns true if a ListResult contains no Volumes. |
| 97 | func (r ListResult) IsEmpty() (bool, error) { |
| 98 | volumes, err := ExtractVolumes(r) |
| 99 | if err != nil { |
| 100 | return true, err |
| 101 | } |
| 102 | return len(volumes) == 0, nil |
| 103 | } |
| 104 | |
| 105 | // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call. |
| 106 | func ExtractVolumes(page pagination.Page) ([]Volume, error) { |
| 107 | var response struct { |
| 108 | Volumes []Volume `json:"volumes"` |
| 109 | } |
| 110 | |
| 111 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
| 112 | return response.Volumes, err |
| 113 | } |
| 114 | |
| 115 | // UpdateResult contains the response body and error from an Update request. |
| 116 | type UpdateResult struct { |
| 117 | commonResult |
| 118 | } |
| 119 | |
| 120 | type commonResult struct { |
| 121 | gophercloud.Result |
| 122 | } |
| 123 | |
| 124 | // Extract will get the Volume object out of the commonResult object. |
| 125 | func (r commonResult) Extract() (*Volume, error) { |
| 126 | if r.Err != nil { |
| 127 | return nil, r.Err |
| 128 | } |
| 129 | |
| 130 | var res struct { |
| 131 | Volume *Volume `json:"volume"` |
| 132 | } |
| 133 | |
| 134 | err := mapstructure.Decode(r.Body, &res) |
| 135 | |
| 136 | return res.Volume, err |
| 137 | } |