blob: 71d14895584e195f9f04854c381727493b9e29e6 [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
Jon Perrittd7468632014-09-22 21:58:59 -05003import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt6d5561b2014-10-01 21:42:15 -05006
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
Jamie Hannaford38509592014-10-27 11:25:15 +010062// DeleteResult contains the response body and error from a Delete request.
63type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050064 gophercloud.ErrResult
Jamie Hannaford38509592014-10-27 11:25:15 +010065}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// ListResult is a pagination.Pager that is returned from a call to the List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050068type ListResult struct {
69 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050070}
71
Jon Perritt42b3a2a2014-10-02 23:06:07 -050072// IsEmpty returns true if a ListResult contains no Snapshots.
Jon Perritt6d5561b2014-10-01 21:42:15 -050073func (r ListResult) IsEmpty() (bool, error) {
74 volumes, err := ExtractSnapshots(r)
75 if err != nil {
76 return true, err
77 }
78 return len(volumes) == 0, nil
79}
80
Jon Perritt42b3a2a2014-10-02 23:06:07 -050081// ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
Jon Perritt6d5561b2014-10-01 21:42:15 -050082func ExtractSnapshots(page pagination.Page) ([]Snapshot, error) {
83 var response struct {
84 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050085 }
86
Jon Perritt6d5561b2014-10-01 21:42:15 -050087 err := mapstructure.Decode(page.(ListResult).Body, &response)
88 return response.Snapshots, err
89}
90
Jon Perritte357e3d2014-10-03 01:53:57 -050091// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
92type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050093 commonResult
94}
95
Jon Perritte357e3d2014-10-03 01:53:57 -050096// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
97func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
98 if r.Err != nil {
99 return nil, r.Err
100 }
101
Ash Wilsond3dc2542014-10-20 10:10:48 -0400102 m := r.Body.(map[string]interface{})["metadata"]
103 return m.(map[string]interface{}), nil
Jon Perritte357e3d2014-10-03 01:53:57 -0500104}
105
Jon Perritt6d5561b2014-10-01 21:42:15 -0500106type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -0400107 gophercloud.Result
Jon Perritt6d5561b2014-10-01 21:42:15 -0500108}
109
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500110// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500111func (r commonResult) Extract() (*Snapshot, error) {
112 if r.Err != nil {
113 return nil, r.Err
114 }
115
116 var res struct {
Jon Perrittd7468632014-09-22 21:58:59 -0500117 Snapshot *Snapshot `json:"snapshot"`
118 }
119
Ash Wilsond3dc2542014-10-20 10:10:48 -0400120 err := mapstructure.Decode(r.Body, &res)
Jamie Hannaford6a83e802014-10-08 17:13:50 +0200121
122 return res.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -0500123}