blob: 9509bca8baa7f0d736953541d777fcda29bc808d [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
Jon Perrittd7468632014-09-22 21:58:59 -05003import (
4 "fmt"
5
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8
Jon Perrittd7468632014-09-22 21:58:59 -05009 "github.com/mitchellh/mapstructure"
10)
11
Jon Perritt42b3a2a2014-10-02 23:06:07 -050012// Snapshot contains all the information associated with an OpenStack Snapshot.
Jon Perrittdfff9972014-09-22 01:14:54 -050013type Snapshot struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014 Status string `mapstructure:"status"` // currect status of the Snapshot
15 Name string `mapstructure:"display_name"` // display name
16 Attachments []string `mapstructure:"attachments"` // instances onto which the Snapshot is attached
17 AvailabilityZone string `mapstructure:"availability_zone"` // logical group
18 Bootable string `mapstructure:"bootable"` // is the Snapshot bootable
19 CreatedAt string `mapstructure:"created_at"` // date created
20 Description string `mapstructure:"display_discription"` // display description
21 VolumeType string `mapstructure:"volume_type"` // see VolumeType object for more information
22 SnapshotID string `mapstructure:"snapshot_id"` // ID of the Snapshot from which this Snapshot was created
23 SourceVolID string `mapstructure:"source_volid"` // ID of the Volume from which this Snapshot was created
24 Metadata map[string]string `mapstructure:"metadata"` // user-defined key-value pairs
25 ID string `mapstructure:"id"` // unique identifier
26 Size int `mapstructure:"size"` // size of the Snapshot, in GB
Jon Perrittdfff9972014-09-22 01:14:54 -050027}
Jon Perritt56d43b22014-09-22 20:47:11 -050028
Jon Perritt42b3a2a2014-10-02 23:06:07 -050029// CreateResult contains the response body and error from a Create request.
30type CreateResult struct {
31 commonResult
32}
33
34// GetResult contains the response body and error from a Get request.
35type GetResult struct {
36 commonResult
37}
38
39// ListResult is a pagination.Pager that is returned from a call to the List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050040type ListResult struct {
41 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050042}
43
Jon Perritt42b3a2a2014-10-02 23:06:07 -050044// IsEmpty returns true if a ListResult contains no Snapshots.
Jon Perritt6d5561b2014-10-01 21:42:15 -050045func (r ListResult) IsEmpty() (bool, error) {
46 volumes, err := ExtractSnapshots(r)
47 if err != nil {
48 return true, err
49 }
50 return len(volumes) == 0, nil
51}
52
Jon Perritt42b3a2a2014-10-02 23:06:07 -050053// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
Jon Perritt6d5561b2014-10-01 21:42:15 -050054func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
55 var response struct {
56 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050057 }
58
Jon Perritt6d5561b2014-10-01 21:42:15 -050059 err := mapstructure.Decode(page.(ListResult).Body, &response)
60 return response.Snapshots, err
61}
62
Jon Perritte357e3d2014-10-03 01:53:57 -050063// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
64type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050065 commonResult
66}
67
Jon Perritte357e3d2014-10-03 01:53:57 -050068// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
69func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
70 if r.Err != nil {
71 return nil, r.Err
72 }
73
74 m := r.Resp["metadata"].(map[string]interface{})
75
76 return m, nil
77}
78
Jon Perritt6d5561b2014-10-01 21:42:15 -050079type commonResult struct {
80 gophercloud.CommonResult
81}
82
Jon Perritt42b3a2a2014-10-02 23:06:07 -050083// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -050084func (r commonResult) Extract() (*Snapshot, error) {
85 if r.Err != nil {
86 return nil, r.Err
87 }
88
89 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -050090 Snapshot *Snapshot `json:"snapshot"`
91 }
92
Jon Perritt6d5561b2014-10-01 21:42:15 -050093 err := mapstructure.Decode(r.Resp, &res)
Jon Perrittd7468632014-09-22 21:58:59 -050094 if err != nil {
Jon Perritt6d5561b2014-10-01 21:42:15 -050095 return nil, fmt.Errorf("snapshots: Error decoding snapshots.commonResult: %v", err)
Jon Perrittd7468632014-09-22 21:58:59 -050096 }
Jon Perritt6d5561b2014-10-01 21:42:15 -050097 return res.Snapshot, nil
Jon Perrittd7468632014-09-22 21:58:59 -050098}