blob: cb9d0d0e068c0ca18b3560d25849b1e214e23492 [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 })
jrperritt29ae6b32016-04-13 12:59:37 -050043 return
Jon Perrittdfff9972014-09-22 01:14:54 -050044}
Jon Perritt56d43b22014-09-22 20:47:11 -050045
Jon Perritt42b3a2a2014-10-02 23:06:07 -050046// Delete will delete the existing Snapshot with the provided ID.
Jon Perritt3860b512016-03-29 12:01:48 -050047func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060048 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -050049 return
Jon Perrittd4788f92014-09-24 12:05:27 -050050}
51
Jon Perritt1c2356b2014-10-13 19:56:43 -050052// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
53// object from the response, call the Extract method on the GetResult.
Jon Perritt3860b512016-03-29 12:01:48 -050054func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060055 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -050056 return
Jon Perritt6d5561b2014-10-01 21:42:15 -050057}
58
Jon Perritt1c2356b2014-10-13 19:56:43 -050059// ListOptsBuilder allows extensions to add additional parameters to the List
60// request.
61type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050062 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050063}
64
Jon Perritt42b3a2a2014-10-02 23:06:07 -050065// ListOpts hold options for listing Snapshots. It is passed to the
66// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050067type ListOpts struct {
68 Name string `q:"display_name"`
69 Status string `q:"status"`
70 VolumeID string `q:"volume_id"`
71}
72
Jon Perritt26780d52014-10-14 11:35:58 -050073// ToSnapshotListQuery formats a ListOpts into a query string.
74func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -050075 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060076 return q.String(), err
Jon Perritt1c2356b2014-10-13 19:56:43 -050077}
78
79// List returns Snapshots optionally limited by the conditions provided in
80// ListOpts.
81func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -050082 url := listURL(client)
83 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050084 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -050085 if err != nil {
86 return pagination.Pager{Err: err}
87 }
Jon Perritt1c2356b2014-10-13 19:56:43 -050088 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -050089 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060090 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060091 return SnapshotPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060092 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050093}
94
Jon Perritt1c2356b2014-10-13 19:56:43 -050095// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
96// the Update request.
97type UpdateMetadataOptsBuilder interface {
98 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
99}
100
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200101// UpdateMetadataOpts contain options for updating an existing Snapshot. This
102// object is passed to the snapshots.Update function. For more information
103// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500104type UpdateMetadataOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600105 Metadata map[string]interface{} `json:"metadata,omitempty"`
Jon Perritt6d5561b2014-10-01 21:42:15 -0500106}
107
Jon Perritt1c2356b2014-10-13 19:56:43 -0500108// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
109// an UpdateMetadataOpts.
110func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600111 return gophercloud.BuildRequestBody(opts, "")
Jon Perritt1c2356b2014-10-13 19:56:43 -0500112}
113
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200114// UpdateMetadata will update the Snapshot with provided information. To
115// extract the updated Snapshot from the response, call the ExtractMetadata
116// method on the UpdateMetadataResult.
Jon Perritt3860b512016-03-29 12:01:48 -0500117func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600118 b, err := opts.ToSnapshotUpdateMetadataMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -0500119 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600120 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500121 return
Jon Perritt1c2356b2014-10-13 19:56:43 -0500122 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600123 _, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100124 OkCodes: []int{200},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500125 })
jrperritt29ae6b32016-04-13 12:59:37 -0500126 return
Jon Perritt56d43b22014-09-22 20:47:11 -0500127}
Jon Perritt24c20832015-06-30 09:57:00 -0600128
129// IDFromName is a convienience function that returns a snapshot's ID given its name.
130func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600131 count := 0
132 id := ""
Jon Perritted258942016-02-28 23:38:21 -0600133 pages, err := List(client, nil).AllPages()
134 if err != nil {
135 return "", err
136 }
Jon Perritt24c20832015-06-30 09:57:00 -0600137
Jon Perritted258942016-02-28 23:38:21 -0600138 all, err := ExtractSnapshots(pages)
139 if err != nil {
140 return "", err
141 }
142
143 for _, s := range all {
144 if s.Name == name {
145 count++
146 id = s.ID
147 }
148 }
149
150 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600151 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600152 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600153 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600154 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600155 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600156 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600157 }
158}