blob: 1b313a6080e82b1bd6de30c76c04c62cbcb3ef0a [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
Ash Wilsonacb1b902015-02-12 14:29:31 -050081 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{
82 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050083 })
Jamie Hannaford38509592014-10-27 11:25:15 +010084 return res
Jon Perrittd4788f92014-09-24 12:05:27 -050085}
86
Jon Perritt1c2356b2014-10-13 19:56:43 -050087// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
88// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050089func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050090 var res GetResult
Ash Wilsonacb1b902015-02-12 14:29:31 -050091 _, res.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{
92 OkCodes: []int{200},
93 JSONResponse: &res.Body,
Jon Perritt56d43b22014-09-22 20:47:11 -050094 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050095 return res
96}
97
Jon Perritt1c2356b2014-10-13 19:56:43 -050098// ListOptsBuilder allows extensions to add additional parameters to the List
99// request.
100type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500101 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500102}
103
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500104// ListOpts hold options for listing Snapshots. It is passed to the
105// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500106type ListOpts struct {
107 Name string `q:"display_name"`
108 Status string `q:"status"`
109 VolumeID string `q:"volume_id"`
110}
111
Jon Perritt26780d52014-10-14 11:35:58 -0500112// ToSnapshotListQuery formats a ListOpts into a query string.
113func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500114 q, err := gophercloud.BuildQueryString(opts)
115 if err != nil {
116 return "", err
117 }
118 return q.String(), nil
119}
120
121// List returns Snapshots optionally limited by the conditions provided in
122// ListOpts.
123func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500124 url := listURL(client)
125 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500126 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500127 if err != nil {
128 return pagination.Pager{Err: err}
129 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500130 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -0500131 }
132
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400133 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500134 return ListResult{pagination.SinglePageBase(r)}
135 }
136 return pagination.NewPager(client, url, createPage)
137}
138
Jon Perritt1c2356b2014-10-13 19:56:43 -0500139// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
140// the Update request.
141type UpdateMetadataOptsBuilder interface {
142 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
143}
144
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200145// UpdateMetadataOpts contain options for updating an existing Snapshot. This
146// object is passed to the snapshots.Update function. For more information
147// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500148type UpdateMetadataOpts struct {
149 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500150}
151
Jon Perritt1c2356b2014-10-13 19:56:43 -0500152// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
153// an UpdateMetadataOpts.
154func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
155 v := make(map[string]interface{})
156
157 if opts.Metadata != nil {
158 v["metadata"] = opts.Metadata
159 }
160
161 return v, nil
162}
163
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200164// UpdateMetadata will update the Snapshot with provided information. To
165// extract the updated Snapshot from the response, call the ExtractMetadata
166// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500167func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perritte357e3d2014-10-03 01:53:57 -0500168 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500169
Jon Perritt1c2356b2014-10-13 19:56:43 -0500170 reqBody, err := opts.ToSnapshotUpdateMetadataMap()
171 if err != nil {
172 res.Err = err
173 return res
174 }
175
Ash Wilsonacb1b902015-02-12 14:29:31 -0500176 _, res.Err = client.Request("PUT", updateMetadataURL(client, id), gophercloud.RequestOpts{
177 OkCodes: []int{200},
178 JSONBody: &reqBody,
179 JSONResponse: &res.Body,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500180 })
181 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500182}