blob: f3180d761d5d4dd34c70ba808b85f2ce445a5d40 [file] [log] [blame]
Jon Perrittdfff9972014-09-22 01:14:54 -05001package snapshots
2
3import (
Jon Perrittdfff9972014-09-22 01:14:54 -05004 "github.com/rackspace/gophercloud"
Jon Perritt6d5561b2014-10-01 21:42:15 -05005 "github.com/rackspace/gophercloud/pagination"
6
7 "github.com/racker/perigee"
Jon Perrittdfff9972014-09-22 01:14:54 -05008)
9
Jon Perritt42b3a2a2014-10-02 23:06:07 -050010// CreateOpts contains options for creating a Snapshot. This object is passed to
11// the snapshots.Create function. For more information about these parameters,
12// see the Snapshot object.
Jon Perrittdfff9972014-09-22 01:14:54 -050013type CreateOpts struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014 Description string // OPTIONAL
15 Force bool // OPTIONAL
16 Metadata map[string]interface{} // OPTIONAL
17 Name string // OPTIONAL
18 VolumeID string // REQUIRED
Jon Perrittdfff9972014-09-22 01:14:54 -050019}
20
Jon Perritt42b3a2a2014-10-02 23:06:07 -050021// Create will create a new Snapshot based on the values in CreateOpts. To extract
22// the Snapshot object from the response, call the Extract method on the
23// CreateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -050024func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
Jon Perrittdfff9972014-09-22 01:14:54 -050025 type snapshot struct {
26 Description *string `json:"display_description,omitempty"`
Jon Perrittd0399572014-09-22 18:03:02 -050027 Force bool `json:"force,omitempty"`
Jon Perrittdfff9972014-09-22 01:14:54 -050028 Metadata map[string]interface{} `json:"metadata,omitempty"`
29 Name *string `json:"display_name,omitempty"`
30 VolumeID *string `json:"volume_id,omitempty"`
31 }
32
33 type request struct {
34 Snapshot snapshot `json:"snapshot"`
35 }
36
37 reqBody := request{
38 Snapshot: snapshot{},
39 }
40
Jon Perritt8d262582014-10-03 11:11:46 -050041 reqBody.Snapshot.Description = gophercloud.MaybeString(opts.Description)
42 reqBody.Snapshot.Name = gophercloud.MaybeString(opts.Name)
43 reqBody.Snapshot.VolumeID = gophercloud.MaybeString(opts.VolumeID)
Jon Perrittdfff9972014-09-22 01:14:54 -050044
Jon Perrittd0399572014-09-22 18:03:02 -050045 reqBody.Snapshot.Force = opts.Force
46
Jon Perritt6d5561b2014-10-01 21:42:15 -050047 var res CreateResult
48 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perrittdfff9972014-09-22 01:14:54 -050049 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd4788f92014-09-24 12:05:27 -050050 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050051 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050052 Results: &res.Resp,
Jon Perrittdfff9972014-09-22 01:14:54 -050053 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050054 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050055}
Jon Perritt56d43b22014-09-22 20:47:11 -050056
Jon Perritt42b3a2a2014-10-02 23:06:07 -050057// Delete will delete the existing Snapshot with the provided ID.
Jon Perritt57ba7632014-10-02 20:32:22 -050058func Delete(client *gophercloud.ServiceClient, id string) error {
59 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perrittd4788f92014-09-24 12:05:27 -050060 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -050061 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050062 })
Jon Perritt57ba7632014-10-02 20:32:22 -050063 return err
Jon Perrittd4788f92014-09-24 12:05:27 -050064}
65
Jon Perritt42b3a2a2014-10-02 23:06:07 -050066// Get retrieves the Snapshot with the provided ID. To extract the Snapshot object
67// from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050068func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050069 var res GetResult
70 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
71 Results: &res.Resp,
Jon Perritt56d43b22014-09-22 20:47:11 -050072 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050073 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050074 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050075 return res
76}
77
Jon Perritt42b3a2a2014-10-02 23:06:07 -050078// ListOpts hold options for listing Snapshots. It is passed to the
79// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050080type ListOpts struct {
81 Name string `q:"display_name"`
82 Status string `q:"status"`
83 VolumeID string `q:"volume_id"`
84}
85
Jon Perritt42b3a2a2014-10-02 23:06:07 -050086// List returns Snapshots optionally limited by the conditions provided in ListOpts.
Jon Perritt6d5561b2014-10-01 21:42:15 -050087func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
88 url := listURL(client)
89 if opts != nil {
90 query, err := gophercloud.BuildQueryString(opts)
91 if err != nil {
92 return pagination.Pager{Err: err}
93 }
94 url += query.String()
95 }
96
97 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
98 return ListResult{pagination.SinglePageBase(r)}
99 }
100 return pagination.NewPager(client, url, createPage)
101}
102
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200103// UpdateMetadataOpts contain options for updating an existing Snapshot. This
104// object is passed to the snapshots.Update function. For more information
105// about the parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500106type UpdateMetadataOpts struct {
107 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500108}
109
Jamie Hannafordd8275bb2014-10-06 16:12:23 +0200110// UpdateMetadata will update the Snapshot with provided information. To
111// extract the updated Snapshot from the response, call the ExtractMetadata
112// method on the UpdateMetadataResult.
Jon Perritte357e3d2014-10-03 01:53:57 -0500113func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts *UpdateMetadataOpts) UpdateMetadataResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500114 type request struct {
Jon Perritte357e3d2014-10-03 01:53:57 -0500115 Metadata map[string]interface{} `json:"metadata,omitempty"`
Jon Perritt6d5561b2014-10-01 21:42:15 -0500116 }
117
Jon Perritte357e3d2014-10-03 01:53:57 -0500118 reqBody := request{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500119
Jon Perritte357e3d2014-10-03 01:53:57 -0500120 reqBody.Metadata = opts.Metadata
Jon Perritt6d5561b2014-10-01 21:42:15 -0500121
Jon Perritte357e3d2014-10-03 01:53:57 -0500122 var res UpdateMetadataResult
Jon Perritt6d5561b2014-10-01 21:42:15 -0500123
Jon Perritte357e3d2014-10-03 01:53:57 -0500124 _, res.Err = perigee.Request("PUT", updateMetadataURL(client, id), perigee.Options{
Jon Perritt6d5561b2014-10-01 21:42:15 -0500125 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritte357e3d2014-10-03 01:53:57 -0500126 OkCodes: []int{200},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500127 ReqBody: &reqBody,
128 Results: &res.Resp,
129 })
130 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500131}