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