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