blob: aef1e3c1ea595f879f6a702b99683ba70c665fe1 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Matthew Treinish9854d5b2012-09-20 10:22:13 -04002# 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
16import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Matthew Treinish9854d5b2012-09-20 10:22:13 -040018
19from lxml import etree
Ryan McNair9acfcf72014-01-27 21:06:48 +000020from xml.sax.saxutils import escape
Matthew Treinish9854d5b2012-09-20 10:22:13 -040021
vponomaryov960eeb42014-02-22 18:25:25 +020022from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023from tempest import config
Matthew Treinish9854d5b2012-09-20 10:22:13 -040024from tempest import exceptions
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025from tempest.services.compute.xml.common import Document
dwallecke62b9f02012-10-10 23:34:42 -050026from tempest.services.compute.xml.common import Element
27from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050028from tempest.services.compute.xml.common import xml_to_json
29from tempest.services.compute.xml.common import XMLNS_11
Matthew Treinish9854d5b2012-09-20 10:22:13 -040030
Matthew Treinish684d8992014-01-30 16:27:40 +000031CONF = config.CONF
32
Matthew Treinish9854d5b2012-09-20 10:22:13 -040033
vponomaryov960eeb42014-02-22 18:25:25 +020034class VolumesClientXML(rest_client.RestClient):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040035 """
36 Client class to send CRUD Volume API requests to a Cinder endpoint
37 """
vponomaryov960eeb42014-02-22 18:25:25 +020038 TYPE = "xml"
Matthew Treinish9854d5b2012-09-20 10:22:13 -040039
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000040 def __init__(self, auth_provider):
41 super(VolumesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000042 self.service = CONF.volume.catalog_type
43 self.build_interval = CONF.compute.build_interval
44 self.build_timeout = CONF.compute.build_timeout
Matthew Treinish9854d5b2012-09-20 10:22:13 -040045
46 def _parse_volume(self, body):
47 vol = dict((attr, body.get(attr)) for attr in body.keys())
48
49 for child in body.getchildren():
50 tag = child.tag
51 if tag.startswith("{"):
52 ns, tag = tag.split("}", 1)
53 if tag == 'metadata':
54 vol['metadata'] = dict((meta.get('key'),
Attila Fazekas786236c2013-01-31 16:06:51 +010055 meta.text) for meta in
56 child.getchildren())
Matthew Treinish9854d5b2012-09-20 10:22:13 -040057 else:
58 vol[tag] = xml_to_json(child)
Attila Fazekas786236c2013-01-31 16:06:51 +010059 return vol
Matthew Treinish9854d5b2012-09-20 10:22:13 -040060
anju tiwari789449a2013-08-29 16:56:17 +053061 def get_attachment_from_volume(self, volume):
62 """Return the element 'attachment' from input volumes."""
63 return volume['attachments']['attachment']
64
Nayna Patel5e76be12013-08-19 12:10:16 +000065 def _check_if_bootable(self, volume):
66 """
67 Check if the volume is bootable, also change the value
68 of 'bootable' from string to boolean.
69 """
John Griffithf55f69e2013-09-19 14:10:57 -060070
71 # NOTE(jdg): Version 1 of Cinder API uses lc strings
72 # We should consider being explicit in this check to
73 # avoid introducing bugs like: LP #1227837
74
75 if volume['bootable'].lower() == 'true':
Nayna Patel5e76be12013-08-19 12:10:16 +000076 volume['bootable'] = True
John Griffithf55f69e2013-09-19 14:10:57 -060077 elif volume['bootable'].lower() == 'false':
Nayna Patel5e76be12013-08-19 12:10:16 +000078 volume['bootable'] = False
79 else:
80 raise ValueError(
81 'bootable flag is supposed to be either True or False,'
82 'it is %s' % volume['bootable'])
83 return volume
84
Matthew Treinish9854d5b2012-09-20 10:22:13 -040085 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050086 """List all the volumes created."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -040087 url = 'volumes'
88
89 if params:
90 url += '?%s' % urllib.urlencode(params)
91
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020092 resp, body = self.get(url)
Matthew Treinish9854d5b2012-09-20 10:22:13 -040093 body = etree.fromstring(body)
94 volumes = []
95 if body is not None:
96 volumes += [self._parse_volume(vol) for vol in list(body)]
Nayna Patel5e76be12013-08-19 12:10:16 +000097 for v in volumes:
98 v = self._check_if_bootable(v)
Matthew Treinish9854d5b2012-09-20 10:22:13 -040099 return resp, volumes
100
101 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -0500102 """List all the details of volumes."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400103 url = 'volumes/detail'
104
105 if params:
106 url += '?%s' % urllib.urlencode(params)
107
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200108 resp, body = self.get(url)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400109 body = etree.fromstring(body)
110 volumes = []
111 if body is not None:
112 volumes += [self._parse_volume(vol) for vol in list(body)]
Nayna Patel5e76be12013-08-19 12:10:16 +0000113 for v in volumes:
114 v = self._check_if_bootable(v)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400115 return resp, volumes
116
Attila Fazekasb8aa7592013-01-26 01:25:45 +0100117 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500118 """Returns the details of a single volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400119 url = "volumes/%s" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200120 resp, body = self.get(url)
Nayna Patel5e76be12013-08-19 12:10:16 +0000121 body = self._parse_volume(etree.fromstring(body))
122 body = self._check_if_bootable(body)
123 return resp, body
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400124
Attila Fazekas786236c2013-01-31 16:06:51 +0100125 def create_volume(self, size, **kwargs):
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400126 """Creates a new Volume.
127
128 :param size: Size of volume in GB. (Required)
129 :param display_name: Optional Volume Name.
130 :param metadata: An optional dictionary of values for metadata.
Attila Fazekas786236c2013-01-31 16:06:51 +0100131 :param volume_type: Optional Name of volume_type for the volume
132 :param snapshot_id: When specified the volume is created from
133 this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +0200134 :param imageRef: When specified the volume is created from this
135 image
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400136 """
Attila Fazekasa8b5fe72013-08-01 16:59:06 +0200137 # NOTE(afazekas): it should use a volume namespace
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800138 volume = Element("volume", xmlns=XMLNS_11, size=size)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400139
Attila Fazekas786236c2013-01-31 16:06:51 +0100140 if 'metadata' in kwargs:
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400141 _metadata = Element('metadata')
142 volume.append(_metadata)
Attila Fazekas786236c2013-01-31 16:06:51 +0100143 for key, value in kwargs['metadata'].items():
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400144 meta = Element('meta')
145 meta.add_attr('key', key)
146 meta.append(Text(value))
147 _metadata.append(meta)
Attila Fazekas786236c2013-01-31 16:06:51 +0100148 attr_to_add = kwargs.copy()
149 del attr_to_add['metadata']
150 else:
151 attr_to_add = kwargs
152
153 for key, value in attr_to_add.items():
154 volume.add_attr(key, value)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400155
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200156 resp, body = self.post('volumes', str(Document(volume)))
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400157 body = xml_to_json(etree.fromstring(body))
158 return resp, body
159
QingXin Meng611768a2013-09-18 00:51:33 -0700160 def update_volume(self, volume_id, **kwargs):
161 """Updates the Specified Volume."""
162 put_body = Element("volume", xmlns=XMLNS_11, **kwargs)
163
164 resp, body = self.put('volumes/%s' % volume_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200165 str(Document(put_body)))
QingXin Meng611768a2013-09-18 00:51:33 -0700166 body = xml_to_json(etree.fromstring(body))
167 return resp, body
168
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400169 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500170 """Deletes the Specified Volume."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400171 return self.delete("volumes/%s" % str(volume_id))
172
173 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500174 """Waits for a Volume to reach a given status."""
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400175 resp, body = self.get_volume(volume_id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400176 volume_status = body['status']
177 start = int(time.time())
178
179 while volume_status != status:
180 time.sleep(self.build_interval)
181 resp, body = self.get_volume(volume_id)
182 volume_status = body['status']
183 if volume_status == 'error':
184 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
185
186 if int(time.time()) - start >= self.build_timeout:
187 message = 'Volume %s failed to reach %s status within '\
Attila Fazekas786236c2013-01-31 16:06:51 +0100188 'the required time (%s s).' % (volume_id,
189 status,
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400190 self.build_timeout)
191 raise exceptions.TimeoutException(message)
192
193 def is_resource_deleted(self, id):
194 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100195 self.get_volume(id)
Matthew Treinish9854d5b2012-09-20 10:22:13 -0400196 except exceptions.NotFound:
197 return True
198 return False
anju tiwari789449a2013-08-29 16:56:17 +0530199
200 def attach_volume(self, volume_id, instance_uuid, mountpoint):
201 """Attaches a volume to a given instance on a given mountpoint."""
202 post_body = Element("os-attach",
203 instance_uuid=instance_uuid,
204 mountpoint=mountpoint
205 )
206 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200207 resp, body = self.post(url, str(Document(post_body)))
anju tiwari789449a2013-08-29 16:56:17 +0530208 if body:
209 body = xml_to_json(etree.fromstring(body))
210 return resp, body
211
212 def detach_volume(self, volume_id):
213 """Detaches a volume from an instance."""
214 post_body = Element("os-detach")
215 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200216 resp, body = self.post(url, str(Document(post_body)))
anju tiwari789449a2013-08-29 16:56:17 +0530217 if body:
218 body = xml_to_json(etree.fromstring(body))
219 return resp, body
220
Ryan Hsua67f4632013-08-29 16:03:06 -0700221 def upload_volume(self, volume_id, image_name, disk_format):
anju tiwari789449a2013-08-29 16:56:17 +0530222 """Uploads a volume in Glance."""
223 post_body = Element("os-volume_upload_image",
Ryan Hsua67f4632013-08-29 16:03:06 -0700224 image_name=image_name,
225 disk_format=disk_format)
anju tiwari789449a2013-08-29 16:56:17 +0530226 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200227 resp, body = self.post(url, str(Document(post_body)))
anju tiwari789449a2013-08-29 16:56:17 +0530228 volume = xml_to_json(etree.fromstring(body))
229 return resp, volume
wanghao5b981752013-10-22 11:41:41 +0800230
231 def extend_volume(self, volume_id, extend_size):
232 """Extend a volume."""
233 post_body = Element("os-extend",
234 new_size=extend_size)
235 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200236 resp, body = self.post(url, str(Document(post_body)))
wanghao5b981752013-10-22 11:41:41 +0800237 if body:
238 body = xml_to_json(etree.fromstring(body))
239 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800240
241 def reset_volume_status(self, volume_id, status):
242 """Reset the Specified Volume's Status."""
243 post_body = Element("os-reset_status",
244 status=status
245 )
246 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200247 resp, body = self.post(url, str(Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800248 if body:
249 body = xml_to_json(etree.fromstring(body))
250 return resp, body
251
252 def volume_begin_detaching(self, volume_id):
253 """Volume Begin Detaching."""
254 post_body = Element("os-begin_detaching")
255 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200256 resp, body = self.post(url, str(Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800257 if body:
258 body = xml_to_json(etree.fromstring(body))
259 return resp, body
260
261 def volume_roll_detaching(self, volume_id):
262 """Volume Roll Detaching."""
263 post_body = Element("os-roll_detaching")
264 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200265 resp, body = self.post(url, str(Document(post_body)))
wanghaoaa1f2f92013-10-10 11:30:37 +0800266 if body:
267 body = xml_to_json(etree.fromstring(body))
268 return resp, body
zhangyanzi6b632432013-10-24 19:08:50 +0800269
270 def reserve_volume(self, volume_id):
271 """Reserves a volume."""
272 post_body = Element("os-reserve")
273 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200274 resp, body = self.post(url, str(Document(post_body)))
zhangyanzi6b632432013-10-24 19:08:50 +0800275 if body:
276 body = xml_to_json(etree.fromstring(body))
277 return resp, body
278
279 def unreserve_volume(self, volume_id):
280 """Restore a reserved volume ."""
281 post_body = Element("os-unreserve")
282 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200283 resp, body = self.post(url, str(Document(post_body)))
zhangyanzi6b632432013-10-24 19:08:50 +0800284 if body:
285 body = xml_to_json(etree.fromstring(body))
286 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800287
288 def create_volume_transfer(self, vol_id, display_name=None):
289 """Create a volume transfer."""
290 post_body = Element("transfer",
291 volume_id=vol_id)
292 if display_name:
293 post_body.add_attr('name', display_name)
294 resp, body = self.post('os-volume-transfer',
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200295 str(Document(post_body)))
wingwjcbd82dc2013-10-22 16:38:39 +0800296 volume = xml_to_json(etree.fromstring(body))
297 return resp, volume
298
299 def get_volume_transfer(self, transfer_id):
300 """Returns the details of a volume transfer."""
301 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200302 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800303 volume = xml_to_json(etree.fromstring(body))
304 return resp, volume
305
306 def list_volume_transfers(self, params=None):
307 """List all the volume transfers created."""
308 url = 'os-volume-transfer'
309 if params:
310 url += '?%s' % urllib.urlencode(params)
311
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200312 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800313 body = etree.fromstring(body)
314 volumes = []
315 if body is not None:
316 volumes += [self._parse_volume_transfer(vol) for vol in list(body)]
317 return resp, volumes
318
319 def _parse_volume_transfer(self, body):
320 vol = dict((attr, body.get(attr)) for attr in body.keys())
321 for child in body.getchildren():
322 tag = child.tag
323 if tag.startswith("{"):
324 tag = tag.split("}", 1)
325 vol[tag] = xml_to_json(child)
326 return vol
327
328 def delete_volume_transfer(self, transfer_id):
329 """Delete a volume transfer."""
330 return self.delete("os-volume-transfer/%s" % str(transfer_id))
331
332 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
333 """Accept a volume transfer."""
334 post_body = Element("accept", auth_key=transfer_auth_key)
335 url = 'os-volume-transfer/%s/accept' % transfer_id
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200336 resp, body = self.post(url, str(Document(post_body)))
wingwjcbd82dc2013-10-22 16:38:39 +0800337 volume = xml_to_json(etree.fromstring(body))
338 return resp, volume
zhangyanziaa180072013-11-21 12:31:26 +0800339
340 def update_volume_readonly(self, volume_id, readonly):
341 """Update the Specified Volume readonly."""
342 post_body = Element("os-update_readonly_flag",
343 readonly=readonly)
344 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200345 resp, body = self.post(url, str(Document(post_body)))
zhangyanziaa180072013-11-21 12:31:26 +0800346 if body:
347 body = xml_to_json(etree.fromstring(body))
348 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800349
350 def force_delete_volume(self, volume_id):
351 """Force Delete Volume."""
352 post_body = Element("os-force_delete")
353 url = 'volumes/%s/action' % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200354 resp, body = self.post(url, str(Document(post_body)))
wanghao9d3d6cb2013-11-12 15:10:10 +0800355 if body:
356 body = xml_to_json(etree.fromstring(body))
357 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800358
359 def _metadata_body(self, meta):
360 post_body = Element('metadata')
361 for k, v in meta.items():
362 data = Element('meta', key=k)
Ryan McNair9acfcf72014-01-27 21:06:48 +0000363 # Escape value to allow for special XML chars
364 data.append(Text(escape(v)))
huangtianhua0ff41682013-12-16 14:49:31 +0800365 post_body.append(data)
366 return post_body
367
368 def _parse_key_value(self, node):
369 """Parse <foo key='key'>value</foo> data into {'key': 'value'}."""
370 data = {}
371 for node in node.getchildren():
372 data[node.get('key')] = node.text
373 return data
374
375 def create_volume_metadata(self, volume_id, metadata):
376 """Create metadata for the volume."""
377 post_body = self._metadata_body(metadata)
378 resp, body = self.post('volumes/%s/metadata' % volume_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200379 str(Document(post_body)))
huangtianhua0ff41682013-12-16 14:49:31 +0800380 body = self._parse_key_value(etree.fromstring(body))
381 return resp, body
382
383 def get_volume_metadata(self, volume_id):
384 """Get metadata of the volume."""
385 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200386 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800387 body = self._parse_key_value(etree.fromstring(body))
388 return resp, body
389
390 def update_volume_metadata(self, volume_id, metadata):
391 """Update metadata for the volume."""
392 put_body = self._metadata_body(metadata)
393 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200394 resp, body = self.put(url, str(Document(put_body)))
huangtianhua0ff41682013-12-16 14:49:31 +0800395 body = self._parse_key_value(etree.fromstring(body))
396 return resp, body
397
398 def update_volume_metadata_item(self, volume_id, id, meta_item):
399 """Update metadata item for the volume."""
400 for k, v in meta_item.items():
401 put_body = Element('meta', key=k)
402 put_body.append(Text(v))
403 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200404 resp, body = self.put(url, str(Document(put_body)))
huangtianhua0ff41682013-12-16 14:49:31 +0800405 body = xml_to_json(etree.fromstring(body))
406 return resp, body
407
408 def delete_volume_metadata_item(self, volume_id, id):
409 """Delete metadata item for the volume."""
410 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
411 return self.delete(url)