blob: f0f886434f6157a9a3e4501a27160394eaa2cbb5 [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 Perrittd7468632014-09-22 21:58:59 -05006)
7
Jon Perritt42b3a2a2014-10-02 23:06:07 -05008// Snapshot contains all the information associated with an OpenStack Snapshot.
Jon Perrittdfff9972014-09-22 01:14:54 -05009type Snapshot struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050010 // Currect status of the Snapshot.
Jon Perritt12395212016-02-24 10:41:17 -060011 Status string `json:"status"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040012
Jon Perritt1c2356b2014-10-13 19:56:43 -050013 // Display name.
Jon Perritt12395212016-02-24 10:41:17 -060014 Name string `json:"display_name"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040015
Jon Perritt1c2356b2014-10-13 19:56:43 -050016 // Instances onto which the Snapshot is attached.
Jon Perritt12395212016-02-24 10:41:17 -060017 Attachments []string `json:"attachments"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040018
Jon Perritt1c2356b2014-10-13 19:56:43 -050019 // Logical group.
Jon Perritt12395212016-02-24 10:41:17 -060020 AvailabilityZone string `json:"availability_zone"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040021
Jon Perritt1c2356b2014-10-13 19:56:43 -050022 // Is the Snapshot bootable?
Jon Perritt12395212016-02-24 10:41:17 -060023 Bootable string `json:"bootable"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040024
Jon Perritt1c2356b2014-10-13 19:56:43 -050025 // Date created.
Jon Perritt12395212016-02-24 10:41:17 -060026 CreatedAt gophercloud.JSONRFC3339Milli `json:"created_at"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040027
Jon Perritt1c2356b2014-10-13 19:56:43 -050028 // Display description.
Jon Perritt12395212016-02-24 10:41:17 -060029 Description string `json:"display_discription"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040030
Jon Perritt1c2356b2014-10-13 19:56:43 -050031 // See VolumeType object for more information.
Jon Perritt12395212016-02-24 10:41:17 -060032 VolumeType string `json:"volume_type"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040033
Jon Perritt1c2356b2014-10-13 19:56:43 -050034 // ID of the Snapshot from which this Snapshot was created.
Jon Perritt12395212016-02-24 10:41:17 -060035 SnapshotID string `json:"snapshot_id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040036
Jon Perritt1c2356b2014-10-13 19:56:43 -050037 // ID of the Volume from which this Snapshot was created.
Jon Perritt12395212016-02-24 10:41:17 -060038 VolumeID string `json:"volume_id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040039
Jon Perritt1c2356b2014-10-13 19:56:43 -050040 // User-defined key-value pairs.
Jon Perritt12395212016-02-24 10:41:17 -060041 Metadata map[string]string `json:"metadata"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040042
Jon Perritt1c2356b2014-10-13 19:56:43 -050043 // Unique identifier.
Jon Perritt12395212016-02-24 10:41:17 -060044 ID string `json:"id"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040045
Jon Perritt1c2356b2014-10-13 19:56:43 -050046 // Size of the Snapshot, in GB.
Jon Perritt12395212016-02-24 10:41:17 -060047 Size int `json:"size"`
Jon Perrittdfff9972014-09-22 01:14:54 -050048}
Jon Perritt56d43b22014-09-22 20:47:11 -050049
Jon Perritt42b3a2a2014-10-02 23:06:07 -050050// CreateResult contains the response body and error from a Create request.
51type CreateResult struct {
52 commonResult
53}
54
55// GetResult contains the response body and error from a Get request.
56type GetResult struct {
57 commonResult
58}
59
Jamie Hannaford38509592014-10-27 11:25:15 +010060// DeleteResult contains the response body and error from a Delete request.
61type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050062 gophercloud.ErrResult
Jamie Hannaford38509592014-10-27 11:25:15 +010063}
64
Jon Perritt12395212016-02-24 10:41:17 -060065// SnapshotPage is a pagination.Pager that is returned from a call to the List function.
66type SnapshotPage struct {
Jon Perritt6d5561b2014-10-01 21:42:15 -050067 pagination.SinglePageBase
Jon Perrittd7468632014-09-22 21:58:59 -050068}
69
Jon Perritt12395212016-02-24 10:41:17 -060070// IsEmpty returns true if a SnapshotPage contains no Snapshots.
71func (r SnapshotPage) IsEmpty() (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050072 volumes, err := ExtractSnapshots(r)
Jon Perritt12395212016-02-24 10:41:17 -060073 return len(volumes) == 0, err
Jon Perritt6d5561b2014-10-01 21:42:15 -050074}
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) {
Jon Perritt12395212016-02-24 10:41:17 -060078 r := page.(SnapshotPage)
79 var s struct {
Jon Perritt6d5561b2014-10-01 21:42:15 -050080 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050081 }
Jon Perritt12395212016-02-24 10:41:17 -060082 err := r.ExtractInto(&s)
83 return s.Snapshots, err
Jon Perritt6d5561b2014-10-01 21:42:15 -050084}
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) {
Jon Perritt12395212016-02-24 10:41:17 -0600107 var s struct {
Jon Perrittd7468632014-09-22 21:58:59 -0500108 Snapshot *Snapshot `json:"snapshot"`
109 }
Jon Perritt12395212016-02-24 10:41:17 -0600110 err := r.ExtractInto(&s)
111 return s.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -0500112}