blob: 2bcb74f4c6df90ad7739afd3350b1b15bb60f133 [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.
Joe Topjian614b51d2016-08-03 12:19:30 -060026 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
Ash Wilsond3dc2542014-10-20 10:10:48 -040027
Jon Perritt1c2356b2014-10-13 19:56:43 -050028 // Display description.
Joe Topjian614b51d2016-08-03 12:19:30 -060029 Description string `json:"display_description"`
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 Perritt31b66462016-02-25 22:25:30 -060077func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
Jon Perritt12395212016-02-24 10:41:17 -060078 var s struct {
Jon Perritt6d5561b2014-10-01 21:42:15 -050079 Snapshots []Snapshot `json:"snapshots"`
Jon Perrittd7468632014-09-22 21:58:59 -050080 }
Jon Perritt31b66462016-02-25 22:25:30 -060081 err := (r.(SnapshotPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -060082 return s.Snapshots, err
Jon Perritt6d5561b2014-10-01 21:42:15 -050083}
84
Jon Perritte357e3d2014-10-03 01:53:57 -050085// UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
86type UpdateMetadataResult struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050087 commonResult
88}
89
Jon Perritte357e3d2014-10-03 01:53:57 -050090// ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
91func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
92 if r.Err != nil {
93 return nil, r.Err
94 }
Ash Wilsond3dc2542014-10-20 10:10:48 -040095 m := r.Body.(map[string]interface{})["metadata"]
96 return m.(map[string]interface{}), nil
Jon Perritte357e3d2014-10-03 01:53:57 -050097}
98
Jon Perritt6d5561b2014-10-01 21:42:15 -050099type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -0400100 gophercloud.Result
Jon Perritt6d5561b2014-10-01 21:42:15 -0500101}
102
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500103// Extract will get the Snapshot object out of the commonResult object.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500104func (r commonResult) Extract() (*Snapshot, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600105 var s struct {
Jon Perrittd7468632014-09-22 21:58:59 -0500106 Snapshot *Snapshot `json:"snapshot"`
107 }
Jon Perritt12395212016-02-24 10:41:17 -0600108 err := r.ExtractInto(&s)
109 return s.Snapshot, err
Jon Perrittd7468632014-09-22 21:58:59 -0500110}