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 | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
| 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 | |
| 62 | // 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] | 63 | type ListResult struct { |
| 64 | pagination.SinglePageBase |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 67 | // IsEmpty returns true if a ListResult contains no Snapshots. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 68 | func (r ListResult) IsEmpty() (bool, error) { |
| 69 | volumes, err := ExtractSnapshots(r) |
| 70 | if err != nil { |
| 71 | return true, err |
| 72 | } |
| 73 | return len(volumes) == 0, nil |
| 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 | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 77 | func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) { |
| 78 | var response struct { |
| 79 | Snapshots []Snapshot `json:"snapshots"` |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 82 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
| 83 | return response.Snapshots, err |
| 84 | } |
| 85 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 86 | // UpdateMetadataResult contains the response body and error from an UpdateMetadata request. |
| 87 | type UpdateMetadataResult struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 88 | commonResult |
| 89 | } |
| 90 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 91 | // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata. |
| 92 | func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) { |
| 93 | if r.Err != nil { |
| 94 | return nil, r.Err |
| 95 | } |
| 96 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame^] | 97 | m := r.Body.(map[string]interface{})["metadata"] |
| 98 | return m.(map[string]interface{}), nil |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 101 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 102 | gophercloud.Result |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 105 | // Extract will get the Snapshot object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 106 | func (r commonResult) Extract() (*Snapshot, error) { |
| 107 | if r.Err != nil { |
| 108 | return nil, r.Err |
| 109 | } |
| 110 | |
| 111 | var res struct { |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 112 | Snapshot *Snapshot `json:"snapshot"` |
| 113 | } |
| 114 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame^] | 115 | err := mapstructure.Decode(r.Body, &res) |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 116 | |
| 117 | return res.Snapshot, err |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 118 | } |