blob: 0fab2828bc21f5ccd533a51465314287a9df6324 [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 Hannafordeff7e8d2014-10-21 11:02:05 +020074func commonExtract(resp interface{}, err error) (*Snapshot, error) {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020075 if err != nil {
76 return nil, err
77 }
78
79 var respStruct struct {
80 Snapshot *Snapshot `json:"snapshot"`
81 }
82
83 err = mapstructure.Decode(resp, &respStruct)
84
85 return respStruct.Snapshot, err
86}
87
88// Extract will get the Snapshot object out of the GetResult object.
89func (r GetResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020090 return commonExtract(r.Body, r.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020091}
92
93// Extract will get the Snapshot object out of the CreateResult object.
94func (r CreateResult) 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
Jamie Hannaford4a783952014-10-20 16:27:33 +020098// Extract will get the Snapshot object out of the UpdateResult object.
99func (r UpdateResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +0200100 return commonExtract(r.Body, r.Err)
Jamie Hannaford4a783952014-10-20 16:27:33 +0200101}
102
Jamie Hannafordff08ef92014-10-20 16:10:12 +0200103// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
104func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
105 var response struct {
106 Snapshots []Snapshot `json:"snapshots"`
107 }
108
109 err := mapstructure.Decode(page.(os.ListResult).Body, &response)
110 return response.Snapshots, err
111}
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200112
113// WaitUntilComplete will continually poll a snapshot until it successfully
114// transitions to a specified state. It will do this for at most the number of
115// seconds specified.
116func (snapshot Snapshot) WaitUntilComplete(c *gophercloud.ServiceClient, timeout int) error {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200117 return gophercloud.WaitFor(timeout, func() (bool, error) {
118 // Poll resource
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200119 current, err := Get(c, snapshot.ID).Extract()
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200120 if err != nil {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200121 return false, err
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200122 }
123
Jamie Hannaford878cce02014-10-23 16:58:43 +0200124 // Has it been built yet?
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200125 if current.Progress == "100%" {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200126 return true, nil
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200127 }
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200128
Jamie Hannaford878cce02014-10-23 16:58:43 +0200129 return false, nil
130 })
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200131}
132
Jamie Hannaford878cce02014-10-23 16:58:43 +0200133// WaitUntilDeleted will continually poll a snapshot until it has been
134// successfully deleted, i.e. returns a 404 status.
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200135func (snapshot Snapshot) WaitUntilDeleted(c *gophercloud.ServiceClient, timeout int) error {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200136 return gophercloud.WaitFor(timeout, func() (bool, error) {
137 // Poll resource
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200138 _, err := Get(c, snapshot.ID).Extract()
139
Jamie Hannaford878cce02014-10-23 16:58:43 +0200140 // Check for a 404
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200141 if casted, ok := err.(*perigee.UnexpectedResponseCodeError); ok && casted.Actual == 404 {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200142 return true, nil
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200143 } else if err != nil {
Jamie Hannaford878cce02014-10-23 16:58:43 +0200144 return false, err
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200145 }
146
Jamie Hannaford878cce02014-10-23 16:58:43 +0200147 return false, nil
148 })
Jamie Hannaford3a3d0ee2014-10-23 14:48:20 +0200149}