blob: 5c05f9ba9fd900648524c8b4415a11603806dcab [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittdfff9972014-09-22 01:14:54 -05006)
7
Jon Perritt1c2356b2014-10-13 19:56:43 -05008// CreateOptsBuilder allows extensions to add additional parameters to the
9// Create request.
10type CreateOptsBuilder interface {
11 ToSnapshotCreateMap() (map[string]interface{}, error)
12}
13
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014// CreateOpts contains options for creating a Snapshot. This object is passed to
15// the snapshots.Create function. For more information about these parameters,
16// see the Snapshot object.
Jon Perrittdfff9972014-09-22 01:14:54 -050017type CreateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -060018 VolumeID string `json:"volume_id" required:"true"`
19 Description string `json:"display_description,omitempty"`
20 Force bool `json:"force,omitempty"`
21 Metadata map[string]interface{} `json:"metadata,omitempty"`
22 Name string `json:"display_name,omitempty"`
Jon Perrittdfff9972014-09-22 01:14:54 -050023}
24
Jon Perritt1c2356b2014-10-13 19:56:43 -050025// ToSnapshotCreateMap assembles a request body based on the contents of a
26// CreateOpts.
27func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060028 return gophercloud.BuildRequestBody(opts, "snapshot")
Jon Perritt1c2356b2014-10-13 19:56:43 -050029}
30
31// Create will create a new Snapshot based on the values in CreateOpts. To
32// extract the Snapshot object from the response, call the Extract method on the
Jon Perritt42b3a2a2014-10-02 23:06:07 -050033// CreateResult.
Jon Perritt3860b512016-03-29 12:01:48 -050034func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 b, err := opts.ToSnapshotCreateMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -050036 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060037 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050038 return
Jon Perritt1c2356b2014-10-13 19:56:43 -050039 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060040 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010041 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050042 })
Jon Perrittdfff9972014-09-22 01:14:54 -050043}
Jon Perritt56d43b22014-09-22 20:47:11 -050044
Jon Perritt42b3a2a2014-10-02 23:06:07 -050045// Delete will delete the existing Snapshot with the provided ID.
Jon Perritt3860b512016-03-29 12:01:48 -050046func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060047 _, r.Err = client.Delete(deleteURL(client, id), nil)
Jon Perrittd4788f92014-09-24 12:05:27 -050048}
49
Jon Perritt1c2356b2014-10-13 19:56:43 -050050// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
51// object from the response, call the Extract method on the GetResult.
Jon Perritt3860b512016-03-29 12:01:48 -050052func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060053 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
Jon Perritt6d5561b2014-10-01 21:42:15 -050054}
55
Jon Perritt1c2356b2014-10-13 19:56:43 -050056// ListOptsBuilder allows extensions to add additional parameters to the List
57// request.
58type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050059 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050060}
61
Jon Perritt42b3a2a2014-10-02 23:06:07 -050062// ListOpts hold options for listing Snapshots. It is passed to the
63// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050064type ListOpts struct {
65 Name string `q:"display_name"`
66 Status string `q:"status"`
67 VolumeID string `q:"volume_id"`
68}
69
Jon Perritt26780d52014-10-14 11:35:58 -050070// ToSnapshotListQuery formats a ListOpts into a query string.
71func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -050072 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060073 return q.String(), err
Jon Perritt1c2356b2014-10-13 19:56:43 -050074}
75
76// List returns Snapshots optionally limited by the conditions provided in
77// ListOpts.
78func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -050079 url := listURL(client)
80 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050081 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 if err != nil {
83 return pagination.Pager{Err: err}
84 }
Jon Perritt1c2356b2014-10-13 19:56:43 -050085 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -050086 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060087 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060088 return SnapshotPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050090}
91
Jon Perritt1c2356b2014-10-13 19:56:43 -050092// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
93// the Update request.
94type UpdateMetadataOptsBuilder interface {
95 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
96}
97
Jamie Hannafordd8275bb2014-10-06 16:12:23 +020098// UpdateMetadataOpts contain options for updating an existing Snapshot. This
99// object is passed to the snapshots.Update function. For more information
100// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500101type UpdateMetadataOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600102 Metadata map[string]interface{} `json:"metadata,omitempty"`
Jon Perritt6d5561b2014-10-01 21:42:15 -0500103}
104
Jon Perritt1c2356b2014-10-13 19:56:43 -0500105// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
106// an UpdateMetadataOpts.
107func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600108 return gophercloud.BuildRequestBody(opts, "")
Jon Perritt1c2356b2014-10-13 19:56:43 -0500109}
110
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200111// UpdateMetadata will update the Snapshot with provided information. To
112// extract the updated Snapshot from the response, call the ExtractMetadata
113// method on the UpdateMetadataResult.
Jon Perritt3860b512016-03-29 12:01:48 -0500114func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600115 b, err := opts.ToSnapshotUpdateMetadataMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -0500116 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600117 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500118 return
Jon Perritt1c2356b2014-10-13 19:56:43 -0500119 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600120 _, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100121 OkCodes: []int{200},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500122 })
Jon Perritt56d43b22014-09-22 20:47:11 -0500123}
Jon Perritt24c20832015-06-30 09:57:00 -0600124
125// IDFromName is a convienience function that returns a snapshot's ID given its name.
126func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600127 count := 0
128 id := ""
Jon Perritted258942016-02-28 23:38:21 -0600129 pages, err := List(client, nil).AllPages()
130 if err != nil {
131 return "", err
132 }
Jon Perritt24c20832015-06-30 09:57:00 -0600133
Jon Perritted258942016-02-28 23:38:21 -0600134 all, err := ExtractSnapshots(pages)
135 if err != nil {
136 return "", err
137 }
138
139 for _, s := range all {
140 if s.Name == name {
141 count++
142 id = s.ID
143 }
144 }
145
146 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600147 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600148 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600149 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600150 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600151 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600152 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600153 }
154}