blob: d2f10aa6b592e487a3f705e67ee8dd3f58483d96 [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
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010070 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
71 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050072 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050073 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050074}
Jon Perritt56d43b22014-09-22 20:47:11 -050075
Jon Perritt42b3a2a2014-10-02 23:06:07 -050076// Delete will delete the existing Snapshot with the provided ID.
Jamie Hannaford38509592014-10-27 11:25:15 +010077func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
78 var res DeleteResult
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010079 _, res.Err = client.Delete(deleteURL(client, id), nil)
Jamie Hannaford38509592014-10-27 11:25:15 +010080 return res
Jon Perrittd4788f92014-09-24 12:05:27 -050081}
82
Jon Perritt1c2356b2014-10-13 19:56:43 -050083// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
84// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050085func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050086 var res GetResult
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010087 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
Jon Perritt6d5561b2014-10-01 21:42:15 -050088 return res
89}
90
Jon Perritt1c2356b2014-10-13 19:56:43 -050091// ListOptsBuilder allows extensions to add additional parameters to the List
92// request.
93type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -050094 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -050095}
96
Jon Perritt42b3a2a2014-10-02 23:06:07 -050097// ListOpts hold options for listing Snapshots. It is passed to the
98// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050099type ListOpts struct {
100 Name string `q:"display_name"`
101 Status string `q:"status"`
102 VolumeID string `q:"volume_id"`
103}
104
Jon Perritt26780d52014-10-14 11:35:58 -0500105// ToSnapshotListQuery formats a ListOpts into a query string.
106func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500107 q, err := gophercloud.BuildQueryString(opts)
108 if err != nil {
109 return "", err
110 }
111 return q.String(), nil
112}
113
114// List returns Snapshots optionally limited by the conditions provided in
115// ListOpts.
116func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500117 url := listURL(client)
118 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500119 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500120 if err != nil {
121 return pagination.Pager{Err: err}
122 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500123 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -0500124 }
125
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400126 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500127 return ListResult{pagination.SinglePageBase(r)}
128 }
129 return pagination.NewPager(client, url, createPage)
130}
131
Jon Perritt1c2356b2014-10-13 19:56:43 -0500132// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
133// the Update request.
134type UpdateMetadataOptsBuilder interface {
135 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
136}
137
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200138// UpdateMetadataOpts contain options for updating an existing Snapshot. This
139// object is passed to the snapshots.Update function. For more information
140// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500141type UpdateMetadataOpts struct {
142 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500143}
144
Jon Perritt1c2356b2014-10-13 19:56:43 -0500145// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
146// an UpdateMetadataOpts.
147func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
148 v := make(map[string]interface{})
149
150 if opts.Metadata != nil {
151 v["metadata"] = opts.Metadata
152 }
153
154 return v, nil
155}
156
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200157// UpdateMetadata will update the Snapshot with provided information. To
158// extract the updated Snapshot from the response, call the ExtractMetadata
159// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500160func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perritte357e3d2014-10-03 01:53:57 -0500161 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500162
Jon Perritt1c2356b2014-10-13 19:56:43 -0500163 reqBody, err := opts.ToSnapshotUpdateMetadataMap()
164 if err != nil {
165 res.Err = err
166 return res
167 }
168
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100169 _, res.Err = client.Put(updateMetadataURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
170 OkCodes: []int{200},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500171 })
172 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500173}