Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame^] | 1 | package backups |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | type Backup struct { |
| 10 | Description string |
| 11 | ID string |
| 12 | InstanceID string `json:"instance_id" mapstructure:"instance_id"` |
| 13 | LocationRef string |
| 14 | Name string |
| 15 | ParentID string `json:"parent_id" mapstructure:"parent_id"` |
| 16 | Size float64 |
| 17 | Status string |
| 18 | Created string |
| 19 | Updated string |
| 20 | } |
| 21 | |
| 22 | type CreateResult struct { |
| 23 | commonResult |
| 24 | } |
| 25 | |
| 26 | type GetResult struct { |
| 27 | commonResult |
| 28 | } |
| 29 | |
| 30 | type commonResult struct { |
| 31 | gophercloud.Result |
| 32 | } |
| 33 | |
| 34 | func (r commonResult) Extract() (*Backup, error) { |
| 35 | if r.Err != nil { |
| 36 | return nil, r.Err |
| 37 | } |
| 38 | |
| 39 | var response struct { |
| 40 | Backup Backup `mapstructure:"backup"` |
| 41 | } |
| 42 | |
| 43 | err := mapstructure.Decode(r.Body, &response) |
| 44 | return &response.Backup, err |
| 45 | } |
| 46 | |
| 47 | type DeleteResult struct { |
| 48 | gophercloud.ErrResult |
| 49 | } |
| 50 | |
| 51 | type BackupPage struct { |
| 52 | pagination.SinglePageBase |
| 53 | } |
| 54 | |
| 55 | // IsEmpty checks whether an BackupPage struct is empty. |
| 56 | func (r BackupPage) IsEmpty() (bool, error) { |
| 57 | is, err := ExtractBackups(r) |
| 58 | if err != nil { |
| 59 | return true, err |
| 60 | } |
| 61 | return len(is) == 0, nil |
| 62 | } |
| 63 | |
| 64 | func ExtractBackups(page pagination.Page) ([]Backup, error) { |
| 65 | casted := page.(BackupPage).Body |
| 66 | |
| 67 | var resp struct { |
| 68 | Backups []Backup `mapstructure:"backups" json:"backups"` |
| 69 | } |
| 70 | |
| 71 | err := mapstructure.Decode(casted, &resp) |
| 72 | return resp.Backups, err |
| 73 | } |