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" |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame^] | 6 | "github.com/rackspace/gophercloud/openstack/db/v1/datastores" |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
| 8 | ) |
| 9 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 10 | // Status represents the various states a Backup can be in. |
| 11 | type Status string |
| 12 | |
| 13 | // Enum types for the status. |
| 14 | const ( |
| 15 | StatusNew Status = "NEW" |
| 16 | StatusBuilding Status = "BUILDING" |
| 17 | StatusCompleted Status = "COMPLETED" |
| 18 | StatusFailed Status = "FAILED" |
| 19 | StatusDeleteFailed Status = "DELETE_FAILED" |
| 20 | ) |
| 21 | |
| 22 | // Backup represents a Backup API resource. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 23 | type Backup struct { |
| 24 | Description string |
| 25 | ID string |
| 26 | InstanceID string `json:"instance_id" mapstructure:"instance_id"` |
| 27 | LocationRef string |
| 28 | Name string |
| 29 | ParentID string `json:"parent_id" mapstructure:"parent_id"` |
| 30 | Size float64 |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 31 | Status Status |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 32 | Created string |
| 33 | Updated string |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 34 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 37 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 38 | type CreateResult struct { |
| 39 | commonResult |
| 40 | } |
| 41 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 42 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 43 | type GetResult struct { |
| 44 | commonResult |
| 45 | } |
| 46 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 47 | // DeleteResult represents the result of a delete operation. |
| 48 | type DeleteResult struct { |
| 49 | gophercloud.ErrResult |
| 50 | } |
| 51 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 52 | type commonResult struct { |
| 53 | gophercloud.Result |
| 54 | } |
| 55 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 56 | // Extract will retrieve a Backup struct from an operation's result. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 57 | func (r commonResult) Extract() (*Backup, error) { |
| 58 | if r.Err != nil { |
| 59 | return nil, r.Err |
| 60 | } |
| 61 | |
| 62 | var response struct { |
| 63 | Backup Backup `mapstructure:"backup"` |
| 64 | } |
| 65 | |
| 66 | err := mapstructure.Decode(r.Body, &response) |
| 67 | return &response.Backup, err |
| 68 | } |
| 69 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 70 | // BackupPage represents a page of backups. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 71 | type BackupPage struct { |
| 72 | pagination.SinglePageBase |
| 73 | } |
| 74 | |
| 75 | // IsEmpty checks whether an BackupPage struct is empty. |
| 76 | func (r BackupPage) IsEmpty() (bool, error) { |
| 77 | is, err := ExtractBackups(r) |
| 78 | if err != nil { |
| 79 | return true, err |
| 80 | } |
| 81 | return len(is) == 0, nil |
| 82 | } |
| 83 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 84 | // ExtractBackups will retrieve a slice of Backup structs from a paginated collection. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 85 | func ExtractBackups(page pagination.Page) ([]Backup, error) { |
| 86 | casted := page.(BackupPage).Body |
| 87 | |
| 88 | var resp struct { |
| 89 | Backups []Backup `mapstructure:"backups" json:"backups"` |
| 90 | } |
| 91 | |
| 92 | err := mapstructure.Decode(casted, &resp) |
| 93 | return resp.Backups, err |
| 94 | } |