blob: 89f1fc69c8548a973a2bf43a281591ec070adda6 [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"
7)
8
Jamie Hannaforde0524732015-02-16 14:44:13 +01009type Datastore struct {
10 Version string
11 Type string
12 VersionID string `json:"version_id" mapstructure:"version_id"`
13}
14
Jamie Hannaford302c0b62015-02-16 14:12:34 +010015type Backup struct {
16 Description string
17 ID string
18 InstanceID string `json:"instance_id" mapstructure:"instance_id"`
19 LocationRef string
20 Name string
21 ParentID string `json:"parent_id" mapstructure:"parent_id"`
22 Size float64
23 Status string
24 Created string
25 Updated string
Jamie Hannaforde0524732015-02-16 14:44:13 +010026 Datastore Datastore
Jamie Hannaford302c0b62015-02-16 14:12:34 +010027}
28
29type CreateResult struct {
30 commonResult
31}
32
33type GetResult struct {
34 commonResult
35}
36
37type commonResult struct {
38 gophercloud.Result
39}
40
41func (r commonResult) Extract() (*Backup, error) {
42 if r.Err != nil {
43 return nil, r.Err
44 }
45
46 var response struct {
47 Backup Backup `mapstructure:"backup"`
48 }
49
50 err := mapstructure.Decode(r.Body, &response)
51 return &response.Backup, err
52}
53
54type DeleteResult struct {
55 gophercloud.ErrResult
56}
57
58type BackupPage struct {
59 pagination.SinglePageBase
60}
61
62// IsEmpty checks whether an BackupPage struct is empty.
63func (r BackupPage) IsEmpty() (bool, error) {
64 is, err := ExtractBackups(r)
65 if err != nil {
66 return true, err
67 }
68 return len(is) == 0, nil
69}
70
71func ExtractBackups(page pagination.Page) ([]Backup, error) {
72 casted := page.(BackupPage).Body
73
74 var resp struct {
75 Backups []Backup `mapstructure:"backups" json:"backups"`
76 }
77
78 err := mapstructure.Decode(casted, &resp)
79 return resp.Backups, err
80}