blob: 071cd330d57e5be0c0cc86fb0014585b670877ad [file] [log] [blame]
Jamie Hannaford302c0b62015-02-16 14:12:34 +01001package backups
2
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
Jamie Hannaford52dbcee2015-10-06 16:09:56 +02006 "github.com/rackspace/gophercloud/openstack/db/v1/datastores"
Jamie Hannaford302c0b62015-02-16 14:12:34 +01007 "github.com/rackspace/gophercloud/pagination"
8)
9
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010010// Status represents the various states a Backup can be in.
11type Status string
12
13// Enum types for the status.
14const (
15 StatusNew Status = "NEW"
16 StatusBuilding Status = "BUILDING"
17 StatusCompleted Status = "COMPLETED"
18 StatusFailed Status = "FAILED"
19 StatusDeleteFailed Status = "DELETE_FAILED"
20)
21
22// Backup represents a Backup API resource.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010023type Backup struct {
24 Description string
25 ID string
26 InstanceID string `json:"instance_id" mapstructure:"instance_id"`
27 LocationRef string
28 Name string
29 ParentID string `json:"parent_id" mapstructure:"parent_id"`
30 Size float64
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010031 Status Status
Jamie Hannaford302c0b62015-02-16 14:12:34 +010032 Created string
33 Updated string
Jamie Hannaforda50d1352015-02-18 11:38:38 +010034 Datastore datastores.DatastorePartial
Jamie Hannaford302c0b62015-02-16 14:12:34 +010035}
36
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010037// CreateResult represents the result of a create operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010038type CreateResult struct {
39 commonResult
40}
41
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010042// GetResult represents the result of a get operation.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010043type GetResult struct {
44 commonResult
45}
46
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010047// DeleteResult represents the result of a delete operation.
48type DeleteResult struct {
49 gophercloud.ErrResult
50}
51
Jamie Hannaford302c0b62015-02-16 14:12:34 +010052type commonResult struct {
53 gophercloud.Result
54}
55
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010056// Extract will retrieve a Backup struct from an operation's result.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010057func (r commonResult) Extract() (*Backup, error) {
58 if r.Err != nil {
59 return nil, r.Err
60 }
61
62 var response struct {
63 Backup Backup `mapstructure:"backup"`
64 }
65
66 err := mapstructure.Decode(r.Body, &response)
67 return &response.Backup, err
68}
69
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010070// BackupPage represents a page of backups.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010071type BackupPage struct {
72 pagination.SinglePageBase
73}
74
75// IsEmpty checks whether an BackupPage struct is empty.
76func (r BackupPage) IsEmpty() (bool, error) {
77 is, err := ExtractBackups(r)
78 if err != nil {
79 return true, err
80 }
81 return len(is) == 0, nil
82}
83
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010084// ExtractBackups will retrieve a slice of Backup structs from a paginated collection.
Jamie Hannaford302c0b62015-02-16 14:12:34 +010085func ExtractBackups(page pagination.Page) ([]Backup, error) {
86 casted := page.(BackupPage).Body
87
88 var resp struct {
89 Backups []Backup `mapstructure:"backups" json:"backups"`
90 }
91
92 err := mapstructure.Decode(casted, &resp)
93 return resp.Backups, err
94}