blob: 402fc86050707a1af86b94283ddf2aafa94ba7ef [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
Jamie Hannafordc1c6bf82015-02-17 16:53:38 +01007 "github.com/rackspace/gophercloud/rackspace/db/v1/datastores"
Jamie Hannaford302c0b62015-02-16 14:12:34 +01008)
9
10type Backup struct {
11 Description string
12 ID string
13 InstanceID string `json:"instance_id" mapstructure:"instance_id"`
14 LocationRef string
15 Name string
16 ParentID string `json:"parent_id" mapstructure:"parent_id"`
17 Size float64
18 Status string
19 Created string
20 Updated string
Jamie Hannaforda50d1352015-02-18 11:38:38 +010021 Datastore datastores.DatastorePartial
Jamie Hannaford302c0b62015-02-16 14:12:34 +010022}
23
24type CreateResult struct {
25 commonResult
26}
27
28type GetResult struct {
29 commonResult
30}
31
32type commonResult struct {
33 gophercloud.Result
34}
35
36func (r commonResult) Extract() (*Backup, error) {
37 if r.Err != nil {
38 return nil, r.Err
39 }
40
41 var response struct {
42 Backup Backup `mapstructure:"backup"`
43 }
44
45 err := mapstructure.Decode(r.Body, &response)
46 return &response.Backup, err
47}
48
49type DeleteResult struct {
50 gophercloud.ErrResult
51}
52
53type BackupPage struct {
54 pagination.SinglePageBase
55}
56
57// IsEmpty checks whether an BackupPage struct is empty.
58func (r BackupPage) IsEmpty() (bool, error) {
59 is, err := ExtractBackups(r)
60 if err != nil {
61 return true, err
62 }
63 return len(is) == 0, nil
64}
65
66func ExtractBackups(page pagination.Page) ([]Backup, error) {
67 casted := page.(BackupPage).Body
68
69 var resp struct {
70 Backups []Backup `mapstructure:"backups" json:"backups"`
71 }
72
73 err := mapstructure.Decode(casted, &resp)
74 return resp.Backups, err
75}