| Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 1 | package snapshots | 
|  | 2 |  | 
|  | 3 | import ( | 
|  | 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 | 
|  | 12 | type Status string | 
|  | 13 |  | 
|  | 14 | // Constants to use for supported statuses | 
|  | 15 | const ( | 
|  | 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. | 
|  | 24 | type 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 Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 57 | // CreateResult represents the result of a create operation | 
|  | 58 | type CreateResult struct { | 
|  | 59 | os.CreateResult | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | // GetResult represents the result of a get operation | 
|  | 63 | type GetResult struct { | 
|  | 64 | os.GetResult | 
|  | 65 | } | 
|  | 66 |  | 
| Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 67 | // UpdateResult represents the result of an update operation | 
|  | 68 | type UpdateResult struct { | 
| Jamie Hannaford | eff7e8d | 2014-10-21 11:02:05 +0200 | [diff] [blame] | 69 | gophercloud.Result | 
| Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Jamie Hannaford | eff7e8d | 2014-10-21 11:02:05 +0200 | [diff] [blame] | 72 | func commonExtract(resp interface{}, err error) (*Snapshot, error) { | 
| Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 73 | 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. | 
|  | 87 | func (r GetResult) Extract() (*Snapshot, error) { | 
| Jamie Hannaford | eff7e8d | 2014-10-21 11:02:05 +0200 | [diff] [blame] | 88 | return commonExtract(r.Body, r.Err) | 
| Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
|  | 91 | // Extract will get the Snapshot object out of the CreateResult object. | 
|  | 92 | func (r CreateResult) Extract() (*Snapshot, error) { | 
| Jamie Hannaford | eff7e8d | 2014-10-21 11:02:05 +0200 | [diff] [blame] | 93 | return commonExtract(r.Body, r.Err) | 
| Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 96 | // Extract will get the Snapshot object out of the UpdateResult object. | 
|  | 97 | func (r UpdateResult) Extract() (*Snapshot, error) { | 
| Jamie Hannaford | eff7e8d | 2014-10-21 11:02:05 +0200 | [diff] [blame] | 98 | return commonExtract(r.Body, r.Err) | 
| Jamie Hannaford | 4a78395 | 2014-10-20 16:27:33 +0200 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Jamie Hannaford | ff08ef9 | 2014-10-20 16:10:12 +0200 | [diff] [blame] | 101 | // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call. | 
|  | 102 | func 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 Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 110 |  | 
|  | 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. | 
|  | 114 | func (snapshot Snapshot) WaitUntilComplete(c *gophercloud.ServiceClient, timeout int) error { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 115 | return gophercloud.WaitFor(timeout, func() (bool, error) { | 
|  | 116 | // Poll resource | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 117 | current, err := Get(c, snapshot.ID).Extract() | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 118 | if err != nil { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 119 | return false, err | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 122 | // Has it been built yet? | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 123 | if current.Progress == "100%" { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 124 | return true, nil | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 125 | } | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 126 |  | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 127 | return false, nil | 
|  | 128 | }) | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 131 | // WaitUntilDeleted will continually poll a snapshot until it has been | 
|  | 132 | // successfully deleted, i.e. returns a 404 status. | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 133 | func (snapshot Snapshot) WaitUntilDeleted(c *gophercloud.ServiceClient, timeout int) error { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 134 | return gophercloud.WaitFor(timeout, func() (bool, error) { | 
|  | 135 | // Poll resource | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 136 | _, err := Get(c, snapshot.ID).Extract() | 
|  | 137 |  | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 138 | // Check for a 404 | 
| Ash Wilson | accf2f5 | 2015-02-12 16:26:46 -0500 | [diff] [blame] | 139 | if casted, ok := err.(*gophercloud.UnexpectedResponseCodeError); ok && casted.Actual == 404 { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 140 | return true, nil | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 141 | } else if err != nil { | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 142 | return false, err | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Jamie Hannaford | 878cce0 | 2014-10-23 16:58:43 +0200 | [diff] [blame] | 145 | return false, nil | 
|  | 146 | }) | 
| Jamie Hannaford | 3a3d0ee | 2014-10-23 14:48:20 +0200 | [diff] [blame] | 147 | } |