blob: 82b551dbba5d9c586228aa201a2cd49fe9366518 [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
Jamie Hannaforde65ad952015-11-16 14:05:11 +01004 "time"
5
Jamie Hannaford302c0b62015-02-16 14:12:34 +01006 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02008 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford302c0b62015-02-16 14:12:34 +01009 "github.com/rackspace/gophercloud/pagination"
10)
11
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010012// Status represents the various states a Backup can be in.
13type Status string
14
15// Enum types for the status.
16const (
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 Hannaford302c0b62015-02-16 14:12:34 +010025type 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 Hannafordb0d267b2015-02-19 11:59:53 +010033 Status Status
Jamie Hannaforde65ad952015-11-16 14:05:11 +010034 Created time.Time `mapstructure:"-"`
35 Updated time.Time `mapstructure:"-"`
Jamie Hannaforda50d1352015-02-18 11:38:38 +010036 Datastore datastores.DatastorePartial
Jamie Hannaford302c0b62015-02-16 14:12:34 +010037}
38
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010039// CreateResult represents the result of a create operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010040type CreateResult struct {
41 commonResult
42}
43
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010044// GetResult represents the result of a get operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010045type GetResult struct {
46 commonResult
47}
48
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010049// DeleteResult represents the result of a delete operation.
50type DeleteResult struct {
51 gophercloud.ErrResult
52}
53
Jamie Hannaford302c0b62015-02-16 14:12:34 +010054type commonResult struct {
55 gophercloud.Result
56}
57
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010058// Extract will retrieve a Backup struct from an operation's result.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010059func (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 Hannaforde65ad952015-11-16 14:05:11 +010069 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 Hannaford302c0b62015-02-16 14:12:34 +010087 return &response.Backup, err
88}
89
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010090// BackupPage represents a page of backups.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010091type BackupPage struct {
92 pagination.SinglePageBase
93}
94
95// IsEmpty checks whether an BackupPage struct is empty.
96func (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 Hannafordb0d267b2015-02-19 11:59:53 +0100104// ExtractBackups will retrieve a slice of Backup structs from a paginated collection.
Jamie Hannaford302c0b62015-02-16 14:12:34 +0100105func 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 Hannaforde65ad952015-11-16 14:05:11 +0100113
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 Hannaford302c0b62015-02-16 14:12:34 +0100140 return resp.Backups, err
141}