blob: ad4e574f57586856d4d97872e6718e58ed3c2d6e [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
Jon Perritt1c2356b2014-10-13 19:56:43 -05004 "fmt"
5
Jon Perrittd1d6a742014-09-17 01:10:59 -05006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
Jon Perrittee6074f2014-04-30 18:42:32 -05008)
9
Jon Perritt1c2356b2014-10-13 19:56:43 -050010// CreateOptsBuilder allows extensions to add additional parameters to the
11// Create request.
12type CreateOptsBuilder interface {
13 ToVolumeCreateMap() (map[string]interface{}, error)
14}
15
Jon Perritt42b3a2a2014-10-02 23:06:07 -050016// CreateOpts contains options for creating a Volume. This object is passed to
17// the volumes.Create function. For more information about these parameters,
18// see the Volume object.
Jon Perritt97347a02014-09-21 13:34:48 -050019type CreateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050020 // OPTIONAL
21 Availability string
22 // OPTIONAL
23 Description string
24 // OPTIONAL
25 Metadata map[string]string
26 // OPTIONAL
27 Name string
28 // REQUIRED
29 Size int
30 // OPTIONAL
31 SnapshotID, SourceVolID, ImageID string
32 // OPTIONAL
33 VolumeType string
34}
35
36// ToVolumeCreateMap assembles a request body based on the contents of a
37// CreateOpts.
38func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
39 v := make(map[string]interface{})
40
41 if opts.Size == 0 {
42 return nil, fmt.Errorf("Required CreateOpts field 'Size' not set.")
43 }
44 v["size"] = opts.Size
45
46 if opts.Availability != "" {
47 v["availability_zone"] = opts.Availability
48 }
49 if opts.Description != "" {
50 v["display_description"] = opts.Description
51 }
52 if opts.ImageID != "" {
53 v["imageRef"] = opts.ImageID
54 }
55 if opts.Metadata != nil {
56 v["metadata"] = opts.Metadata
57 }
58 if opts.Name != "" {
59 v["display_name"] = opts.Name
60 }
61 if opts.SourceVolID != "" {
62 v["source_volid"] = opts.SourceVolID
63 }
64 if opts.SnapshotID != "" {
65 v["snapshot_id"] = opts.SnapshotID
66 }
67 if opts.VolumeType != "" {
68 v["volume_type"] = opts.VolumeType
69 }
70
71 return map[string]interface{}{"volume": v}, nil
Jon Perrittee6074f2014-04-30 18:42:32 -050072}
Jon Perritte77b9b22014-05-01 13:11:12 -050073
Jon Perritt42b3a2a2014-10-02 23:06:07 -050074// Create will create a new Volume based on the values in CreateOpts. To extract
Jon Perritt1c2356b2014-10-13 19:56:43 -050075// the Volume object from the response, call the Extract method on the
76// CreateResult.
77func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050078 var res CreateResult
Jon Perritt1c2356b2014-10-13 19:56:43 -050079
80 reqBody, err := opts.ToVolumeCreateMap()
81 if err != nil {
82 res.Err = err
83 return res
84 }
85
Ash Wilsonacb1b902015-02-12 14:29:31 -050086 _, res.Err = client.Request("POST", createURL(client), gophercloud.RequestOpts{
87 OkCodes: []int{200, 201},
88 JSONBody: &reqBody,
89 JSONResponse: &res.Body,
Jon Perritt94963ad2014-05-05 12:14:39 -050090 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050091 return res
Jon Perrittd1d6a742014-09-17 01:10:59 -050092}
93
Jon Perritt42b3a2a2014-10-02 23:06:07 -050094// Delete will delete the existing Volume with the provided ID.
Jamie Hannafordce9f9082014-10-27 11:27:12 +010095func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
96 var res DeleteResult
Jamie Hannafordc530ba12015-03-23 17:50:46 +010097 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{})
Jamie Hannafordce9f9082014-10-27 11:27:12 +010098 return res
Jon Perritt6d5561b2014-10-01 21:42:15 -050099}
Jon Perrittd1d6a742014-09-17 01:10:59 -0500100
Jon Perritt1c2356b2014-10-13 19:56:43 -0500101// Get retrieves the Volume with the provided ID. To extract the Volume object
102// from the response, call the Extract method on the GetResult.
Jon Perritt03cb46d2014-09-22 20:46:20 -0500103func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500104 var res GetResult
Ash Wilsonacb1b902015-02-12 14:29:31 -0500105 _, res.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{
106 JSONResponse: &res.Body,
Jon Perritt70dd47d2014-05-01 13:51:53 -0500107 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500108 return res
Jon Perritt70dd47d2014-05-01 13:51:53 -0500109}
110
Jon Perritt1c2356b2014-10-13 19:56:43 -0500111// ListOptsBuilder allows extensions to add additional parameters to the List
112// request.
113type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500114 ToVolumeListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500115}
116
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500117// ListOpts holds options for listing Volumes. It is passed to the volumes.List
118// function.
119type ListOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500120 // admin-only option. Set it to true to see all tenant volumes.
121 AllTenants bool `q:"all_tenants"`
122 // List only volumes that contain Metadata.
123 Metadata map[string]string `q:"metadata"`
124 // List only volumes that have Name as the display name.
125 Name string `q:"name"`
126 // List only volumes that have a status of Status.
127 Status string `q:"status"`
128}
129
Jon Perritt26780d52014-10-14 11:35:58 -0500130// ToVolumeListQuery formats a ListOpts into a query string.
131func (opts ListOpts) ToVolumeListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500132 q, err := gophercloud.BuildQueryString(opts)
133 if err != nil {
134 return "", err
135 }
136 return q.String(), nil
Jon Perritt97347a02014-09-21 13:34:48 -0500137}
138
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500139// List returns Volumes optionally limited by the conditions provided in ListOpts.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500140func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -0500141 url := listURL(client)
142 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500143 query, err := opts.ToVolumeListQuery()
Jon Perritt0871a812014-10-03 11:02:35 -0500144 if err != nil {
145 return pagination.Pager{Err: err}
146 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500147 url += query
Jon Perritt0871a812014-10-03 11:02:35 -0500148 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400149 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500150 return ListResult{pagination.SinglePageBase(r)}
151 }
Jon Perritt3d0a1852015-02-19 08:51:39 -0700152
153 return pagination.NewPager(client, url, createPage)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500154}
155
156// UpdateOptsBuilder allows extensions to add additional parameters to the
157// Update request.
158type UpdateOptsBuilder interface {
159 ToVolumeUpdateMap() (map[string]interface{}, error)
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500160}
161
162// UpdateOpts contain options for updating an existing Volume. This object is passed
163// to the volumes.Update function. For more information about the parameters, see
164// the Volume object.
165type UpdateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500166 // OPTIONAL
167 Name string
168 // OPTIONAL
169 Description string
170 // OPTIONAL
171 Metadata map[string]string
172}
173
174// ToVolumeUpdateMap assembles a request body based on the contents of an
175// UpdateOpts.
176func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
177 v := make(map[string]interface{})
178
179 if opts.Description != "" {
180 v["display_description"] = opts.Description
181 }
182 if opts.Metadata != nil {
183 v["metadata"] = opts.Metadata
184 }
185 if opts.Name != "" {
186 v["display_name"] = opts.Name
187 }
188
189 return map[string]interface{}{"volume": v}, nil
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500190}
191
192// Update will update the Volume with provided information. To extract the updated
193// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt04851d32014-10-14 02:07:13 -0500194func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500195 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500196
Jon Perritt1c2356b2014-10-13 19:56:43 -0500197 reqBody, err := opts.ToVolumeUpdateMap()
198 if err != nil {
199 res.Err = err
200 return res
201 }
202
Ash Wilsonacb1b902015-02-12 14:29:31 -0500203 _, res.Err = client.Request("PUT", updateURL(client, id), gophercloud.RequestOpts{
204 OkCodes: []int{200},
205 JSONBody: &reqBody,
206 JSONResponse: &res.Body,
Jon Perritt97347a02014-09-21 13:34:48 -0500207 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500208 return res
Jon Perritte77b9b22014-05-01 13:11:12 -0500209}