blob: 49b637a140a7b5162d4959772a4cbaf08c303c01 [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"
8
9 "github.com/racker/perigee"
Jon Perrittdfff9972014-09-22 01:14:54 -050010)
11
Jon Perritt1c2356b2014-10-13 19:56:43 -050012// CreateOptsBuilder allows extensions to add additional parameters to the
13// Create request.
14type CreateOptsBuilder interface {
15 ToSnapshotCreateMap() (map[string]interface{}, error)
16}
17
Jon Perritt42b3a2a2014-10-02 23:06:07 -050018// CreateOpts contains options for creating a Snapshot. This object is passed to
19// the snapshots.Create function. For more information about these parameters,
20// see the Snapshot object.
Jon Perrittdfff9972014-09-22 01:14:54 -050021type CreateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050022 // OPTIONAL
23 Description string
24 // OPTIONAL
25 Force bool
26 // OPTIONAL
27 Metadata map[string]interface{}
28 // OPTIONAL
29 Name string
30 // REQUIRED
31 VolumeID string
Jon Perrittdfff9972014-09-22 01:14:54 -050032}
33
Jon Perritt1c2356b2014-10-13 19:56:43 -050034// ToSnapshotCreateMap assembles a request body based on the contents of a
35// CreateOpts.
36func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
37 s := make(map[string]interface{})
38
39 if opts.VolumeID == "" {
40 return nil, fmt.Errorf("Required CreateOpts field 'VolumeID' not set.")
41 }
42 s["volume_id"] = opts.VolumeID
43
44 if opts.Description != "" {
45 s["display_description"] = opts.Description
46 }
47 if opts.Force == true {
48 s["force"] = opts.Force
49 }
50 if opts.Metadata != nil {
51 s["metadata"] = opts.Metadata
52 }
53 if opts.Name != "" {
54 s["display_name"] = opts.Name
55 }
56
57 return map[string]interface{}{"snapshot": s}, nil
58}
59
60// Create will create a new Snapshot based on the values in CreateOpts. To
61// extract the Snapshot object from the response, call the Extract method on the
Jon Perritt42b3a2a2014-10-02 23:06:07 -050062// CreateResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -050063func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050064 var res CreateResult
Jon Perritt1c2356b2014-10-13 19:56:43 -050065
66 reqBody, err := opts.ToSnapshotCreateMap()
67 if err != nil {
68 res.Err = err
69 return res
70 }
71
Jon Perritt6d5561b2014-10-01 21:42:15 -050072 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perrittdfff9972014-09-22 01:14:54 -050073 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd4788f92014-09-24 12:05:27 -050074 OkCodes: []int{200, 201},
Jon Perritt04851d32014-10-14 02:07:13 -050075 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -040076 Results: &res.Body,
Jon Perrittdfff9972014-09-22 01:14:54 -050077 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050078 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050079}
Jon Perritt56d43b22014-09-22 20:47:11 -050080
Jon Perritt42b3a2a2014-10-02 23:06:07 -050081// Delete will delete the existing Snapshot with the provided ID.
Jon Perritt57ba7632014-10-02 20:32:22 -050082func Delete(client *gophercloud.ServiceClient, id string) error {
83 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perrittd4788f92014-09-24 12:05:27 -050084 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -050085 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050086 })
Jon Perritt57ba7632014-10-02 20:32:22 -050087 return err
Jon Perrittd4788f92014-09-24 12:05:27 -050088}
89
Jon Perritt1c2356b2014-10-13 19:56:43 -050090// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
91// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050092func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050093 var res GetResult
94 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -040095 Results: &res.Body,
Jon Perritt56d43b22014-09-22 20:47:11 -050096 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050097 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050098 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050099 return res
100}
101
Jon Perritt1c2356b2014-10-13 19:56:43 -0500102// ListOptsBuilder allows extensions to add additional parameters to the List
103// request.
104type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500105 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500106}
107
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500108// ListOpts hold options for listing Snapshots. It is passed to the
109// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500110type ListOpts struct {
111 Name string `q:"display_name"`
112 Status string `q:"status"`
113 VolumeID string `q:"volume_id"`
114}
115
Jon Perritt26780d52014-10-14 11:35:58 -0500116// ToSnapshotListQuery formats a ListOpts into a query string.
117func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500118 q, err := gophercloud.BuildQueryString(opts)
119 if err != nil {
120 return "", err
121 }
122 return q.String(), nil
123}
124
125// List returns Snapshots optionally limited by the conditions provided in
126// ListOpts.
127func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500128 url := listURL(client)
129 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500130 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500131 if err != nil {
132 return pagination.Pager{Err: err}
133 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500134 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -0500135 }
136
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400137 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500138 return ListResult{pagination.SinglePageBase(r)}
139 }
140 return pagination.NewPager(client, url, createPage)
141}
142
Jon Perritt1c2356b2014-10-13 19:56:43 -0500143// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
144// the Update request.
145type UpdateMetadataOptsBuilder interface {
146 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
147}
148
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200149// UpdateMetadataOpts contain options for updating an existing Snapshot. This
150// object is passed to the snapshots.Update function. For more information
151// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500152type UpdateMetadataOpts struct {
153 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500154}
155
Jon Perritt1c2356b2014-10-13 19:56:43 -0500156// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
157// an UpdateMetadataOpts.
158func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
159 v := make(map[string]interface{})
160
161 if opts.Metadata != nil {
162 v["metadata"] = opts.Metadata
163 }
164
165 return v, nil
166}
167
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200168// UpdateMetadata will update the Snapshot with provided information. To
169// extract the updated Snapshot from the response, call the ExtractMetadata
170// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500171func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perritte357e3d2014-10-03 01:53:57 -0500172 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500173
Jon Perritt1c2356b2014-10-13 19:56:43 -0500174 reqBody, err := opts.ToSnapshotUpdateMetadataMap()
175 if err != nil {
176 res.Err = err
177 return res
178 }
179
Jon Perritte357e3d2014-10-03 01:53:57 -0500180 _, res.Err = perigee.Request("PUT", updateMetadataURL(client, id), perigee.Options{
Jon Perritt6d5561b2014-10-01 21:42:15 -0500181 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritte357e3d2014-10-03 01:53:57 -0500182 OkCodes: []int{200},
Jon Perritt04851d32014-10-14 02:07:13 -0500183 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400184 Results: &res.Body,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500185 })
186 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500187}