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