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