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