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