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 | |
| 57 | type commonResult struct { |
| 58 | gophercloud.CommonResult |
| 59 | } |
| 60 | |
| 61 | // CreateResult represents the result of a create operation |
| 62 | type CreateResult struct { |
| 63 | os.CreateResult |
| 64 | } |
| 65 | |
| 66 | // GetResult represents the result of a get operation |
| 67 | type GetResult struct { |
| 68 | os.GetResult |
| 69 | } |
| 70 | |
| 71 | func commonExtract(resp map[string]interface{}, err error) (*Snapshot, error) { |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | var respStruct struct { |
| 77 | Snapshot *Snapshot `json:"snapshot"` |
| 78 | } |
| 79 | |
| 80 | err = mapstructure.Decode(resp, &respStruct) |
| 81 | |
| 82 | return respStruct.Snapshot, err |
| 83 | } |
| 84 | |
| 85 | // Extract will get the Snapshot object out of the GetResult object. |
| 86 | func (r GetResult) Extract() (*Snapshot, error) { |
| 87 | return commonExtract(r.Resp, r.Err) |
| 88 | } |
| 89 | |
| 90 | // Extract will get the Snapshot object out of the CreateResult object. |
| 91 | func (r CreateResult) Extract() (*Snapshot, error) { |
| 92 | return commonExtract(r.Resp, r.Err) |
| 93 | } |
| 94 | |
| 95 | // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call. |
| 96 | func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) { |
| 97 | var response struct { |
| 98 | Snapshots []Snapshot `json:"snapshots"` |
| 99 | } |
| 100 | |
| 101 | err := mapstructure.Decode(page.(os.ListResult).Body, &response) |
| 102 | return response.Snapshots, err |
| 103 | } |