blob: e8583cb219e0e945706ec5d5ee6e5f90618cc2a1 [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 Perritt1c2356b2014-10-13 19:56:43 -050034func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 var r CreateResult
36 b, err := opts.ToSnapshotCreateMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -050037 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060038 r.Err = err
39 return r
Jon Perritt1c2356b2014-10-13 19:56:43 -050040 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060041 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010042 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050043 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060044 return r
Jon Perrittdfff9972014-09-22 01:14:54 -050045}
Jon Perritt56d43b22014-09-22 20:47:11 -050046
Jon Perritt42b3a2a2014-10-02 23:06:07 -050047// Delete will delete the existing Snapshot with the provided ID.
Jamie Hannaford38509592014-10-27 11:25:15 +010048func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 var r DeleteResult
50 _, r.Err = client.Delete(deleteURL(client, id), nil)
51 return r
Jon Perrittd4788f92014-09-24 12:05:27 -050052}
53
Jon Perritt1c2356b2014-10-13 19:56:43 -050054// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
55// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050056func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060057 var r GetResult
58 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
59 return r
Jon Perritt6d5561b2014-10-01 21:42:15 -050060}
61
Jon Perritt1c2356b2014-10-13 19:56:43 -050062// ListOptsBuilder allows extensions to add additional parameters to the List
63// request.
64type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050065 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050066}
67
Jon Perritt42b3a2a2014-10-02 23:06:07 -050068// ListOpts hold options for listing Snapshots. It is passed to the
69// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050070type ListOpts struct {
71 Name string `q:"display_name"`
72 Status string `q:"status"`
73 VolumeID string `q:"volume_id"`
74}
75
Jon Perritt26780d52014-10-14 11:35:58 -050076// ToSnapshotListQuery formats a ListOpts into a query string.
77func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -050078 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060079 return q.String(), err
Jon Perritt1c2356b2014-10-13 19:56:43 -050080}
81
82// List returns Snapshots optionally limited by the conditions provided in
83// ListOpts.
84func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -050085 url := listURL(client)
86 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -050087 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -050088 if err != nil {
89 return pagination.Pager{Err: err}
90 }
Jon Perritt1c2356b2014-10-13 19:56:43 -050091 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -050092 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060093 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060094 return SnapshotPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -060095 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050096}
97
Jon Perritt1c2356b2014-10-13 19:56:43 -050098// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
99// the Update request.
100type UpdateMetadataOptsBuilder interface {
101 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
102}
103
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200104// UpdateMetadataOpts contain options for updating an existing Snapshot. This
105// object is passed to the snapshots.Update function. For more information
106// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500107type UpdateMetadataOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600108 Metadata map[string]interface{} `json:"metadata,omitempty"`
Jon Perritt6d5561b2014-10-01 21:42:15 -0500109}
110
Jon Perritt1c2356b2014-10-13 19:56:43 -0500111// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
112// an UpdateMetadataOpts.
113func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600114 return gophercloud.BuildRequestBody(opts, "")
Jon Perritt1c2356b2014-10-13 19:56:43 -0500115}
116
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200117// UpdateMetadata will update the Snapshot with provided information. To
118// extract the updated Snapshot from the response, call the ExtractMetadata
119// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500120func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600121 var r UpdateMetadataResult
122 b, err := opts.ToSnapshotUpdateMetadataMap()
Jon Perritt1c2356b2014-10-13 19:56:43 -0500123 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600124 r.Err = err
125 return r
Jon Perritt1c2356b2014-10-13 19:56:43 -0500126 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600127 _, r.Err = client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100128 OkCodes: []int{200},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500129 })
Jon Perrittdb0ae142016-03-13 00:33:41 -0600130 return r
Jon Perritt56d43b22014-09-22 20:47:11 -0500131}
Jon Perritt24c20832015-06-30 09:57:00 -0600132
133// IDFromName is a convienience function that returns a snapshot's ID given its name.
134func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600135 count := 0
136 id := ""
Jon Perritted258942016-02-28 23:38:21 -0600137 pages, err := List(client, nil).AllPages()
138 if err != nil {
139 return "", err
140 }
Jon Perritt24c20832015-06-30 09:57:00 -0600141
Jon Perritted258942016-02-28 23:38:21 -0600142 all, err := ExtractSnapshots(pages)
143 if err != nil {
144 return "", err
145 }
146
147 for _, s := range all {
148 if s.Name == name {
149 count++
150 id = s.ID
151 }
152 }
153
154 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600155 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600156 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600157 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600158 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600159 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600160 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "snapshot"}
Jon Perritt24c20832015-06-30 09:57:00 -0600161 }
162}