blob: fad5adf1889e61ca1c32172dfaf01b931e890ca7 [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"
5 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritt6d5561b2014-10-01 21:42:15 -05006 "github.com/rackspace/gophercloud/pagination"
7
8 "github.com/racker/perigee"
Jon Perrittdfff9972014-09-22 01:14:54 -05009)
10
Jon Perritt42b3a2a2014-10-02 23:06:07 -050011// CreateOpts contains options for creating a Snapshot. This object is passed to
12// the snapshots.Create function. For more information about these parameters,
13// see the Snapshot object.
Jon Perrittdfff9972014-09-22 01:14:54 -050014type CreateOpts struct {
Jon Perritt42b3a2a2014-10-02 23:06:07 -050015 Description string // OPTIONAL
16 Force bool // OPTIONAL
17 Metadata map[string]interface{} // OPTIONAL
18 Name string // OPTIONAL
19 VolumeID string // REQUIRED
Jon Perrittdfff9972014-09-22 01:14:54 -050020}
21
Jon Perritt42b3a2a2014-10-02 23:06:07 -050022// Create will create a new Snapshot based on the values in CreateOpts. To extract
23// the Snapshot object from the response, call the Extract method on the
24// CreateResult.
Jon Perritt6d5561b2014-10-01 21:42:15 -050025func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
Jon Perrittdfff9972014-09-22 01:14:54 -050026 type snapshot struct {
27 Description *string `json:"display_description,omitempty"`
Jon Perrittd0399572014-09-22 18:03:02 -050028 Force bool `json:"force,omitempty"`
Jon Perrittdfff9972014-09-22 01:14:54 -050029 Metadata map[string]interface{} `json:"metadata,omitempty"`
30 Name *string `json:"display_name,omitempty"`
31 VolumeID *string `json:"volume_id,omitempty"`
32 }
33
34 type request struct {
35 Snapshot snapshot `json:"snapshot"`
36 }
37
38 reqBody := request{
39 Snapshot: snapshot{},
40 }
41
42 reqBody.Snapshot.Description = utils.MaybeString(opts.Description)
Jon Perrittdfff9972014-09-22 01:14:54 -050043 reqBody.Snapshot.Name = utils.MaybeString(opts.Name)
44 reqBody.Snapshot.VolumeID = utils.MaybeString(opts.VolumeID)
45
Jon Perrittd0399572014-09-22 18:03:02 -050046 reqBody.Snapshot.Force = opts.Force
47
Jon Perritt6d5561b2014-10-01 21:42:15 -050048 var res CreateResult
49 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perrittdfff9972014-09-22 01:14:54 -050050 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd4788f92014-09-24 12:05:27 -050051 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050052 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050053 Results: &res.Resp,
Jon Perrittdfff9972014-09-22 01:14:54 -050054 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050055 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050056}
Jon Perritt56d43b22014-09-22 20:47:11 -050057
Jon Perritt42b3a2a2014-10-02 23:06:07 -050058// Delete will delete the existing Snapshot with the provided ID.
Jon Perritt57ba7632014-10-02 20:32:22 -050059func Delete(client *gophercloud.ServiceClient, id string) error {
60 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perrittd4788f92014-09-24 12:05:27 -050061 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -050062 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050063 })
Jon Perritt57ba7632014-10-02 20:32:22 -050064 return err
Jon Perrittd4788f92014-09-24 12:05:27 -050065}
66
Jon Perritt42b3a2a2014-10-02 23:06:07 -050067// Get retrieves the Snapshot with the provided ID. To extract the Snapshot object
68// from the response, call the Extract method on the GetResult.
Jon Perrittd7468632014-09-22 21:58:59 -050069func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050070 var res GetResult
71 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
72 Results: &res.Resp,
Jon Perritt56d43b22014-09-22 20:47:11 -050073 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050074 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050075 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050076 return res
77}
78
Jon Perritt42b3a2a2014-10-02 23:06:07 -050079// ListOpts hold options for listing Snapshots. It is passed to the
80// snapshots.List function.
Jon Perritt6d5561b2014-10-01 21:42:15 -050081type ListOpts struct {
82 Name string `q:"display_name"`
83 Status string `q:"status"`
84 VolumeID string `q:"volume_id"`
85}
86
Jon Perritt42b3a2a2014-10-02 23:06:07 -050087// List returns Snapshots optionally limited by the conditions provided in ListOpts.
Jon Perritt6d5561b2014-10-01 21:42:15 -050088func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
89 url := listURL(client)
90 if opts != nil {
91 query, err := gophercloud.BuildQueryString(opts)
92 if err != nil {
93 return pagination.Pager{Err: err}
94 }
95 url += query.String()
96 }
97
98 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
99 return ListResult{pagination.SinglePageBase(r)}
100 }
101 return pagination.NewPager(client, url, createPage)
102}
103
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500104// UpdateOpts contain options for updating an existing Snapshot. This object is
105// passed to the snapshots.Update function. For more information about the
106// parameters, see the Snapshot object.
Jon Perritte357e3d2014-10-03 01:53:57 -0500107type UpdateMetadataOpts struct {
108 Metadata map[string]interface{}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500109}
110
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500111// Update will update the Snapshot with provided information. To extract the updated
Jon Perritte357e3d2014-10-03 01:53:57 -0500112// Snapshot from the response, call the ExtractMetadata method on the UpdateResult.
113func 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}