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"` |
| 14 | // Display name. |
| 15 | Name string `mapstructure:"display_name"` |
| 16 | // Instances onto which the Snapshot is attached. |
| 17 | Attachments []string `mapstructure:"attachments"` |
| 18 | // Logical group. |
| 19 | AvailabilityZone string `mapstructure:"availability_zone"` |
| 20 | // Is the Snapshot bootable? |
| 21 | Bootable string `mapstructure:"bootable"` |
| 22 | // Date created. |
| 23 | CreatedAt string `mapstructure:"created_at"` |
| 24 | // Display description. |
| 25 | Description string `mapstructure:"display_discription"` |
| 26 | // See VolumeType object for more information. |
| 27 | VolumeType string `mapstructure:"volume_type"` |
| 28 | // ID of the Snapshot from which this Snapshot was created. |
| 29 | SnapshotID string `mapstructure:"snapshot_id"` |
| 30 | // ID of the Volume from which this Snapshot was created. |
| 31 | VolumeID string `mapstructure:"volume_id"` |
| 32 | // User-defined key-value pairs. |
| 33 | Metadata map[string]string `mapstructure:"metadata"` |
| 34 | // Unique identifier. |
| 35 | ID string `mapstructure:"id"` |
| 36 | // Size of the Snapshot, in GB. |
| 37 | Size int `mapstructure:"size"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 38 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 39 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 40 | // CreateResult contains the response body and error from a Create request. |
| 41 | type CreateResult struct { |
| 42 | commonResult |
| 43 | } |
| 44 | |
| 45 | // GetResult contains the response body and error from a Get request. |
| 46 | type GetResult struct { |
| 47 | commonResult |
| 48 | } |
| 49 | |
| 50 | // 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] | 51 | type ListResult struct { |
| 52 | pagination.SinglePageBase |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 55 | // IsEmpty returns true if a ListResult contains no Snapshots. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 56 | func (r ListResult) IsEmpty() (bool, error) { |
| 57 | volumes, err := ExtractSnapshots(r) |
| 58 | if err != nil { |
| 59 | return true, err |
| 60 | } |
| 61 | return len(volumes) == 0, nil |
| 62 | } |
| 63 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 64 | // 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] | 65 | func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) { |
| 66 | var response struct { |
| 67 | Snapshots []Snapshot `json:"snapshots"` |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 70 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
| 71 | return response.Snapshots, err |
| 72 | } |
| 73 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 74 | // UpdateMetadataResult contains the response body and error from an UpdateMetadata request. |
| 75 | type UpdateMetadataResult struct { |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 76 | commonResult |
| 77 | } |
| 78 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 79 | // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata. |
| 80 | func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) { |
| 81 | if r.Err != nil { |
| 82 | return nil, r.Err |
| 83 | } |
| 84 | |
| 85 | m := r.Resp["metadata"].(map[string]interface{}) |
| 86 | |
| 87 | return m, nil |
| 88 | } |
| 89 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 90 | type commonResult struct { |
| 91 | gophercloud.CommonResult |
| 92 | } |
| 93 | |
Jon Perritt | 42b3a2a | 2014-10-02 23:06:07 -0500 | [diff] [blame] | 94 | // Extract will get the Snapshot object out of the commonResult object. |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 95 | func (r commonResult) Extract() (*Snapshot, error) { |
| 96 | if r.Err != nil { |
| 97 | return nil, r.Err |
| 98 | } |
| 99 | |
| 100 | var res struct { |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 101 | Snapshot *Snapshot `json:"snapshot"` |
| 102 | } |
| 103 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 104 | err := mapstructure.Decode(r.Resp, &res) |
Jamie Hannaford | 6a83e80 | 2014-10-08 17:13:50 +0200 | [diff] [blame] | 105 | |
| 106 | return res.Snapshot, err |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 107 | } |