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