blob: 7dcbc465b9c9d2a5af5befa695dc20fb436c706b [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
11type CreateOpts struct {
12 Description string
13 Force bool
14 Metadata map[string]interface{}
15 Name string
16 VolumeID string
17}
18
Jon Perritt6d5561b2014-10-01 21:42:15 -050019func Create(client *gophercloud.ServiceClient, opts *CreateOpts) CreateResult {
Jon Perrittdfff9972014-09-22 01:14:54 -050020 type snapshot struct {
21 Description *string `json:"display_description,omitempty"`
Jon Perrittd0399572014-09-22 18:03:02 -050022 Force bool `json:"force,omitempty"`
Jon Perrittdfff9972014-09-22 01:14:54 -050023 Metadata map[string]interface{} `json:"metadata,omitempty"`
24 Name *string `json:"display_name,omitempty"`
25 VolumeID *string `json:"volume_id,omitempty"`
26 }
27
28 type request struct {
29 Snapshot snapshot `json:"snapshot"`
30 }
31
32 reqBody := request{
33 Snapshot: snapshot{},
34 }
35
36 reqBody.Snapshot.Description = utils.MaybeString(opts.Description)
Jon Perrittdfff9972014-09-22 01:14:54 -050037 reqBody.Snapshot.Name = utils.MaybeString(opts.Name)
38 reqBody.Snapshot.VolumeID = utils.MaybeString(opts.VolumeID)
39
Jon Perrittd0399572014-09-22 18:03:02 -050040 reqBody.Snapshot.Force = opts.Force
41
Jon Perritt6d5561b2014-10-01 21:42:15 -050042 var res CreateResult
43 _, res.Err = perigee.Request("POST", createURL(client), perigee.Options{
Jon Perrittdfff9972014-09-22 01:14:54 -050044 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perrittd4788f92014-09-24 12:05:27 -050045 OkCodes: []int{200, 201},
Jon Perrittdfff9972014-09-22 01:14:54 -050046 ReqBody: &reqBody,
Jon Perritt6d5561b2014-10-01 21:42:15 -050047 Results: &res.Resp,
Jon Perrittdfff9972014-09-22 01:14:54 -050048 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050049 return res
Jon Perrittdfff9972014-09-22 01:14:54 -050050}
Jon Perritt56d43b22014-09-22 20:47:11 -050051
Jon Perritt57ba7632014-10-02 20:32:22 -050052func Delete(client *gophercloud.ServiceClient, id string) error {
53 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perrittd4788f92014-09-24 12:05:27 -050054 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -050055 OkCodes: []int{202, 204},
Jon Perrittd4788f92014-09-24 12:05:27 -050056 })
Jon Perritt57ba7632014-10-02 20:32:22 -050057 return err
Jon Perrittd4788f92014-09-24 12:05:27 -050058}
59
Jon Perrittd7468632014-09-22 21:58:59 -050060func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050061 var res GetResult
62 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
63 Results: &res.Resp,
Jon Perritt56d43b22014-09-22 20:47:11 -050064 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050065 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050066 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050067 return res
68}
69
70type ListOpts struct {
71 Name string `q:"display_name"`
72 Status string `q:"status"`
73 VolumeID string `q:"volume_id"`
74}
75
76func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
77 url := listURL(client)
78 if opts != nil {
79 query, err := gophercloud.BuildQueryString(opts)
80 if err != nil {
81 return pagination.Pager{Err: err}
82 }
83 url += query.String()
84 }
85
86 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
87 return ListResult{pagination.SinglePageBase(r)}
88 }
89 return pagination.NewPager(client, url, createPage)
90}
91
92type UpdateOpts struct {
93 Description string
94 Name string
95}
96
97func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
98 type update struct {
99 Description *string `json:"display_description,omitempty"`
100 Name *string `json:"display_name,omitempty"`
101 }
102
103 type request struct {
104 Volume update `json:"snapshot"`
105 }
106
107 reqBody := request{
108 Volume: update{},
109 }
110
111 reqBody.Volume.Description = utils.MaybeString(opts.Description)
112 reqBody.Volume.Name = utils.MaybeString(opts.Name)
113
114 var res UpdateResult
115
116 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
117 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt57ba7632014-10-02 20:32:22 -0500118 OkCodes: []int{202, 204},
Jon Perritt6d5561b2014-10-01 21:42:15 -0500119 ReqBody: &reqBody,
120 Results: &res.Resp,
121 })
122 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500123}