blob: 04faf3227d03f9b49b87eba16fb4b6a430adbeb8 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
Jamie Hannaford87704ba2016-01-14 11:49:56 +01004 "fmt"
5 "reflect"
Jamie Hannaforde65ad952015-11-16 14:05:11 +01006 "time"
7
Jamie Hannaford302c0b62015-02-16 14:12:34 +01008 "github.com/mitchellh/mapstructure"
9 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +020010 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford302c0b62015-02-16 14:12:34 +010011 "github.com/rackspace/gophercloud/pagination"
12)
13
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010014// Status represents the various states a Backup can be in.
15type Status string
16
17// Enum types for the status.
18const (
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 Hannaford302c0b62015-02-16 14:12:34 +010027type 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 Hannafordb0d267b2015-02-19 11:59:53 +010035 Status Status
Jamie Hannaforde65ad952015-11-16 14:05:11 +010036 Created time.Time `mapstructure:"-"`
37 Updated time.Time `mapstructure:"-"`
Jamie Hannaforda50d1352015-02-18 11:38:38 +010038 Datastore datastores.DatastorePartial
Jamie Hannaford302c0b62015-02-16 14:12:34 +010039}
40
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010041// CreateResult represents the result of a create operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010042type CreateResult struct {
43 commonResult
44}
45
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010046// GetResult represents the result of a get operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010047type GetResult struct {
48 commonResult
49}
50
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010051// DeleteResult represents the result of a delete operation.
52type DeleteResult struct {
53 gophercloud.ErrResult
54}
55
Jamie Hannaford302c0b62015-02-16 14:12:34 +010056type commonResult struct {
57 gophercloud.Result
58}
59
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010060// Extract will retrieve a Backup struct from an operation's result.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010061func (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 Hannaforde65ad952015-11-16 14:05:11 +010071 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 Hannaford302c0b62015-02-16 14:12:34 +010089 return &response.Backup, err
90}
91
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010092// BackupPage represents a page of backups.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010093type BackupPage struct {
94 pagination.SinglePageBase
95}
96
97// IsEmpty checks whether an BackupPage struct is empty.
98func (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 Hannafordb0d267b2015-02-19 11:59:53 +0100106// ExtractBackups will retrieve a slice of Backup structs from a paginated collection.
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100107func 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 Hannaford87704ba2016-01-14 11:49:56 +0100114 if err := mapstructure.Decode(casted, &resp); err != nil {
115 return nil, err
116 }
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100117
118 var vals []interface{}
Jamie Hannaford87704ba2016-01-14 11:49:56 +0100119 switch casted.(type) {
120 case map[string]interface{}:
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100121 vals = casted.(map[string]interface{})["backups"].([]interface{})
Jamie Hannaford87704ba2016-01-14 11:49:56 +0100122 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 Hannaforde65ad952015-11-16 14:05:11 +0100126 }
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 Hannaford87704ba2016-01-14 11:49:56 +0100148 return resp.Backups, nil
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100149}