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