blob: d23090d180196dc904baa74c3eee7f2a9f1b0b66 [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 Perritt42b3a2a2014-10-02 23:06:07 -050012 Status string `mapstructure:"status"` // currect status of the Snapshot
13 Name string `mapstructure:"display_name"` // display name
14 Attachments []string `mapstructure:"attachments"` // instances onto which the Snapshot is attached
15 AvailabilityZone string `mapstructure:"availability_zone"` // logical group
16 Bootable string `mapstructure:"bootable"` // is the Snapshot bootable
17 CreatedAt string `mapstructure:"created_at"` // date created
18 Description string `mapstructure:"display_discription"` // display description
19 VolumeType string `mapstructure:"volume_type"` // see VolumeType object for more information
20 SnapshotID string `mapstructure:"snapshot_id"` // ID of the Snapshot from which this Snapshot was created
21 SourceVolID string `mapstructure:"source_volid"` // ID of the Volume from which this Snapshot was created
22 Metadata map[string]string `mapstructure:"metadata"` // user-defined key-value pairs
23 ID string `mapstructure:"id"` // unique identifier
24 Size int `mapstructure:"size"` // size of the Snapshot, in GB
Jon Perrittdfff9972014-09-22 01:14:54 -050025}
Jon Perritt56d43b22014-09-22 20:47:11 -050026
Jon Perritt42b3a2a2014-10-02 23:06:07 -050027// CreateResult contains the response body and error from a Create request.
28type CreateResult struct {
29 commonResult
30}
31
32// GetResult contains the response body and error from a Get request.
33type GetResult struct {
34 commonResult
35}
36
37// ListResult is a pagination.Pager that is returned from a call to the List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050038type ListResult struct {
39 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050040}
41
Jon Perritt42b3a2a2014-10-02 23:06:07 -050042// IsEmpty returns true if a ListResult contains no Snapshots.
Jon Perritt6d5561b2014-10-01 21:42:15 -050043func (r ListResult) IsEmpty() (bool, error) {
44 volumes, err := ExtractSnapshots(r)
45 if err != nil {
46 return true, err
47 }
48 return len(volumes) == 0, nil
49}
50
Jon Perritt42b3a2a2014-10-02 23:06:07 -050051// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
Jon Perritt6d5561b2014-10-01 21:42:15 -050052func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
53 var response struct {
54 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050055 }
56
Jon Perritt6d5561b2014-10-01 21:42:15 -050057 err := mapstructure.Decode(page.(ListResult).Body, &response)
58 return response.Snapshots, err
59}
60
Jon Perritte357e3d2014-10-03 01:53:57 -050061// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
62type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050063 commonResult
64}
65
Jon Perritte357e3d2014-10-03 01:53:57 -050066// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
67func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
68 if r.Err != nil {
69 return nil, r.Err
70 }
71
72 m := r.Resp["metadata"].(map[string]interface{})
73
74 return m, nil
75}
76
Jon Perritt6d5561b2014-10-01 21:42:15 -050077type commonResult struct {
78 gophercloud.CommonResult
79}
80
Jon Perritt42b3a2a2014-10-02 23:06:07 -050081// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050082func (r commonResult) Extract() (*Snapshot, error) {
83 if r.Err != nil {
84 return nil, r.Err
85 }
86
87 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -050088 Snapshot *Snapshot `json:"snapshot"`
89 }
90
Jon Perritt6d5561b2014-10-01 21:42:15 -050091 err := mapstructure.Decode(r.Resp, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +020092
93 return res.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -050094}