blob: c2e2b92567b8bba3a2113a33a957791979d80c59 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
Gorka Eguileor0ea58af2015-06-17 18:32:42 +020017import six
Matthew Treinish89128142015-04-23 10:44:30 -040018from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090019
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080020from tempest.lib.common import rest_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050021from tempest.lib import exceptions as lib_exc
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053023
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080024class BaseVolumesClient(rest_client.RestClient):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000025 """Base client class to send CRUD Volume API requests"""
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053026
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000027 create_resp = 200
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053028
Ken'ichi Ohmichi234da802015-02-13 04:48:06 +000029 def __init__(self, auth_provider, service, region,
30 default_volume_size=1, **kwargs):
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000031 super(BaseVolumesClient, self).__init__(
Ken'ichi Ohmichi234da802015-02-13 04:48:06 +000032 auth_provider, service, region, **kwargs)
33 self.default_volume_size = default_volume_size
34
anju tiwari789449a2013-08-29 16:56:17 +053035 def get_attachment_from_volume(self, volume):
36 """Return the element 'attachment' from input volumes."""
37 return volume['attachments'][0]
38
Gorka Eguileor0ea58af2015-06-17 18:32:42 +020039 def _prepare_params(self, params):
40 """Prepares params for use in get or _ext_get methods.
41
42 If params is a string it will be left as it is, but if it's not it will
43 be urlencoded.
44 """
45 if isinstance(params, six.string_types):
46 return params
47 return urllib.urlencode(params)
48
John Warren6177c9e2015-08-19 20:00:17 +000049 def list_volumes(self, detail=False, params=None):
Gorka Eguileor0ea58af2015-06-17 18:32:42 +020050 """List all the volumes created.
51
52 Params can be a string (must be urlencoded) or a dictionary.
Gorka Eguileor0ea58af2015-06-17 18:32:42 +020053 """
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070054 url = 'volumes'
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000055 if detail:
56 url += '/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050057 if params:
Gorka Eguileor0ea58af2015-06-17 18:32:42 +020058 url += '?%s' % self._prepare_params(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053059
John Warren6177c9e2015-08-19 20:00:17 +000060 resp, body = self.get(url)
61 body = json.loads(body)
62 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080063 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053064
bkopilov1f161e52016-04-18 13:27:48 +030065 def show_pools(self, detail=False):
lkuchlanb6baff82016-06-01 11:55:47 +030066 """List all the volumes pools (hosts).
67
68 Output params: see http://developer.openstack.org/
69 api-ref-blockstorage-v2.html#listPools
70 """
bkopilov1f161e52016-04-18 13:27:48 +030071 url = 'scheduler-stats/get_pools'
72 if detail:
73 url += '?detail=True'
74
75 resp, body = self.get(url)
76 body = json.loads(body)
77 self.expected_success(200, resp.status)
78 return rest_client.ResponseBody(resp, body)
79
lkuchlanb6baff82016-06-01 11:55:47 +030080 def show_backend_capabilities(self, host):
81 """Shows capabilities for a storage back end.
82
83 Output params: see http://developer.openstack.org/
84 api-ref-blockstorage-v2.html
85 #showBackendCapabilities
86 """
87 url = 'capabilities/%s' % host
88 resp, body = self.get(url)
89 body = json.loads(body)
90 self.expected_success(200, resp.status)
91 return rest_client.ResponseBody(resp, body)
92
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000093 def show_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050094 """Returns the details of a single volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070095 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010096 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053097 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000098 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080099 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +0530100
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900101 def create_volume(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000102 """Creates a new Volume.
103
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900104 Available params: see http://developer.openstack.org/
105 api-ref-blockstorage-v2.html#createVolume
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530106 """
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900107 if 'size' not in kwargs:
108 kwargs['size'] = self.default_volume_size
109 post_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200110 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530111 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000112 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800113 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530114
QingXin Meng611768a2013-09-18 00:51:33 -0700115 def update_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800116 """Updates the Specified Volume.
117
118 Available params: see http://developer.openstack.org/
119 api-ref-blockstorage-v2.html#updateVolume
120 """
QingXin Meng611768a2013-09-18 00:51:33 -0700121 put_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200122 resp, body = self.put('volumes/%s' % volume_id, put_body)
QingXin Meng611768a2013-09-18 00:51:33 -0700123 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000124 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800125 return rest_client.ResponseBody(resp, body)
QingXin Meng611768a2013-09-18 00:51:33 -0700126
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +0530127 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500128 """Deletes the Specified Volume."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000129 resp, body = self.delete("volumes/%s" % str(volume_id))
130 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800131 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530132
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900133 def upload_volume(self, volume_id, **kwargs):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200134 """Uploads a volume in Glance."""
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900135 post_body = json.dumps({'os-volume_upload_image': kwargs})
Giulio Fidente884e9da2013-06-21 17:25:42 +0200136 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200137 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200138 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000139 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800140 return rest_client.ResponseBody(resp, body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200141
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900142 def attach_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800143 """Attaches a volume to a given instance on a given mountpoint.
144
145 Available params: see http://developer.openstack.org/
146 api-ref-blockstorage-v2.html#attachVolume
147 """
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900148 post_body = json.dumps({'os-attach': kwargs})
Rohit Karajgia42fe442012-09-21 03:08:33 -0700149 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200150 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000151 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800152 return rest_client.ResponseBody(resp, body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700153
Ghanshyam58a9e872015-12-18 10:46:07 +0900154 def set_bootable_volume(self, volume_id, **kwargs):
bkopilov8a657ae2015-05-11 11:45:23 +0300155 """set a bootable flag for a volume - true or false."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900156 post_body = json.dumps({'os-set_bootable': kwargs})
bkopilov8a657ae2015-05-11 11:45:23 +0300157 url = 'volumes/%s/action' % (volume_id)
158 resp, body = self.post(url, post_body)
159 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800160 return rest_client.ResponseBody(resp, body)
bkopilov8a657ae2015-05-11 11:45:23 +0300161
Rohit Karajgia42fe442012-09-21 03:08:33 -0700162 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500163 """Detaches a volume from an instance."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900164 post_body = json.dumps({'os-detach': {}})
Rohit Karajgia42fe442012-09-21 03:08:33 -0700165 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200166 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000167 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800168 return rest_client.ResponseBody(resp, body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700169
zhangyanzi6b632432013-10-24 19:08:50 +0800170 def reserve_volume(self, volume_id):
171 """Reserves a volume."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900172 post_body = json.dumps({'os-reserve': {}})
zhangyanzi6b632432013-10-24 19:08:50 +0800173 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200174 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000175 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800176 return rest_client.ResponseBody(resp, body)
zhangyanzi6b632432013-10-24 19:08:50 +0800177
178 def unreserve_volume(self, volume_id):
179 """Restore a reserved volume ."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900180 post_body = json.dumps({'os-unreserve': {}})
zhangyanzi6b632432013-10-24 19:08:50 +0800181 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200182 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000183 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800184 return rest_client.ResponseBody(resp, body)
zhangyanzi6b632432013-10-24 19:08:50 +0800185
David Kranz6aceb4a2012-06-05 14:05:45 -0400186 def is_resource_deleted(self, id):
187 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000188 self.show_volume(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900189 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -0400190 return True
191 return False
wanghao5b981752013-10-22 11:41:41 +0800192
Matt Riedemannd2b96512014-10-13 10:18:16 -0700193 @property
194 def resource_type(self):
195 """Returns the primary type of resource this client works with."""
196 return 'volume'
197
Ghanshyam58a9e872015-12-18 10:46:07 +0900198 def extend_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800199 """Extend a volume.
200
201 Available params: see http://developer.openstack.org/
202 api-ref-blockstorage-v2.html#extendVolume
203 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900204 post_body = json.dumps({'os-extend': kwargs})
wanghao5b981752013-10-22 11:41:41 +0800205 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200206 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000207 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800208 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800209
Ghanshyam58a9e872015-12-18 10:46:07 +0900210 def reset_volume_status(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800211 """Reset the Specified Volume's Status.
212
213 Available params: see http://developer.openstack.org/
214 api-ref-blockstorage-v2.html#resetVolume
215 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900216 post_body = json.dumps({'os-reset_status': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200217 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000218 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800219 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800220
221 def volume_begin_detaching(self, volume_id):
222 """Volume Begin Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000223 # ref cinder/api/contrib/volume_actions.py#L158
wanghaoaa1f2f92013-10-10 11:30:37 +0800224 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200225 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000226 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800227 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800228
229 def volume_roll_detaching(self, volume_id):
230 """Volume Roll Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000231 # cinder/api/contrib/volume_actions.py#L170
wanghaoaa1f2f92013-10-10 11:30:37 +0800232 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200233 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000234 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800235 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800236
Ghanshyam58a9e872015-12-18 10:46:07 +0900237 def create_volume_transfer(self, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800238 """Create a volume transfer.
239
240 Available params: see http://developer.openstack.org/
241 api-ref-blockstorage-v2.html#createVolumeTransfer
242 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900243 post_body = json.dumps({'transfer': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200244 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800245 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000246 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800247 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800248
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000249 def show_volume_transfer(self, transfer_id):
wingwjcbd82dc2013-10-22 16:38:39 +0800250 """Returns the details of a volume transfer."""
251 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200252 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800253 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000254 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800255 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800256
Ghanshyam58a9e872015-12-18 10:46:07 +0900257 def list_volume_transfers(self, **params):
guo xiand8bc1cd2016-06-23 12:35:43 +0800258 """List all the volume transfers created.
259
260 Available params: see http://developer.openstack.org/
261 api-ref-blockstorage-v2.html#listVolumeTransfer
262 """
wingwjcbd82dc2013-10-22 16:38:39 +0800263 url = 'os-volume-transfer'
264 if params:
265 url += '?%s' % urllib.urlencode(params)
266 resp, body = self.get(url)
267 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000268 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800269 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800270
271 def delete_volume_transfer(self, transfer_id):
272 """Delete a volume transfer."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000273 resp, body = self.delete("os-volume-transfer/%s" % str(transfer_id))
274 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800275 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800276
Ghanshyam58a9e872015-12-18 10:46:07 +0900277 def accept_volume_transfer(self, transfer_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800278 """Accept a volume transfer.
279
280 Available params: see http://developer.openstack.org/
281 api-ref-blockstorage-v2.html#acceptVolumeTransfer
282 """
wingwjcbd82dc2013-10-22 16:38:39 +0800283 url = 'os-volume-transfer/%s/accept' % transfer_id
Ghanshyam58a9e872015-12-18 10:46:07 +0900284 post_body = json.dumps({'accept': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200285 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800286 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000287 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800288 return rest_client.ResponseBody(resp, body)
zhangyanziaa180072013-11-21 12:31:26 +0800289
Ghanshyam58a9e872015-12-18 10:46:07 +0900290 def update_volume_readonly(self, volume_id, **kwargs):
zhangyanziaa180072013-11-21 12:31:26 +0800291 """Update the Specified Volume readonly."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900292 post_body = json.dumps({'os-update_readonly_flag': kwargs})
zhangyanziaa180072013-11-21 12:31:26 +0800293 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200294 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000295 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800296 return rest_client.ResponseBody(resp, body)
wanghao9d3d6cb2013-11-12 15:10:10 +0800297
298 def force_delete_volume(self, volume_id):
299 """Force Delete Volume."""
300 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200301 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000302 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800303 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800304
305 def create_volume_metadata(self, volume_id, metadata):
306 """Create metadata for the volume."""
307 put_body = json.dumps({'metadata': metadata})
308 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200309 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800310 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000311 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800312 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800313
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000314 def show_volume_metadata(self, volume_id):
huangtianhua0ff41682013-12-16 14:49:31 +0800315 """Get metadata of the volume."""
316 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200317 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800318 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000319 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800320 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800321
322 def update_volume_metadata(self, volume_id, metadata):
323 """Update metadata for the volume."""
324 put_body = json.dumps({'metadata': metadata})
325 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200326 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800327 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000328 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800329 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800330
331 def update_volume_metadata_item(self, volume_id, id, meta_item):
332 """Update metadata item for the volume."""
333 put_body = json.dumps({'meta': meta_item})
334 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200335 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800336 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000337 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800338 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800339
340 def delete_volume_metadata_item(self, volume_id, id):
341 """Delete metadata item for the volume."""
342 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200343 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000344 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800345 return rest_client.ResponseBody(resp, body)
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +0800346
lkuchlan119b2f22016-06-20 20:59:41 +0300347 def update_volume_image_metadata(self, volume_id, **kwargs):
lkuchlan6a61bd92016-06-23 22:46:08 +0300348 """Update image metadata for the volume.
349
350 Available params: see http://developer.openstack.org/
351 api-ref-blockstorage-v2.html
352 #setVolumeimagemetadata
353 """
lkuchlan119b2f22016-06-20 20:59:41 +0300354 post_body = json.dumps({'os-set_image_metadata': {'metadata': kwargs}})
355 url = "volumes/%s/action" % (volume_id)
356 resp, body = self.post(url, post_body)
357 body = json.loads(body)
358 self.expected_success(200, resp.status)
359 return rest_client.ResponseBody(resp, body)
360
361 def delete_volume_image_metadata(self, volume_id, key_name):
362 """Delete image metadata item for the volume."""
363 post_body = json.dumps({'os-unset_image_metadata': {'key': key_name}})
364 url = "volumes/%s/action" % (volume_id)
365 resp, body = self.post(url, post_body)
366 self.expected_success(200, resp.status)
367 return rest_client.ResponseBody(resp, body)
368
Ghanshyam58a9e872015-12-18 10:46:07 +0900369 def retype_volume(self, volume_id, **kwargs):
nayna-patel78f743e2015-01-09 10:52:51 +0000370 """Updates volume with new volume type."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900371 post_body = json.dumps({'os-retype': kwargs})
nayna-patel78f743e2015-01-09 10:52:51 +0000372 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
373 self.expected_success(202, resp.status)