blob: f823b3d7a9263138466f9a8f5f399a5fafefbdca [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 Perritt6d5561b2014-10-01 21:42:15 -050052func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
53 var res DeleteResult
54 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Jon Perrittd4788f92014-09-24 12:05:27 -050055 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050056 OkCodes: []int{204},
Jon Perrittd4788f92014-09-24 12:05:27 -050057 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050058 return res
Jon Perrittd4788f92014-09-24 12:05:27 -050059}
60
Jon Perrittd7468632014-09-22 21:58:59 -050061func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050062 var res GetResult
63 _, res.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
64 Results: &res.Resp,
Jon Perritt56d43b22014-09-22 20:47:11 -050065 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt6d5561b2014-10-01 21:42:15 -050066 OkCodes: []int{200},
Jon Perritt56d43b22014-09-22 20:47:11 -050067 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050068 return res
69}
70
71type ListOpts struct {
72 Name string `q:"display_name"`
73 Status string `q:"status"`
74 VolumeID string `q:"volume_id"`
75}
76
77func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
78 url := listURL(client)
79 if opts != nil {
80 query, err := gophercloud.BuildQueryString(opts)
81 if err != nil {
82 return pagination.Pager{Err: err}
83 }
84 url += query.String()
85 }
86
87 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
88 return ListResult{pagination.SinglePageBase(r)}
89 }
90 return pagination.NewPager(client, url, createPage)
91}
92
93type UpdateOpts struct {
94 Description string
95 Name string
96}
97
98func Update(client *gophercloud.ServiceClient, id string, opts *UpdateOpts) UpdateResult {
99 type update struct {
100 Description *string `json:"display_description,omitempty"`
101 Name *string `json:"display_name,omitempty"`
102 }
103
104 type request struct {
105 Volume update `json:"snapshot"`
106 }
107
108 reqBody := request{
109 Volume: update{},
110 }
111
112 reqBody.Volume.Description = utils.MaybeString(opts.Description)
113 reqBody.Volume.Name = utils.MaybeString(opts.Name)
114
115 var res UpdateResult
116
117 _, res.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
118 MoreHeaders: client.Provider.AuthenticatedHeaders(),
119 OkCodes: []int{200},
120 ReqBody: &reqBody,
121 Results: &res.Resp,
122 })
123 return res
Jon Perritt56d43b22014-09-22 20:47:11 -0500124}