blob: dc94a32f01cfbcc38c1884b28f016bbf66e34699 [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
Jon Perrittd7468632014-09-22 21:58:59 -05003import (
Jon Perritt6d5561b2014-10-01 21:42:15 -05004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
6
Jon Perrittd7468632014-09-22 21:58:59 -05007 "github.com/mitchellh/mapstructure"
8)
9
Jon Perritt42b3a2a2014-10-02 23:06:07 -050010// Snapshot contains all the information associated with an OpenStack Snapshot.
Jon Perrittdfff9972014-09-22 01:14:54 -050011type Snapshot struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050012 // Currect status of the Snapshot.
13 Status string `mapstructure:"status"`
14 // Display name.
15 Name string `mapstructure:"display_name"`
16 // Instances onto which the Snapshot is attached.
17 Attachments []string `mapstructure:"attachments"`
18 // Logical group.
19 AvailabilityZone string `mapstructure:"availability_zone"`
20 // Is the Snapshot bootable?
21 Bootable string `mapstructure:"bootable"`
22 // Date created.
23 CreatedAt string `mapstructure:"created_at"`
24 // Display description.
25 Description string `mapstructure:"display_discription"`
26 // See VolumeType object for more information.
27 VolumeType string `mapstructure:"volume_type"`
28 // ID of the Snapshot from which this Snapshot was created.
29 SnapshotID string `mapstructure:"snapshot_id"`
30 // ID of the Volume from which this Snapshot was created.
31 VolumeID string `mapstructure:"volume_id"`
32 // User-defined key-value pairs.
33 Metadata map[string]string `mapstructure:"metadata"`
34 // Unique identifier.
35 ID string `mapstructure:"id"`
36 // Size of the Snapshot, in GB.
37 Size int `mapstructure:"size"`
Jon Perrittdfff9972014-09-22 01:14:54 -050038}
Jon Perritt56d43b22014-09-22 20:47:11 -050039
Jon Perritt42b3a2a2014-10-02 23:06:07 -050040// CreateResult contains the response body and error from a Create request.
41type CreateResult struct {
42 commonResult
43}
44
45// GetResult contains the response body and error from a Get request.
46type GetResult struct {
47 commonResult
48}
49
50// ListResult is a pagination.Pager that is returned from a call to the List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050051type ListResult struct {
52 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050053}
54
Jon Perritt42b3a2a2014-10-02 23:06:07 -050055// IsEmpty returns true if a ListResult contains no Snapshots.
Jon Perritt6d5561b2014-10-01 21:42:15 -050056func (r ListResult) IsEmpty() (bool, error) {
57 volumes, err := ExtractSnapshots(r)
58 if err != nil {
59 return true, err
60 }
61 return len(volumes) == 0, nil
62}
63
Jon Perritt42b3a2a2014-10-02 23:06:07 -050064// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
Jon Perritt6d5561b2014-10-01 21:42:15 -050065func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
66 var response struct {
67 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050068 }
69
Jon Perritt6d5561b2014-10-01 21:42:15 -050070 err := mapstructure.Decode(page.(ListResult).Body, &response)
71 return response.Snapshots, err
72}
73
Jon Perritte357e3d2014-10-03 01:53:57 -050074// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
75type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050076 commonResult
77}
78
Jon Perritte357e3d2014-10-03 01:53:57 -050079// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
80func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
81 if r.Err != nil {
82 return nil, r.Err
83 }
84
85 m := r.Resp["metadata"].(map[string]interface{})
86
87 return m, nil
88}
89
Jon Perritt6d5561b2014-10-01 21:42:15 -050090type commonResult struct {
91 gophercloud.CommonResult
92}
93
Jon Perritt42b3a2a2014-10-02 23:06:07 -050094// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050095func (r commonResult) Extract() (*Snapshot, error) {
96 if r.Err != nil {
97 return nil, r.Err
98 }
99
100 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -0500101 Snapshot *Snapshot `json:"snapshot"`
102 }
103
Jon Perritt6d5561b2014-10-01 21:42:15 -0500104 err := mapstructure.Decode(r.Resp, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +0200105
106 return res.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -0500107}