blob: 52899cea5c26cc4040177c2c14b7259be7d779e7 [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):
66 # List all the volumes pools (hosts)
67 url = 'scheduler-stats/get_pools'
68 if detail:
69 url += '?detail=True'
70
71 resp, body = self.get(url)
72 body = json.loads(body)
73 self.expected_success(200, resp.status)
74 return rest_client.ResponseBody(resp, body)
75
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +000076 def show_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050077 """Returns the details of a single volume."""
guo yunxian6cdf0562016-08-17 16:21:52 +080078 url = "volumes/%s" % volume_id
Attila Fazekasb8aa7592013-01-26 01:25:45 +010079 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053080 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000081 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080082 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053083
Ghanshyam8fc0ed22015-12-18 10:25:14 +090084 def create_volume(self, **kwargs):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000085 """Creates a new Volume.
86
Ghanshyam8fc0ed22015-12-18 10:25:14 +090087 Available params: see http://developer.openstack.org/
88 api-ref-blockstorage-v2.html#createVolume
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053089 """
Ghanshyam8fc0ed22015-12-18 10:25:14 +090090 if 'size' not in kwargs:
91 kwargs['size'] = self.default_volume_size
92 post_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020093 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053094 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000095 self.expected_success(self.create_resp, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -080096 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053097
QingXin Meng611768a2013-09-18 00:51:33 -070098 def update_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +080099 """Updates the Specified Volume.
100
101 Available params: see http://developer.openstack.org/
102 api-ref-blockstorage-v2.html#updateVolume
103 """
QingXin Meng611768a2013-09-18 00:51:33 -0700104 put_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200105 resp, body = self.put('volumes/%s' % volume_id, put_body)
QingXin Meng611768a2013-09-18 00:51:33 -0700106 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000107 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800108 return rest_client.ResponseBody(resp, body)
QingXin Meng611768a2013-09-18 00:51:33 -0700109
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +0530110 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500111 """Deletes the Specified Volume."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800112 resp, body = self.delete("volumes/%s" % volume_id)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000113 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800114 return rest_client.ResponseBody(resp, body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530115
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900116 def upload_volume(self, volume_id, **kwargs):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200117 """Uploads a volume in Glance."""
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900118 post_body = json.dumps({'os-volume_upload_image': kwargs})
Giulio Fidente884e9da2013-06-21 17:25:42 +0200119 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200120 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200121 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000122 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800123 return rest_client.ResponseBody(resp, body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200124
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900125 def attach_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800126 """Attaches a volume to a given instance on a given mountpoint.
127
128 Available params: see http://developer.openstack.org/
129 api-ref-blockstorage-v2.html#attachVolume
130 """
Ghanshyam8fc0ed22015-12-18 10:25:14 +0900131 post_body = json.dumps({'os-attach': kwargs})
Rohit Karajgia42fe442012-09-21 03:08:33 -0700132 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200133 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000134 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800135 return rest_client.ResponseBody(resp, body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700136
Ghanshyam58a9e872015-12-18 10:46:07 +0900137 def set_bootable_volume(self, volume_id, **kwargs):
bkopilov8a657ae2015-05-11 11:45:23 +0300138 """set a bootable flag for a volume - true or false."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900139 post_body = json.dumps({'os-set_bootable': kwargs})
bkopilov8a657ae2015-05-11 11:45:23 +0300140 url = 'volumes/%s/action' % (volume_id)
141 resp, body = self.post(url, post_body)
142 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800143 return rest_client.ResponseBody(resp, body)
bkopilov8a657ae2015-05-11 11:45:23 +0300144
Rohit Karajgia42fe442012-09-21 03:08:33 -0700145 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500146 """Detaches a volume from an instance."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900147 post_body = json.dumps({'os-detach': {}})
Rohit Karajgia42fe442012-09-21 03:08:33 -0700148 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200149 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000150 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800151 return rest_client.ResponseBody(resp, body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700152
zhangyanzi6b632432013-10-24 19:08:50 +0800153 def reserve_volume(self, volume_id):
154 """Reserves a volume."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900155 post_body = json.dumps({'os-reserve': {}})
zhangyanzi6b632432013-10-24 19:08:50 +0800156 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200157 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000158 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800159 return rest_client.ResponseBody(resp, body)
zhangyanzi6b632432013-10-24 19:08:50 +0800160
161 def unreserve_volume(self, volume_id):
162 """Restore a reserved volume ."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900163 post_body = json.dumps({'os-unreserve': {}})
zhangyanzi6b632432013-10-24 19:08:50 +0800164 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200165 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000166 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800167 return rest_client.ResponseBody(resp, body)
zhangyanzi6b632432013-10-24 19:08:50 +0800168
David Kranz6aceb4a2012-06-05 14:05:45 -0400169 def is_resource_deleted(self, id):
170 try:
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000171 self.show_volume(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900172 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -0400173 return True
174 return False
wanghao5b981752013-10-22 11:41:41 +0800175
Matt Riedemannd2b96512014-10-13 10:18:16 -0700176 @property
177 def resource_type(self):
178 """Returns the primary type of resource this client works with."""
179 return 'volume'
180
Ghanshyam58a9e872015-12-18 10:46:07 +0900181 def extend_volume(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800182 """Extend a volume.
183
184 Available params: see http://developer.openstack.org/
185 api-ref-blockstorage-v2.html#extendVolume
186 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900187 post_body = json.dumps({'os-extend': kwargs})
wanghao5b981752013-10-22 11:41:41 +0800188 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200189 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000190 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800191 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800192
Ghanshyam58a9e872015-12-18 10:46:07 +0900193 def reset_volume_status(self, volume_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800194 """Reset the Specified Volume's Status.
195
196 Available params: see http://developer.openstack.org/
197 api-ref-blockstorage-v2.html#resetVolume
198 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900199 post_body = json.dumps({'os-reset_status': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200200 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000201 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800202 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800203
204 def volume_begin_detaching(self, volume_id):
205 """Volume Begin Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000206 # ref cinder/api/contrib/volume_actions.py#L158
wanghaoaa1f2f92013-10-10 11:30:37 +0800207 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200208 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000209 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800210 return rest_client.ResponseBody(resp, body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800211
212 def volume_roll_detaching(self, volume_id):
213 """Volume Roll Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000214 # cinder/api/contrib/volume_actions.py#L170
wanghaoaa1f2f92013-10-10 11:30:37 +0800215 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200216 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000217 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800218 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800219
Ghanshyam58a9e872015-12-18 10:46:07 +0900220 def create_volume_transfer(self, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800221 """Create a volume transfer.
222
223 Available params: see http://developer.openstack.org/
224 api-ref-blockstorage-v2.html#createVolumeTransfer
225 """
Ghanshyam58a9e872015-12-18 10:46:07 +0900226 post_body = json.dumps({'transfer': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200227 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800228 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000229 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800230 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800231
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000232 def show_volume_transfer(self, transfer_id):
wingwjcbd82dc2013-10-22 16:38:39 +0800233 """Returns the details of a volume transfer."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800234 url = "os-volume-transfer/%s" % transfer_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200235 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800236 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000237 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800238 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800239
Ghanshyam58a9e872015-12-18 10:46:07 +0900240 def list_volume_transfers(self, **params):
guo xiand8bc1cd2016-06-23 12:35:43 +0800241 """List all the volume transfers created.
242
243 Available params: see http://developer.openstack.org/
244 api-ref-blockstorage-v2.html#listVolumeTransfer
245 """
wingwjcbd82dc2013-10-22 16:38:39 +0800246 url = 'os-volume-transfer'
247 if params:
248 url += '?%s' % urllib.urlencode(params)
249 resp, body = self.get(url)
250 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000251 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800252 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800253
254 def delete_volume_transfer(self, transfer_id):
255 """Delete a volume transfer."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800256 resp, body = self.delete("os-volume-transfer/%s" % transfer_id)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000257 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800258 return rest_client.ResponseBody(resp, body)
wingwjcbd82dc2013-10-22 16:38:39 +0800259
Ghanshyam58a9e872015-12-18 10:46:07 +0900260 def accept_volume_transfer(self, transfer_id, **kwargs):
guo xiand8bc1cd2016-06-23 12:35:43 +0800261 """Accept a volume transfer.
262
263 Available params: see http://developer.openstack.org/
264 api-ref-blockstorage-v2.html#acceptVolumeTransfer
265 """
wingwjcbd82dc2013-10-22 16:38:39 +0800266 url = 'os-volume-transfer/%s/accept' % transfer_id
Ghanshyam58a9e872015-12-18 10:46:07 +0900267 post_body = json.dumps({'accept': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200268 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800269 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000270 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800271 return rest_client.ResponseBody(resp, body)
zhangyanziaa180072013-11-21 12:31:26 +0800272
Ghanshyam58a9e872015-12-18 10:46:07 +0900273 def update_volume_readonly(self, volume_id, **kwargs):
zhangyanziaa180072013-11-21 12:31:26 +0800274 """Update the Specified Volume readonly."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900275 post_body = json.dumps({'os-update_readonly_flag': kwargs})
zhangyanziaa180072013-11-21 12:31:26 +0800276 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200277 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000278 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800279 return rest_client.ResponseBody(resp, body)
wanghao9d3d6cb2013-11-12 15:10:10 +0800280
281 def force_delete_volume(self, volume_id):
282 """Force Delete Volume."""
283 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200284 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000285 self.expected_success(202, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800286 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800287
288 def create_volume_metadata(self, volume_id, metadata):
289 """Create metadata for the volume."""
290 put_body = json.dumps({'metadata': metadata})
guo yunxian6cdf0562016-08-17 16:21:52 +0800291 url = "volumes/%s/metadata" % volume_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200292 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800293 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000294 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800295 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800296
Ken'ichi Ohmichi35798fb2015-04-06 01:22:41 +0000297 def show_volume_metadata(self, volume_id):
huangtianhua0ff41682013-12-16 14:49:31 +0800298 """Get metadata of the volume."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800299 url = "volumes/%s/metadata" % volume_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200300 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800301 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000302 self.expected_success(200, 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 update_volume_metadata(self, volume_id, metadata):
306 """Update metadata for the volume."""
307 put_body = json.dumps({'metadata': metadata})
guo yunxian6cdf0562016-08-17 16:21:52 +0800308 url = "volumes/%s/metadata" % volume_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200309 resp, body = self.put(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
314 def update_volume_metadata_item(self, volume_id, id, meta_item):
315 """Update metadata item for the volume."""
316 put_body = json.dumps({'meta': meta_item})
guo yunxian6cdf0562016-08-17 16:21:52 +0800317 url = "volumes/%s/metadata/%s" % (volume_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200318 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800319 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000320 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800321 return rest_client.ResponseBody(resp, body)
huangtianhua0ff41682013-12-16 14:49:31 +0800322
323 def delete_volume_metadata_item(self, volume_id, id):
324 """Delete metadata item for the volume."""
guo yunxian6cdf0562016-08-17 16:21:52 +0800325 url = "volumes/%s/metadata/%s" % (volume_id, id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200326 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000327 self.expected_success(200, resp.status)
Ken'ichi Ohmichi90d446a2016-03-02 10:17:38 -0800328 return rest_client.ResponseBody(resp, body)
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +0800329
lkuchlan119b2f22016-06-20 20:59:41 +0300330 def update_volume_image_metadata(self, volume_id, **kwargs):
lkuchlan6a61bd92016-06-23 22:46:08 +0300331 """Update image metadata for the volume.
332
333 Available params: see http://developer.openstack.org/
334 api-ref-blockstorage-v2.html
335 #setVolumeimagemetadata
336 """
lkuchlan119b2f22016-06-20 20:59:41 +0300337 post_body = json.dumps({'os-set_image_metadata': {'metadata': kwargs}})
338 url = "volumes/%s/action" % (volume_id)
339 resp, body = self.post(url, post_body)
340 body = json.loads(body)
341 self.expected_success(200, resp.status)
342 return rest_client.ResponseBody(resp, body)
343
344 def delete_volume_image_metadata(self, volume_id, key_name):
345 """Delete image metadata item for the volume."""
346 post_body = json.dumps({'os-unset_image_metadata': {'key': key_name}})
347 url = "volumes/%s/action" % (volume_id)
348 resp, body = self.post(url, post_body)
349 self.expected_success(200, resp.status)
350 return rest_client.ResponseBody(resp, body)
351
Ghanshyam58a9e872015-12-18 10:46:07 +0900352 def retype_volume(self, volume_id, **kwargs):
nayna-patel78f743e2015-01-09 10:52:51 +0000353 """Updates volume with new volume type."""
Ghanshyam58a9e872015-12-18 10:46:07 +0900354 post_body = json.dumps({'os-retype': kwargs})
nayna-patel78f743e2015-01-09 10:52:51 +0000355 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
356 self.expected_success(202, resp.status)