blob: a43bee96f97269552238b907981301cbd4e9c853 [file] [log] [blame]
Jamie Hannafordff08ef92014-10-20 16:10:12 +02001package snapshots
2
3import (
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +02004 "github.com/racker/perigee"
5
Jamie Hannafordff08ef92014-10-20 16:10:12 +02006 "github.com/rackspace/gophercloud"
7 os "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
8 "github.com/rackspace/gophercloud/pagination"
9
10 "github.com/mitchellh/mapstructure"
11)
12
13// Status is the type used to represent a snapshot's status
14type Status string
15
16// Constants to use for supported statuses
17const (
18 Creating Status = "CREATING"
19 Available Status = "AVAILABLE"
20 Deleting Status = "DELETING"
21 Error Status = "ERROR"
22 DeleteError Status = "ERROR_DELETING"
23)
24
25// Snapshot is the Rackspace representation of an external block storage device.
26type Snapshot struct {
27 // The timestamp when this snapshot was created.
28 CreatedAt string `mapstructure:"created_at"`
29
30 // The human-readable description for this snapshot.
31 Description string `mapstructure:"display_description"`
32
33 // The human-readable name for this snapshot.
34 Name string `mapstructure:"display_name"`
35
36 // The UUID for this snapshot.
37 ID string `mapstructure:"id"`
38
39 // The random metadata associated with this snapshot. Note: unlike standard
40 // OpenStack snapshots, this cannot actually be set.
41 Metadata map[string]string `mapstructure:"metadata"`
42
43 // Indicates the current progress of the snapshot's backup procedure.
44 Progress string `mapstructure:"os-extended-snapshot-attributes:progress"`
45
46 // The project ID.
47 ProjectID string `mapstructure:"os-extended-snapshot-attributes:project_id"`
48
49 // The size of the volume which this snapshot backs up.
50 Size int `mapstructure:"size"`
51
52 // The status of the snapshot.
53 Status Status `mapstructure:"status"`
54
55 // The ID of the volume which this snapshot seeks to back up.
56 VolumeID string `mapstructure:"volume_id"`
57}
58
Jamie Hannafordff08ef92014-10-20 16:10:12 +020059// CreateResult represents the result of a create operation
60type CreateResult struct {
61 os.CreateResult
62}
63
64// GetResult represents the result of a get operation
65type GetResult struct {
66 os.GetResult
67}
68
Jamie Hannaford4a783952014-10-20 16:27:33 +020069// UpdateResult represents the result of an update operation
70type UpdateResult struct {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020071 gophercloud.Result
Jamie Hannaford4a783952014-10-20 16:27:33 +020072}
73
Jamie Hannafordc7589632014-10-27 11:39:17 +010074// DeleteResult represents the result of a delete operation
75type DeleteResult struct {
76 os.DeleteResult
77}
78
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020079func commonExtract(resp interface{}, err error) (*Snapshot, error) {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020080 if err != nil {
81 return nil, err
82 }
83
84 var respStruct struct {
85 Snapshot *Snapshot `json:"snapshot"`
86 }
87
88 err = mapstructure.Decode(resp, &respStruct)
89
90 return respStruct.Snapshot, err
91}
92
93// Extract will get the Snapshot object out of the GetResult object.
94func (r GetResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020095 return commonExtract(r.Body, r.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020096}
97
98// Extract will get the Snapshot object out of the CreateResult object.
99func (r CreateResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +0200100 return commonExtract(r.Body, r.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +0200101}
102
Jamie Hannaford4a783952014-10-20 16:27:33 +0200103// Extract will get the Snapshot object out of the UpdateResult object.
104func (r UpdateResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +0200105 return commonExtract(r.Body, r.Err)
Jamie Hannaford4a783952014-10-20 16:27:33 +0200106}
107
Jamie Hannafordff08ef92014-10-20 16:10:12 +0200108// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
109func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
110 var response struct {
111 Snapshots []Snapshot `json:"snapshots"`
112 }
113
114 err := mapstructure.Decode(page.(os.ListResult).Body, &response)
115 return response.Snapshots, err
116}
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200117
118// WaitUntilComplete will continually poll a snapshot until it successfully
119// transitions to a specified state. It will do this for at most the number of
120// seconds specified.
121func (snapshot Snapshot) WaitUntilComplete(c *gophercloud.ServiceClient, timeout int) error {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200122 return gophercloud.WaitFor(timeout, func() (bool, error) {
123 // Poll resource
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200124 current, err := Get(c, snapshot.ID).Extract()
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200125 if err != nil {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200126 return false, err
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200127 }
128
Jamie Hannaford878cce02014-10-23 16:58:43 +0200129 // Has it been built yet?
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200130 if current.Progress == "100%" {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200131 return true, nil
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200132 }
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200133
Jamie Hannaford878cce02014-10-23 16:58:43 +0200134 return false, nil
135 })
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200136}
137
Jamie Hannaford878cce02014-10-23 16:58:43 +0200138// WaitUntilDeleted will continually poll a snapshot until it has been
139// successfully deleted, i.e. returns a 404 status.
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200140func (snapshot Snapshot) WaitUntilDeleted(c *gophercloud.ServiceClient, timeout int) error {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200141 return gophercloud.WaitFor(timeout, func() (bool, error) {
142 // Poll resource
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200143 _, err := Get(c, snapshot.ID).Extract()
144
Jamie Hannaford878cce02014-10-23 16:58:43 +0200145 // Check for a 404
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200146 if casted, ok := err.(*perigee.UnexpectedResponseCodeError); ok && casted.Actual == 404 {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200147 return true, nil
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200148 } else if err != nil {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200149 return false, err
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200150 }
151
Jamie Hannaford878cce02014-10-23 16:58:43 +0200152 return false, nil
153 })
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200154}