blob: 8f4704c19aa3bc1e6a8c938d6961a52a64855c0d [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
57type commonResult struct {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020058 gophercloud.Result
Jamie Hannafordff08ef92014-10-20 16:10:12 +020059}
60
61// CreateResult represents the result of a create operation
62type CreateResult struct {
63 os.CreateResult
64}
65
66// GetResult represents the result of a get operation
67type GetResult struct {
68 os.GetResult
69}
70
Jamie Hannaford4a783952014-10-20 16:27:33 +020071// UpdateResult represents the result of an update operation
72type UpdateResult struct {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020073 gophercloud.Result
Jamie Hannaford4a783952014-10-20 16:27:33 +020074}
75
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020076func commonExtract(resp interface{}, err error) (*Snapshot, error) {
Jamie Hannafordff08ef92014-10-20 16:10:12 +020077 if err != nil {
78 return nil, err
79 }
80
81 var respStruct struct {
82 Snapshot *Snapshot `json:"snapshot"`
83 }
84
85 err = mapstructure.Decode(resp, &respStruct)
86
87 return respStruct.Snapshot, err
88}
89
90// Extract will get the Snapshot object out of the GetResult object.
91func (r GetResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020092 return commonExtract(r.Body, r.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020093}
94
95// Extract will get the Snapshot object out of the CreateResult object.
96func (r CreateResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +020097 return commonExtract(r.Body, r.Err)
Jamie Hannafordff08ef92014-10-20 16:10:12 +020098}
99
Jamie Hannaford4a783952014-10-20 16:27:33 +0200100// Extract will get the Snapshot object out of the UpdateResult object.
101func (r UpdateResult) Extract() (*Snapshot, error) {
Jamie Hannafordeff7e8d2014-10-21 11:02:05 +0200102 return commonExtract(r.Body, r.Err)
Jamie Hannaford4a783952014-10-20 16:27:33 +0200103}
104
Jamie Hannafordff08ef92014-10-20 16:10:12 +0200105// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
106func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
107 var response struct {
108 Snapshots []Snapshot `json:"snapshots"`
109 }
110
111 err := mapstructure.Decode(page.(os.ListResult).Body, &response)
112 return response.Snapshots, err
113}