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