Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 1 | package snapshots |
| 2 | |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 8 | // Snapshot contains all the information associated with an OpenStack Snapshot. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 9 | type Snapshot struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 10 | // Currect status of the Snapshot. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 11 | Status string `json:"status"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 12 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 13 | // Display name. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 14 | Name string `json:"display_name"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 15 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 16 | // Instances onto which the Snapshot is attached. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 17 | Attachments []string `json:"attachments"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 18 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 19 | // Logical group. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 20 | AvailabilityZone string `json:"availability_zone"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 21 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 22 | // Is the Snapshot bootable? |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 23 | Bootable string `json:"bootable"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 24 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 25 | // Date created. |
Joe Topjian | 614b51d | 2016-08-03 12:19:30 -0600 | [diff] [blame] | 26 | CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 27 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 28 | // Display description. |
Joe Topjian | 614b51d | 2016-08-03 12:19:30 -0600 | [diff] [blame] | 29 | Description string `json:"display_description"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 30 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 31 | // See VolumeType object for more information. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 32 | VolumeType string `json:"volume_type"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 33 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 34 | // ID of the Snapshot from which this Snapshot was created. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 35 | SnapshotID string `json:"snapshot_id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 36 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 37 | // ID of the Volume from which this Snapshot was created. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 38 | VolumeID string `json:"volume_id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 39 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 40 | // User-defined key-value pairs. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 41 | Metadata map[string]string `json:"metadata"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 42 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 43 | // Unique identifier. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 44 | ID string `json:"id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 45 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 46 | // Size of the Snapshot, in GB. |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 47 | Size int `json:"size"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 48 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 49 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 50 | // CreateResult contains the response body and error from a Create request. |
| 51 | type CreateResult struct { |
| 52 | commonResult |
| 53 | } |
| 54 | |
| 55 | // GetResult contains the response body and error from a Get request. |
| 56 | type GetResult struct { |
| 57 | commonResult |
| 58 | } |
| 59 | |
Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 60 | // DeleteResult contains the response body and error from a Delete request. |
| 61 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 62 | gophercloud.ErrResult |
Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 65 | // SnapshotPage is a pagination.Pager that is returned from a call to the List function. |
| 66 | type SnapshotPage struct { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 67 | pagination.SinglePageBase |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 70 | // IsEmpty returns true if a SnapshotPage contains no Snapshots. |
| 71 | func (r SnapshotPage) IsEmpty() (bool, error) { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 72 | volumes, err := ExtractSnapshots(r) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 73 | return len(volumes) == 0, err |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 76 | // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 77 | func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 78 | var s struct { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 79 | Snapshots []Snapshot `json:"snapshots"` |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 80 | } |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 81 | err := (r.(SnapshotPage)).ExtractInto(&s) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 82 | return s.Snapshots, err |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 85 | // UpdateMetadataResult contains the response body and error from an UpdateMetadata request. |
| 86 | type UpdateMetadataResult struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 87 | commonResult |
| 88 | } |
| 89 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 90 | // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata. |
| 91 | func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) { |
| 92 | if r.Err != nil { |
| 93 | return nil, r.Err |
| 94 | } |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 95 | m := r.Body.(map[string]interface{})["metadata"] |
| 96 | return m.(map[string]interface{}), nil |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 99 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 100 | gophercloud.Result |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 103 | // Extract will get the Snapshot object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 104 | func (r commonResult) Extract() (*Snapshot, error) { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 105 | var s struct { |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 106 | Snapshot *Snapshot `json:"snapshot"` |
| 107 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 108 | err := r.ExtractInto(&s) |
| 109 | return s.Snapshot, err |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 110 | } |