blob: d414a7d8dc9e43a757f96102f97b936ec583f60a [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"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040014
Jon Perritt1c2356b2014-10-13 19:56:43 -050015 // Display name.
16 Name string `mapstructure:"display_name"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040017
Jon Perritt1c2356b2014-10-13 19:56:43 -050018 // Instances onto which the Snapshot is attached.
19 Attachments []string `mapstructure:"attachments"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040020
Jon Perritt1c2356b2014-10-13 19:56:43 -050021 // Logical group.
22 AvailabilityZone string `mapstructure:"availability_zone"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040023
Jon Perritt1c2356b2014-10-13 19:56:43 -050024 // Is the Snapshot bootable?
25 Bootable string `mapstructure:"bootable"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040026
Jon Perritt1c2356b2014-10-13 19:56:43 -050027 // Date created.
28 CreatedAt string `mapstructure:"created_at"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040029
Jon Perritt1c2356b2014-10-13 19:56:43 -050030 // Display description.
31 Description string `mapstructure:"display_discription"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040032
Jon Perritt1c2356b2014-10-13 19:56:43 -050033 // See VolumeType object for more information.
34 VolumeType string `mapstructure:"volume_type"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040035
Jon Perritt1c2356b2014-10-13 19:56:43 -050036 // ID of the Snapshot from which this Snapshot was created.
37 SnapshotID string `mapstructure:"snapshot_id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040038
Jon Perritt1c2356b2014-10-13 19:56:43 -050039 // ID of the Volume from which this Snapshot was created.
40 VolumeID string `mapstructure:"volume_id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040041
Jon Perritt1c2356b2014-10-13 19:56:43 -050042 // User-defined key-value pairs.
43 Metadata map[string]string `mapstructure:"metadata"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040044
Jon Perritt1c2356b2014-10-13 19:56:43 -050045 // Unique identifier.
46 ID string `mapstructure:"id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040047
Jon Perritt1c2356b2014-10-13 19:56:43 -050048 // Size of the Snapshot, in GB.
49 Size int `mapstructure:"size"`
Jon Perrittdfff9972014-09-22 01:14:54 -050050}
Jon Perritt56d43b22014-09-22 20:47:11 -050051
Jon Perritt42b3a2a2014-10-02 23:06:07 -050052// CreateResult contains the response body and error from a Create request.
53type CreateResult struct {
54 commonResult
55}
56
57// GetResult contains the response body and error from a Get request.
58type GetResult struct {
59 commonResult
60}
61
62// ListResult is a pagination.Pager that is returned from a call to the List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050063type ListResult struct {
64 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050065}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// IsEmpty returns true if a ListResult contains no Snapshots.
Jon Perritt6d5561b2014-10-01 21:42:15 -050068func (r ListResult) IsEmpty() (bool, error) {
69 volumes, err := ExtractSnapshots(r)
70 if err != nil {
71 return true, err
72 }
73 return len(volumes) == 0, nil
74}
75
Jon Perritt42b3a2a2014-10-02 23:06:07 -050076// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
Jon Perritt6d5561b2014-10-01 21:42:15 -050077func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
78 var response struct {
79 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050080 }
81
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 err := mapstructure.Decode(page.(ListResult).Body, &response)
83 return response.Snapshots, err
84}
85
Jon Perritte357e3d2014-10-03 01:53:57 -050086// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
87type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050088 commonResult
89}
90
Jon Perritte357e3d2014-10-03 01:53:57 -050091// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
92func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
93 if r.Err != nil {
94 return nil, r.Err
95 }
96
Ash Wilsond3dc2542014-10-20 10:10:48 -040097 m := r.Body.(map[string]interface{})["metadata"]
98 return m.(map[string]interface{}), nil
Jon Perritte357e3d2014-10-03 01:53:57 -050099}
100
Jon Perritt6d5561b2014-10-01 21:42:15 -0500101type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -0400102 gophercloud.Result
Jon Perritt6d5561b2014-10-01 21:42:15 -0500103}
104
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500105// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500106func (r commonResult) Extract() (*Snapshot, error) {
107 if r.Err != nil {
108 return nil, r.Err
109 }
110
111 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -0500112 Snapshot *Snapshot `json:"snapshot"`
113 }
114
Ash Wilsond3dc2542014-10-20 10:10:48 -0400115 err := mapstructure.Decode(r.Body, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +0200116
117 return res.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -0500118}