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 | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 12 | type Snapshot struct { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 13 | Status string `mapstructure:"status"` |
| 14 | Name string `mapstructure:"display_name"` |
| 15 | Attachments []string `mapstructure:"attachments"` |
| 16 | AvailabilityZone string `mapstructure:"availability_zone"` |
| 17 | Bootable string `mapstructure:"bootable"` |
| 18 | CreatedAt string `mapstructure:"created_at"` |
| 19 | Description string `mapstructure:"display_discription"` |
| 20 | VolumeType string `mapstructure:"volume_type"` |
| 21 | SnapshotID string `mapstructure:"snapshot_id"` |
| 22 | SourceVolID string `mapstructure:"source_volid"` |
| 23 | Metadata map[string]string `mapstructure:"metadata"` |
| 24 | ID string `mapstructure:"id"` |
| 25 | Size int `mapstructure:"size"` |
Jon Perritt | dfff997 | 2014-09-22 01:14:54 -0500 | [diff] [blame] | 26 | } |
Jon Perritt | 56d43b2 | 2014-09-22 20:47:11 -0500 | [diff] [blame] | 27 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 28 | // ListResult is a *http.Response that is returned from a call to the List function. |
| 29 | type ListResult struct { |
| 30 | pagination.SinglePageBase |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 31 | } |
| 32 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 33 | // IsEmpty returns true if a ListResult contains no container names. |
| 34 | func (r ListResult) IsEmpty() (bool, error) { |
| 35 | volumes, err := ExtractSnapshots(r) |
| 36 | if err != nil { |
| 37 | return true, err |
| 38 | } |
| 39 | return len(volumes) == 0, nil |
| 40 | } |
| 41 | |
| 42 | // ExtractSnapshots extracts and returns the Volumes from a 'List' request. |
| 43 | func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) { |
| 44 | var response struct { |
| 45 | Snapshots []Snapshot `json:"snapshots"` |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 48 | err := mapstructure.Decode(page.(ListResult).Body, &response) |
| 49 | return response.Snapshots, err |
| 50 | } |
| 51 | |
| 52 | type commonResult struct { |
| 53 | gophercloud.CommonResult |
| 54 | } |
| 55 | |
| 56 | // Extract returns a pointer to the Snapshot from a commonResult.Resp. |
| 57 | func (r commonResult) Extract() (*Snapshot, error) { |
| 58 | if r.Err != nil { |
| 59 | return nil, r.Err |
| 60 | } |
| 61 | |
| 62 | var res struct { |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 63 | Snapshot *Snapshot `json:"snapshot"` |
| 64 | } |
| 65 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 66 | err := mapstructure.Decode(r.Resp, &res) |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 67 | if err != nil { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 68 | return nil, fmt.Errorf("snapshots: Error decoding snapshots.commonResult: %v", err) |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 69 | } |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 70 | return res.Snapshot, nil |
Jon Perritt | d746863 | 2014-09-22 21:58:59 -0500 | [diff] [blame] | 71 | } |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 72 | |
| 73 | type GetResult struct { |
| 74 | commonResult |
| 75 | } |
| 76 | |
| 77 | type CreateResult struct { |
| 78 | commonResult |
| 79 | } |
| 80 | type UpdateResult struct { |
| 81 | commonResult |
| 82 | } |