blob: 443f6960575dfbfa50b3d9ffa02e9f77af491780 [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{
Ash Wilson77857dc2014-10-22 09:09:02 -040073 MoreHeaders: client.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.
Jamie Hannaford38509592014-10-27 11:25:15 +010082func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
83 var res DeleteResult
84 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -040085 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -050086 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050087 })
Jamie Hannaford38509592014-10-27 11:25:15 +010088 return res
Jon Perrittd4788f92014-09-24 12:05:27 -050089}
90
Jon Perritt1c2356b2014-10-13 19:56:43 -050091// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
92// object from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050093func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050094 var res GetResult
95 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -040096 Results: &res.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -040097 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050098 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050099 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500100 return res
101}
102
Jon Perritt1c2356b2014-10-13 19:56:43 -0500103// ListOptsBuilder allows extensions to add additional parameters to the List
104// request.
105type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500106 ToSnapshotListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500107}
108
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500109// ListOpts hold options for listing Snapshots. It is passed to the
110// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -0500111type ListOpts struct {
112 Name string `q:"display_name"`
113 Status string `q:"status"`
114 VolumeID string `q:"volume_id"`
115}
116
Jon Perritt26780d52014-10-14 11:35:58 -0500117// ToSnapshotListQuery formats a ListOpts into a query string.
118func (opts ListOpts) ToSnapshotListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500119 q, err := gophercloud.BuildQueryString(opts)
120 if err != nil {
121 return "", err
122 }
123 return q.String(), nil
124}
125
126// List returns Snapshots optionally limited by the conditions provided in
127// ListOpts.
128func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500129 url := listURL(client)
130 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500131 query, err := opts.ToSnapshotListQuery()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500132 if err != nil {
133 return pagination.Pager{Err: err}
134 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500135 url += query
Jon Perritt6d5561b2014-10-01 21:42:15 -0500136 }
137
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400138 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500139 return ListResult{pagination.SinglePageBase(r)}
140 }
141 return pagination.NewPager(client, url, createPage)
142}
143
Jon Perritt1c2356b2014-10-13 19:56:43 -0500144// UpdateMetadataOptsBuilder allows extensions to add additional parameters to
145// the Update request.
146type UpdateMetadataOptsBuilder interface {
147 ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
148}
149
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200150// UpdateMetadataOpts contain options for updating an existing Snapshot. This
151// object is passed to the snapshots.Update function. For more information
152// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500153type UpdateMetadataOpts struct {
154 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500155}
156
Jon Perritt1c2356b2014-10-13 19:56:43 -0500157// ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
158// an UpdateMetadataOpts.
159func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
160 v := make(map[string]interface{})
161
162 if opts.Metadata != nil {
163 v["metadata"] = opts.Metadata
164 }
165
166 return v, nil
167}
168
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200169// UpdateMetadata will update the Snapshot with provided information. To
170// extract the updated Snapshot from the response, call the ExtractMetadata
171// method on the UpdateMetadataResult.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500172func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perritte357e3d2014-10-03 01:53:57 -0500173 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500174
Jon Perritt1c2356b2014-10-13 19:56:43 -0500175 reqBody, err := opts.ToSnapshotUpdateMetadataMap()
176 if err != nil {
177 res.Err = err
178 return res
179 }
180
Jon Perritte357e3d2014-10-03 01:53:57 -0500181 _, res.Err = perigee.Request("PUT", updateMetadataURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400182 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritte357e3d2014-10-03 01:53:57 -0500183 OkCodes: []int{200},
Jon Perritt04851d32014-10-14 02:07:13 -0500184 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400185 Results: &res.Body,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500186 })
187 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500188}