Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 1 | package backups |
| 2 | |
| 3 | import ( |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 4 | "time" |
| 5 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 52dbcee | 2015-10-06 16:09:56 +0200 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/openstack/db/v1/datastores" |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 12 | // Status represents the various states a Backup can be in. |
| 13 | type Status string |
| 14 | |
| 15 | // Enum types for the status. |
| 16 | const ( |
| 17 | StatusNew Status = "NEW" |
| 18 | StatusBuilding Status = "BUILDING" |
| 19 | StatusCompleted Status = "COMPLETED" |
| 20 | StatusFailed Status = "FAILED" |
| 21 | StatusDeleteFailed Status = "DELETE_FAILED" |
| 22 | ) |
| 23 | |
| 24 | // Backup represents a Backup API resource. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 25 | type Backup struct { |
| 26 | Description string |
| 27 | ID string |
| 28 | InstanceID string `json:"instance_id" mapstructure:"instance_id"` |
| 29 | LocationRef string |
| 30 | Name string |
| 31 | ParentID string `json:"parent_id" mapstructure:"parent_id"` |
| 32 | Size float64 |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 33 | Status Status |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 34 | Created time.Time `mapstructure:"-"` |
| 35 | Updated time.Time `mapstructure:"-"` |
Jamie Hannaford | a50d135 | 2015-02-18 11:38:38 +0100 | [diff] [blame] | 36 | Datastore datastores.DatastorePartial |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 39 | // CreateResult represents the result of a create operation. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 40 | type CreateResult struct { |
| 41 | commonResult |
| 42 | } |
| 43 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 44 | // GetResult represents the result of a get operation. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 45 | type GetResult struct { |
| 46 | commonResult |
| 47 | } |
| 48 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 49 | // DeleteResult represents the result of a delete operation. |
| 50 | type DeleteResult struct { |
| 51 | gophercloud.ErrResult |
| 52 | } |
| 53 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 54 | type commonResult struct { |
| 55 | gophercloud.Result |
| 56 | } |
| 57 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 58 | // Extract will retrieve a Backup struct from an operation's result. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 59 | func (r commonResult) Extract() (*Backup, error) { |
| 60 | if r.Err != nil { |
| 61 | return nil, r.Err |
| 62 | } |
| 63 | |
| 64 | var response struct { |
| 65 | Backup Backup `mapstructure:"backup"` |
| 66 | } |
| 67 | |
| 68 | err := mapstructure.Decode(r.Body, &response) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 69 | val := r.Body.(map[string]interface{})["backup"].(map[string]interface{}) |
| 70 | |
| 71 | if t, ok := val["created"].(string); ok && t != "" { |
| 72 | creationTime, err := time.Parse(time.RFC3339, t) |
| 73 | if err != nil { |
| 74 | return &response.Backup, err |
| 75 | } |
| 76 | response.Backup.Created = creationTime |
| 77 | } |
| 78 | |
| 79 | if t, ok := val["updated"].(string); ok && t != "" { |
| 80 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 81 | if err != nil { |
| 82 | return &response.Backup, err |
| 83 | } |
| 84 | response.Backup.Updated = updatedTime |
| 85 | } |
| 86 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 87 | return &response.Backup, err |
| 88 | } |
| 89 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 90 | // BackupPage represents a page of backups. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 91 | type BackupPage struct { |
| 92 | pagination.SinglePageBase |
| 93 | } |
| 94 | |
| 95 | // IsEmpty checks whether an BackupPage struct is empty. |
| 96 | func (r BackupPage) IsEmpty() (bool, error) { |
| 97 | is, err := ExtractBackups(r) |
| 98 | if err != nil { |
| 99 | return true, err |
| 100 | } |
| 101 | return len(is) == 0, nil |
| 102 | } |
| 103 | |
Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 104 | // ExtractBackups will retrieve a slice of Backup structs from a paginated collection. |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 105 | func ExtractBackups(page pagination.Page) ([]Backup, error) { |
| 106 | casted := page.(BackupPage).Body |
| 107 | |
| 108 | var resp struct { |
| 109 | Backups []Backup `mapstructure:"backups" json:"backups"` |
| 110 | } |
| 111 | |
| 112 | err := mapstructure.Decode(casted, &resp) |
Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 113 | |
| 114 | var vals []interface{} |
| 115 | switch (casted).(type) { |
| 116 | case interface{}: |
| 117 | vals = casted.(map[string]interface{})["backups"].([]interface{}) |
| 118 | } |
| 119 | |
| 120 | for i, v := range vals { |
| 121 | val := v.(map[string]interface{}) |
| 122 | |
| 123 | if t, ok := val["created"].(string); ok && t != "" { |
| 124 | creationTime, err := time.Parse(time.RFC3339, t) |
| 125 | if err != nil { |
| 126 | return resp.Backups, err |
| 127 | } |
| 128 | resp.Backups[i].Created = creationTime |
| 129 | } |
| 130 | |
| 131 | if t, ok := val["updated"].(string); ok && t != "" { |
| 132 | updatedTime, err := time.Parse(time.RFC3339, t) |
| 133 | if err != nil { |
| 134 | return resp.Backups, err |
| 135 | } |
| 136 | resp.Backups[i].Updated = updatedTime |
| 137 | } |
| 138 | } |
| 139 | |
Jamie Hannaford | 302c0b6 | 2015-02-16 14:12:34 +0100 | [diff] [blame] | 140 | return resp.Backups, err |
| 141 | } |