blob: 1c6b635a5dc69824b17f732b5076a79dd7f22bcc [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001package volumes
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jon Perrittee6074f2014-04-30 18:42:32 -05006)
7
Jon Perritt1c2356b2014-10-13 19:56:43 -05008// CreateOptsBuilder allows extensions to add additional parameters to the
9// Create request.
10type CreateOptsBuilder interface {
11 ToVolumeCreateMap() (map[string]interface{}, error)
12}
13
Jon Perritt42b3a2a2014-10-02 23:06:07 -050014// CreateOpts contains options for creating a Volume. This object is passed to
15// the volumes.Create function. For more information about these parameters,
16// see the Volume object.
Jon Perritt97347a02014-09-21 13:34:48 -050017type CreateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -050018 // OPTIONAL
19 Availability string
20 // OPTIONAL
21 Description string
22 // OPTIONAL
23 Metadata map[string]string
24 // OPTIONAL
25 Name string
26 // REQUIRED
27 Size int
28 // OPTIONAL
29 SnapshotID, SourceVolID, ImageID string
30 // OPTIONAL
31 VolumeType string
32}
33
34// ToVolumeCreateMap assembles a request body based on the contents of a
35// CreateOpts.
36func (opts CreateOpts) ToVolumeCreateMap() (map[string]interface{}, error) {
37 v := make(map[string]interface{})
38
39 if opts.Size == 0 {
Jon Perritted258942016-02-28 23:38:21 -060040 err := &gophercloud.ErrMissingInput{}
41 err.Argument = "CreateOpts.Size"
42 err.Function = "volumes.ToVolumeCreateMap"
43 return nil, err
Jon Perritt1c2356b2014-10-13 19:56:43 -050044 }
45 v["size"] = opts.Size
46
47 if opts.Availability != "" {
48 v["availability_zone"] = opts.Availability
49 }
50 if opts.Description != "" {
51 v["display_description"] = opts.Description
52 }
53 if opts.ImageID != "" {
54 v["imageRef"] = opts.ImageID
55 }
56 if opts.Metadata != nil {
57 v["metadata"] = opts.Metadata
58 }
59 if opts.Name != "" {
60 v["display_name"] = opts.Name
61 }
62 if opts.SourceVolID != "" {
63 v["source_volid"] = opts.SourceVolID
64 }
65 if opts.SnapshotID != "" {
66 v["snapshot_id"] = opts.SnapshotID
67 }
68 if opts.VolumeType != "" {
69 v["volume_type"] = opts.VolumeType
70 }
71
72 return map[string]interface{}{"volume": v}, nil
Jon Perrittee6074f2014-04-30 18:42:32 -050073}
Jon Perritte77b9b22014-05-01 13:11:12 -050074
Jon Perritt42b3a2a2014-10-02 23:06:07 -050075// Create will create a new Volume based on the values in CreateOpts. To extract
Jon Perritt1c2356b2014-10-13 19:56:43 -050076// the Volume object from the response, call the Extract method on the
77// CreateResult.
78func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -050079 var res CreateResult
Jon Perritt1c2356b2014-10-13 19:56:43 -050080
81 reqBody, err := opts.ToVolumeCreateMap()
82 if err != nil {
83 res.Err = err
84 return res
85 }
86
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010087 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
88 OkCodes: []int{200, 201},
Jon Perritt94963ad2014-05-05 12:14:39 -050089 })
Jon Perritt6d5561b2014-10-01 21:42:15 -050090 return res
Jon Perrittd1d6a742014-09-17 01:10:59 -050091}
92
Jon Perritt42b3a2a2014-10-02 23:06:07 -050093// Delete will delete the existing Volume with the provided ID.
Jamie Hannafordce9f9082014-10-27 11:27:12 +010094func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
95 var res DeleteResult
Jamie Hannaford6181fdb2015-03-24 14:55:50 +010096 _, res.Err = client.Delete(deleteURL(client, id), nil)
Jamie Hannafordce9f9082014-10-27 11:27:12 +010097 return res
Jon Perritt6d5561b2014-10-01 21:42:15 -050098}
Jon Perrittd1d6a742014-09-17 01:10:59 -050099
Jon Perritt1c2356b2014-10-13 19:56:43 -0500100// Get retrieves the Volume with the provided ID. To extract the Volume object
101// from the response, call the Extract method on the GetResult.
Jon Perritt03cb46d2014-09-22 20:46:20 -0500102func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500103 var res GetResult
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100104 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500105 return res
Jon Perritt70dd47d2014-05-01 13:51:53 -0500106}
107
Jon Perritt1c2356b2014-10-13 19:56:43 -0500108// ListOptsBuilder allows extensions to add additional parameters to the List
109// request.
110type ListOptsBuilder interface {
Jon Perritt26780d52014-10-14 11:35:58 -0500111 ToVolumeListQuery() (string, error)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500112}
113
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500114// ListOpts holds options for listing Volumes. It is passed to the volumes.List
115// function.
116type ListOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500117 // admin-only option. Set it to true to see all tenant volumes.
118 AllTenants bool `q:"all_tenants"`
119 // List only volumes that contain Metadata.
120 Metadata map[string]string `q:"metadata"`
121 // List only volumes that have Name as the display name.
122 Name string `q:"name"`
123 // List only volumes that have a status of Status.
124 Status string `q:"status"`
125}
126
Jon Perritt26780d52014-10-14 11:35:58 -0500127// ToVolumeListQuery formats a ListOpts into a query string.
128func (opts ListOpts) ToVolumeListQuery() (string, error) {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500129 q, err := gophercloud.BuildQueryString(opts)
130 if err != nil {
131 return "", err
132 }
133 return q.String(), nil
Jon Perritt97347a02014-09-21 13:34:48 -0500134}
135
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500136// List returns Volumes optionally limited by the conditions provided in ListOpts.
Jon Perritt1c2356b2014-10-13 19:56:43 -0500137func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
Jon Perritt0871a812014-10-03 11:02:35 -0500138 url := listURL(client)
139 if opts != nil {
Jon Perritt26780d52014-10-14 11:35:58 -0500140 query, err := opts.ToVolumeListQuery()
Jon Perritt0871a812014-10-03 11:02:35 -0500141 if err != nil {
142 return pagination.Pager{Err: err}
143 }
Jon Perritt1c2356b2014-10-13 19:56:43 -0500144 url += query
Jon Perritt0871a812014-10-03 11:02:35 -0500145 }
Ash Wilsonb8b16f82014-10-20 10:19:49 -0400146 createPage := func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -0600147 return VolumePage{pagination.SinglePageBase(r)}
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500148 }
Jon Perritt3d0a1852015-02-19 08:51:39 -0700149
150 return pagination.NewPager(client, url, createPage)
Jon Perritt1c2356b2014-10-13 19:56:43 -0500151}
152
153// UpdateOptsBuilder allows extensions to add additional parameters to the
154// Update request.
155type UpdateOptsBuilder interface {
156 ToVolumeUpdateMap() (map[string]interface{}, error)
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500157}
158
159// UpdateOpts contain options for updating an existing Volume. This object is passed
160// to the volumes.Update function. For more information about the parameters, see
161// the Volume object.
162type UpdateOpts struct {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500163 // OPTIONAL
164 Name string
165 // OPTIONAL
166 Description string
167 // OPTIONAL
168 Metadata map[string]string
169}
170
171// ToVolumeUpdateMap assembles a request body based on the contents of an
172// UpdateOpts.
173func (opts UpdateOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
174 v := make(map[string]interface{})
175
176 if opts.Description != "" {
177 v["display_description"] = opts.Description
178 }
179 if opts.Metadata != nil {
180 v["metadata"] = opts.Metadata
181 }
182 if opts.Name != "" {
183 v["display_name"] = opts.Name
184 }
185
186 return map[string]interface{}{"volume": v}, nil
Jon Perritt42b3a2a2014-10-02 23:06:07 -0500187}
188
189// Update will update the Volume with provided information. To extract the updated
190// Volume from the response, call the Extract method on the UpdateResult.
Jon Perritt04851d32014-10-14 02:07:13 -0500191func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt6d5561b2014-10-01 21:42:15 -0500192 var res UpdateResult
Jon Perritt97347a02014-09-21 13:34:48 -0500193
Jon Perritt1c2356b2014-10-13 19:56:43 -0500194 reqBody, err := opts.ToVolumeUpdateMap()
195 if err != nil {
196 res.Err = err
197 return res
198 }
199
Jamie Hannaford6181fdb2015-03-24 14:55:50 +0100200 _, res.Err = client.Put(updateURL(client, id), reqBody, &res.Body, &gophercloud.RequestOpts{
201 OkCodes: []int{200},
Jon Perritt97347a02014-09-21 13:34:48 -0500202 })
Jon Perritt6d5561b2014-10-01 21:42:15 -0500203 return res
Jon Perritte77b9b22014-05-01 13:11:12 -0500204}
Jon Perritt24c20832015-06-30 09:57:00 -0600205
206// IDFromName is a convienience function that returns a server's ID given its name.
207func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perritted258942016-02-28 23:38:21 -0600208 count := 0
209 id := ""
Jon Perritt24c20832015-06-30 09:57:00 -0600210 if name == "" {
Jon Perritted258942016-02-28 23:38:21 -0600211 err := &gophercloud.ErrMissingInput{}
212 err.Function = "volumes.IDFromName"
213 err.Argument = "name"
214 return "", err
Jon Perritt24c20832015-06-30 09:57:00 -0600215 }
Jon Perritt24c20832015-06-30 09:57:00 -0600216
Jon Perritted258942016-02-28 23:38:21 -0600217 pages, err := List(client, nil).AllPages()
218 if err != nil {
219 return "", err
220 }
Jon Perritt24c20832015-06-30 09:57:00 -0600221
Jon Perritted258942016-02-28 23:38:21 -0600222 all, err := ExtractVolumes(pages)
223 if err != nil {
224 return "", err
225 }
226
227 for _, s := range all {
228 if s.Name == name {
229 count++
230 id = s.ID
231 }
232 }
233
234 switch count {
Jon Perritt24c20832015-06-30 09:57:00 -0600235 case 0:
Jon Perritted258942016-02-28 23:38:21 -0600236 err := &gophercloud.ErrResourceNotFound{}
237 err.Name = name
238 err.ResourceType = "volume"
239 err.Function = "volumes.IDFromName"
240 return "", err
Jon Perritt24c20832015-06-30 09:57:00 -0600241 case 1:
Jon Perritted258942016-02-28 23:38:21 -0600242 return id, nil
Jon Perritt24c20832015-06-30 09:57:00 -0600243 default:
Jon Perritted258942016-02-28 23:38:21 -0600244 err := &gophercloud.ErrMultipleResourcesFound{}
245 err.Count = count
246 err.Name = name
247 err.ResourceType = "volume"
248 err.Function = "volumes.IDFromName"
249 return "", err
Jon Perritt24c20832015-06-30 09:57:00 -0600250 }
251}