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