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 | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 6 | |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 7 | "github.com/mitchellh/mapstructure" |
| 8 | ) |
| 9 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 10 | // Snapshot contains all the information associated with an OpenStack Snapshot. |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 11 | type Snapshot struct { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 12 | // Currect status of the Snapshot. |
| 13 | Status string `mapstructure:"status"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 14 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 15 | // Display name. |
| 16 | Name string `mapstructure:"display_name"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 17 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 18 | // Instances onto which the Snapshot is attached. |
| 19 | Attachments []string `mapstructure:"attachments"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 20 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 21 | // Logical group. |
| 22 | AvailabilityZone string `mapstructure:"availability_zone"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 23 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 24 | // Is the Snapshot bootable? |
| 25 | Bootable string `mapstructure:"bootable"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 26 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 27 | // Date created. |
| 28 | CreatedAt string `mapstructure:"created_at"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 29 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 30 | // Display description. |
| 31 | Description string `mapstructure:"display_discription"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 32 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 33 | // See VolumeType object for more information. |
| 34 | VolumeType string `mapstructure:"volume_type"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 35 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 36 | // ID of the Snapshot from which this Snapshot was created. |
| 37 | SnapshotID string `mapstructure:"snapshot_id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 38 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 39 | // ID of the Volume from which this Snapshot was created. |
| 40 | VolumeID string `mapstructure:"volume_id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 41 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 42 | // User-defined key-value pairs. |
| 43 | Metadata map[string]string `mapstructure:"metadata"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 44 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 45 | // Unique identifier. |
| 46 | ID string `mapstructure:"id"` |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 47 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 48 | // Size of the Snapshot, in GB. |
| 49 | Size int `mapstructure:"size"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 50 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -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 | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 62 | // DeleteResult contains the response body and error from a Delete request. |
| 63 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 64 | gophercloud.ErrResult |
Jamie Hannaford | 3850959 | 2014-10-27 11:25:15 +0100 | [diff] [blame] | 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 | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 68 | type ListResult struct { |
| 69 | pagination.SinglePageBase |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 72 | // IsEmpty returns true if a ListResult contains no Snapshots. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 73 | func (r ListResult) IsEmpty() (bool, error) { |
| 74 | volumes, err := ExtractSnapshots(r) |
| 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 | // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) { |
| 83 | var response struct { |
| 84 | Snapshots []Snapshot `json:"snapshots"` |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 87 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
| 88 | return response.Snapshots, err |
| 89 | } |
| 90 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 91 | // UpdateMetadataResult contains the response body and error from an UpdateMetadata request. |
| 92 | type UpdateMetadataResult struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 93 | commonResult |
| 94 | } |
| 95 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 96 | // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata. |
| 97 | func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) { |
| 98 | if r.Err != nil { |
| 99 | return nil, r.Err |
| 100 | } |
| 101 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 102 | m := r.Body.(map[string]interface{})["metadata"] |
| 103 | return m.(map[string]interface{}), nil |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 106 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 107 | gophercloud.Result |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 110 | // Extract will get the Snapshot object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 111 | func (r commonResult) Extract() (*Snapshot, error) { |
| 112 | if r.Err != nil { |
| 113 | return nil, r.Err |
| 114 | } |
| 115 | |
| 116 | var res struct { |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 117 | Snapshot *Snapshot `json:"snapshot"` |
| 118 | } |
| 119 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 120 | err := mapstructure.Decode(r.Body, &res) |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 121 | |
| 122 | return res.Snapshot, err |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 123 | } |