blob: cb1fd517362891264ca32a86bc8d5c01ed498b8c [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
3import (
Jon Perritt1c2356b2014-10-13 19:56:43 -05004 "fmt"
5
Jon Perrittdfff9972014-09-22 01:14:54 -05006 "github.com/rackspace/gophercloud"
Jon Perritt6d5561b2014-10-01 21:42:15 -05007 "github.com/rackspace/gophercloud/pagination"
Jon Perrittdfff9972014-09-22 01:14:54 -05008)
9
Jon Perritt1c2356b2014-10-13 19:56:43 -050010// CreateOptsBuilder allows extensions to add additional parameters to the
11// Create request.
12type CreateOptsBuilder interface {
13 ToSnapshotCreateMap() (map[string]interface{}, error)
14}
15
Jon Perritt42b3a2a2014-10-02 23:06:07 -050016// CreateOpts contains options for creating a Snapshot. This object is passed to
17// the snapshots.Create function. For more information about these parameters,
18// see the Snapshot object.
Jon Perrittdfff9972014-09-22 01:14:54 -050019type CreateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050020 // OPTIONAL
21 Description string
22 // OPTIONAL
23 Force bool
24 // OPTIONAL
25 Metadata map[string]interface{}
26 // OPTIONAL
27 Name string
28 // REQUIRED
29 VolumeID string
Jon Perrittdfff9972014-09-22 01:14:54 -050030}
31
Jon Perritt1c2356b2014-10-13 19:56:43 -050032// ToSnapshotCreateMap assembles a request body based on the contents of a
33// CreateOpts.
34func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
35 s := make(map[string]interface{})
36
37 if opts.VolumeID == "" {
38 return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.")
39 }
40 s["volume_id"] = opts.VolumeID
41
42 if opts.Description != "" {
43 s["display_description"] = opts.Description
44 }
45 if opts.Force == true {
46 s["force"] = opts.Force
47 }
48 if opts.Metadata != nil {
49 s["metadata"] = opts.Metadata
50 }
51 if opts.Name != "" {
52 s["display_name"] = opts.Name
53 }
54
55 return map[string]interface{}{"snapshot": s}, nil
56}
57
58// Create will create a new Snapshot based on the values in CreateOpts. To
59// extract the Snapshot object from the response, call the Extract method on the
Jon Perritt42b3a2a2014-10-02 23:06:07 -050060// CreateResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -050061func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050062 var res CreateResult
Jon Perritt1c2356b2014-10-13 19:56:43 -050063
64 reqBody, err := opts.ToSnapshotCreateMap()
65 if err != nil {
66 res.Err = err
67 return res
68 }
69
Ash Wilsonacb1b902015-02-12 14:29:31 -050070 _, res.Err = client.Request("POST", createURL(client), gophercloud.RequestOpts{
71 OkCodes: []int{200, 201},
72 JSONBody: &reqBody,
73 JSONResponse: &res.Body,
Jon Perrittdfff9972014-09-22 01:14:54 -050074 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050075 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050076}
Jon Perritt56d43b22014-09-22 20:47:11 -050077
Jon Perritt42b3a2a2014-10-02 23:06:07 -050078// Delete will delete the existing Snapshot with the provided ID.
Jamie Hannaford38509592014-10-27 11:25:15 +010079func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
80 var res DeleteResult
Jamie Hannafordc530ba12015-03-23 17:50:46 +010081 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{})
Jamie Hannaford38509592014-10-27 11:25:15 +010082 return res
Jon Perrittd4788f92014-09-24 12:05:27 -050083}
84
Jon Perritt1c2356b2014-10-13 19:56:43 -050085// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
86// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050087func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050088 var res GetResult
Ash Wilsonacb1b902015-02-12 14:29:31 -050089 _, res.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{
Ash Wilsonacb1b902015-02-12 14:29:31 -050090 JSONResponse: &res.Body,
Jon Perritt56d43b22014-09-22 20:47:11 -050091 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050092 return res
93}
94
Jon Perritt1c2356b2014-10-13 19:56:43 -050095// ListOptsBuilder allows extensions to add additional parameters to the List
96// request.
97type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050098 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050099}
100
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500101// ListOpts hold options for listing Snapshots. It is passed to the
102// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500103type ListOpts struct {
104 Name string `q:"display_name"`
105 Status string `q:"status"`
106 VolumeID string `q:"volume_id"`
107}
108
Jon Perritt26780d52014-10-14 11:35:58 -0500109// ToSnapshotListQuery formats a ListOpts into a query string.
110func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500111 q, err := gophercloud.BuildQueryString(opts)
112 if err != nil {
113 return "", err
114 }
115 return q.String(), nil
116}
117
118// List returns Snapshots optionally limited by the conditions provided in
119// ListOpts.
120func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500121 url := listURL(client)
122 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500123 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500124 if err != nil {
125 return pagination.Pager{Err: err}
126 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500127 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -0500128 }
129
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400130 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500131 return ListResult{pagination.SinglePageBase(r)}
132 }
133 return pagination.NewPager(client, url, createPage)
134}
135
Jon Perritt1c2356b2014-10-13 19:56:43 -0500136// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
137// the Update request.
138type UpdateMetadataOptsBuilder interface {
139 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
140}
141
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200142// UpdateMetadataOpts contain options for updating an existing Snapshot. This
143// object is passed to the snapshots.Update function. For more information
144// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500145type UpdateMetadataOpts struct {
146 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500147}
148
Jon Perritt1c2356b2014-10-13 19:56:43 -0500149// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
150// an UpdateMetadataOpts.
151func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
152 v := make(map[string]interface{})
153
154 if opts.Metadata != nil {
155 v["metadata"] = opts.Metadata
156 }
157
158 return v, nil
159}
160
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200161// UpdateMetadata will update the Snapshot with provided information. To
162// extract the updated Snapshot from the response, call the ExtractMetadata
163// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500164func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perritte357e3d2014-10-03 01:53:57 -0500165 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500166
Jon Perritt1c2356b2014-10-13 19:56:43 -0500167 reqBody, err := opts.ToSnapshotUpdateMetadataMap()
168 if err != nil {
169 res.Err = err
170 return res
171 }
172
Ash Wilsonacb1b902015-02-12 14:29:31 -0500173 _, res.Err = client.Request("PUT", updateMetadataURL(client, id), gophercloud.RequestOpts{
174 OkCodes: []int{200},
175 JSONBody: &reqBody,
176 JSONResponse: &res.Body,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500177 })
178 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500179}