Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2012 IBM Corp. |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 2 | # 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 | |
| 16 | import time |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 18 | |
| 19 | from lxml import etree |
| 20 | |
| 21 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | from tempest import config |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 23 | from tempest import exceptions |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | from tempest.services.compute.xml.common import Document |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import Element |
| 26 | from tempest.services.compute.xml.common import Text |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 27 | from tempest.services.compute.xml.common import xml_to_json |
| 28 | from tempest.services.compute.xml.common import XMLNS_11 |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 29 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | CONF = config.CONF |
| 31 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 32 | |
| 33 | class VolumesClientXML(RestClientXML): |
| 34 | """ |
| 35 | Client class to send CRUD Volume API requests to a Cinder endpoint |
| 36 | """ |
| 37 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame^] | 38 | def __init__(self, auth_provider): |
| 39 | super(VolumesClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 40 | self.service = CONF.volume.catalog_type |
| 41 | self.build_interval = CONF.compute.build_interval |
| 42 | self.build_timeout = CONF.compute.build_timeout |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 43 | |
| 44 | def _parse_volume(self, body): |
| 45 | vol = dict((attr, body.get(attr)) for attr in body.keys()) |
| 46 | |
| 47 | for child in body.getchildren(): |
| 48 | tag = child.tag |
| 49 | if tag.startswith("{"): |
| 50 | ns, tag = tag.split("}", 1) |
| 51 | if tag == 'metadata': |
| 52 | vol['metadata'] = dict((meta.get('key'), |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 53 | meta.text) for meta in |
| 54 | child.getchildren()) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 55 | else: |
| 56 | vol[tag] = xml_to_json(child) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 57 | return vol |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 58 | |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 59 | def get_attachment_from_volume(self, volume): |
| 60 | """Return the element 'attachment' from input volumes.""" |
| 61 | return volume['attachments']['attachment'] |
| 62 | |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 63 | def _check_if_bootable(self, volume): |
| 64 | """ |
| 65 | Check if the volume is bootable, also change the value |
| 66 | of 'bootable' from string to boolean. |
| 67 | """ |
John Griffith | f55f69e | 2013-09-19 14:10:57 -0600 | [diff] [blame] | 68 | |
| 69 | # NOTE(jdg): Version 1 of Cinder API uses lc strings |
| 70 | # We should consider being explicit in this check to |
| 71 | # avoid introducing bugs like: LP #1227837 |
| 72 | |
| 73 | if volume['bootable'].lower() == 'true': |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 74 | volume['bootable'] = True |
John Griffith | f55f69e | 2013-09-19 14:10:57 -0600 | [diff] [blame] | 75 | elif volume['bootable'].lower() == 'false': |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 76 | volume['bootable'] = False |
| 77 | else: |
| 78 | raise ValueError( |
| 79 | 'bootable flag is supposed to be either True or False,' |
| 80 | 'it is %s' % volume['bootable']) |
| 81 | return volume |
| 82 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 83 | def list_volumes(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 84 | """List all the volumes created.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 85 | url = 'volumes' |
| 86 | |
| 87 | if params: |
| 88 | url += '?%s' % urllib.urlencode(params) |
| 89 | |
| 90 | resp, body = self.get(url, self.headers) |
| 91 | body = etree.fromstring(body) |
| 92 | volumes = [] |
| 93 | if body is not None: |
| 94 | volumes += [self._parse_volume(vol) for vol in list(body)] |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 95 | for v in volumes: |
| 96 | v = self._check_if_bootable(v) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 97 | return resp, volumes |
| 98 | |
| 99 | def list_volumes_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 100 | """List all the details of volumes.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 101 | url = 'volumes/detail' |
| 102 | |
| 103 | if params: |
| 104 | url += '?%s' % urllib.urlencode(params) |
| 105 | |
| 106 | resp, body = self.get(url, self.headers) |
| 107 | body = etree.fromstring(body) |
| 108 | volumes = [] |
| 109 | if body is not None: |
| 110 | volumes += [self._parse_volume(vol) for vol in list(body)] |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 111 | for v in volumes: |
| 112 | v = self._check_if_bootable(v) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 113 | return resp, volumes |
| 114 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 115 | def get_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 116 | """Returns the details of a single volume.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 117 | url = "volumes/%s" % str(volume_id) |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 118 | resp, body = self.get(url, self.headers) |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 119 | body = self._parse_volume(etree.fromstring(body)) |
| 120 | body = self._check_if_bootable(body) |
| 121 | return resp, body |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 122 | |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 123 | def create_volume(self, size, **kwargs): |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 124 | """Creates a new Volume. |
| 125 | |
| 126 | :param size: Size of volume in GB. (Required) |
| 127 | :param display_name: Optional Volume Name. |
| 128 | :param metadata: An optional dictionary of values for metadata. |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 129 | :param volume_type: Optional Name of volume_type for the volume |
| 130 | :param snapshot_id: When specified the volume is created from |
| 131 | this snapshot |
Giulio Fidente | 36836c4 | 2013-04-05 15:43:51 +0200 | [diff] [blame] | 132 | :param imageRef: When specified the volume is created from this |
| 133 | image |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 134 | """ |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 135 | # NOTE(afazekas): it should use a volume namespace |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 136 | volume = Element("volume", xmlns=XMLNS_11, size=size) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 137 | |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 138 | if 'metadata' in kwargs: |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 139 | _metadata = Element('metadata') |
| 140 | volume.append(_metadata) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 141 | for key, value in kwargs['metadata'].items(): |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 142 | meta = Element('meta') |
| 143 | meta.add_attr('key', key) |
| 144 | meta.append(Text(value)) |
| 145 | _metadata.append(meta) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 146 | attr_to_add = kwargs.copy() |
| 147 | del attr_to_add['metadata'] |
| 148 | else: |
| 149 | attr_to_add = kwargs |
| 150 | |
| 151 | for key, value in attr_to_add.items(): |
| 152 | volume.add_attr(key, value) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 153 | |
| 154 | resp, body = self.post('volumes', str(Document(volume)), |
| 155 | self.headers) |
| 156 | body = xml_to_json(etree.fromstring(body)) |
| 157 | return resp, body |
| 158 | |
QingXin Meng | 611768a | 2013-09-18 00:51:33 -0700 | [diff] [blame] | 159 | def update_volume(self, volume_id, **kwargs): |
| 160 | """Updates the Specified Volume.""" |
| 161 | put_body = Element("volume", xmlns=XMLNS_11, **kwargs) |
| 162 | |
| 163 | resp, body = self.put('volumes/%s' % volume_id, |
| 164 | str(Document(put_body)), |
| 165 | self.headers) |
| 166 | body = xml_to_json(etree.fromstring(body)) |
| 167 | return resp, body |
| 168 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 169 | def delete_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 170 | """Deletes the Specified Volume.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 171 | return self.delete("volumes/%s" % str(volume_id)) |
| 172 | |
| 173 | def wait_for_volume_status(self, volume_id, status): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 174 | """Waits for a Volume to reach a given status.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 175 | resp, body = self.get_volume(volume_id) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 176 | 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 Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 188 | 'the required time (%s s).' % (volume_id, |
| 189 | status, |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 190 | self.build_timeout) |
| 191 | raise exceptions.TimeoutException(message) |
| 192 | |
| 193 | def is_resource_deleted(self, id): |
| 194 | try: |
Attila Fazekas | f53172c | 2013-01-26 01:04:42 +0100 | [diff] [blame] | 195 | self.get_volume(id) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 196 | except exceptions.NotFound: |
| 197 | return True |
| 198 | return False |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 199 | |
| 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) |
| 207 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 208 | 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) |
| 216 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 217 | if body: |
| 218 | body = xml_to_json(etree.fromstring(body)) |
| 219 | return resp, body |
| 220 | |
Ryan Hsu | a67f463 | 2013-08-29 16:03:06 -0700 | [diff] [blame] | 221 | def upload_volume(self, volume_id, image_name, disk_format): |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 222 | """Uploads a volume in Glance.""" |
| 223 | post_body = Element("os-volume_upload_image", |
Ryan Hsu | a67f463 | 2013-08-29 16:03:06 -0700 | [diff] [blame] | 224 | image_name=image_name, |
| 225 | disk_format=disk_format) |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 226 | url = 'volumes/%s/action' % str(volume_id) |
| 227 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 228 | volume = xml_to_json(etree.fromstring(body)) |
| 229 | return resp, volume |
wanghao | 5b98175 | 2013-10-22 11:41:41 +0800 | [diff] [blame] | 230 | |
| 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) |
| 236 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 237 | if body: |
| 238 | body = xml_to_json(etree.fromstring(body)) |
| 239 | return resp, body |
wanghao | aa1f2f9 | 2013-10-10 11:30:37 +0800 | [diff] [blame] | 240 | |
| 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) |
| 247 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 248 | 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) |
| 256 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 257 | 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) |
| 265 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 266 | if body: |
| 267 | body = xml_to_json(etree.fromstring(body)) |
| 268 | return resp, body |
zhangyanzi | 6b63243 | 2013-10-24 19:08:50 +0800 | [diff] [blame] | 269 | |
| 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) |
| 274 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 275 | 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) |
| 283 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 284 | if body: |
| 285 | body = xml_to_json(etree.fromstring(body)) |
| 286 | return resp, body |
wingwj | cbd82dc | 2013-10-22 16:38:39 +0800 | [diff] [blame] | 287 | |
| 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', |
| 295 | str(Document(post_body)), |
| 296 | self.headers) |
| 297 | volume = xml_to_json(etree.fromstring(body)) |
| 298 | return resp, volume |
| 299 | |
| 300 | def get_volume_transfer(self, transfer_id): |
| 301 | """Returns the details of a volume transfer.""" |
| 302 | url = "os-volume-transfer/%s" % str(transfer_id) |
| 303 | resp, body = self.get(url, self.headers) |
| 304 | volume = xml_to_json(etree.fromstring(body)) |
| 305 | return resp, volume |
| 306 | |
| 307 | def list_volume_transfers(self, params=None): |
| 308 | """List all the volume transfers created.""" |
| 309 | url = 'os-volume-transfer' |
| 310 | if params: |
| 311 | url += '?%s' % urllib.urlencode(params) |
| 312 | |
| 313 | resp, body = self.get(url, self.headers) |
| 314 | body = etree.fromstring(body) |
| 315 | volumes = [] |
| 316 | if body is not None: |
| 317 | volumes += [self._parse_volume_transfer(vol) for vol in list(body)] |
| 318 | return resp, volumes |
| 319 | |
| 320 | def _parse_volume_transfer(self, body): |
| 321 | vol = dict((attr, body.get(attr)) for attr in body.keys()) |
| 322 | for child in body.getchildren(): |
| 323 | tag = child.tag |
| 324 | if tag.startswith("{"): |
| 325 | tag = tag.split("}", 1) |
| 326 | vol[tag] = xml_to_json(child) |
| 327 | return vol |
| 328 | |
| 329 | def delete_volume_transfer(self, transfer_id): |
| 330 | """Delete a volume transfer.""" |
| 331 | return self.delete("os-volume-transfer/%s" % str(transfer_id)) |
| 332 | |
| 333 | def accept_volume_transfer(self, transfer_id, transfer_auth_key): |
| 334 | """Accept a volume transfer.""" |
| 335 | post_body = Element("accept", auth_key=transfer_auth_key) |
| 336 | url = 'os-volume-transfer/%s/accept' % transfer_id |
| 337 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 338 | volume = xml_to_json(etree.fromstring(body)) |
| 339 | return resp, volume |
zhangyanzi | aa18007 | 2013-11-21 12:31:26 +0800 | [diff] [blame] | 340 | |
| 341 | def update_volume_readonly(self, volume_id, readonly): |
| 342 | """Update the Specified Volume readonly.""" |
| 343 | post_body = Element("os-update_readonly_flag", |
| 344 | readonly=readonly) |
| 345 | url = 'volumes/%s/action' % str(volume_id) |
| 346 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 347 | if body: |
| 348 | body = xml_to_json(etree.fromstring(body)) |
| 349 | return resp, body |
wanghao | 9d3d6cb | 2013-11-12 15:10:10 +0800 | [diff] [blame] | 350 | |
| 351 | def force_delete_volume(self, volume_id): |
| 352 | """Force Delete Volume.""" |
| 353 | post_body = Element("os-force_delete") |
| 354 | url = 'volumes/%s/action' % str(volume_id) |
| 355 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 356 | if body: |
| 357 | body = xml_to_json(etree.fromstring(body)) |
| 358 | return resp, body |
huangtianhua | 0ff4168 | 2013-12-16 14:49:31 +0800 | [diff] [blame] | 359 | |
| 360 | def _metadata_body(self, meta): |
| 361 | post_body = Element('metadata') |
| 362 | for k, v in meta.items(): |
| 363 | data = Element('meta', key=k) |
| 364 | data.append(Text(v)) |
| 365 | 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, |
| 379 | str(Document(post_body)), |
| 380 | self.headers) |
| 381 | body = self._parse_key_value(etree.fromstring(body)) |
| 382 | return resp, body |
| 383 | |
| 384 | def get_volume_metadata(self, volume_id): |
| 385 | """Get metadata of the volume.""" |
| 386 | url = "volumes/%s/metadata" % str(volume_id) |
| 387 | resp, body = self.get(url, self.headers) |
| 388 | body = self._parse_key_value(etree.fromstring(body)) |
| 389 | return resp, body |
| 390 | |
| 391 | def update_volume_metadata(self, volume_id, metadata): |
| 392 | """Update metadata for the volume.""" |
| 393 | put_body = self._metadata_body(metadata) |
| 394 | url = "volumes/%s/metadata" % str(volume_id) |
| 395 | resp, body = self.put(url, str(Document(put_body)), self.headers) |
| 396 | body = self._parse_key_value(etree.fromstring(body)) |
| 397 | return resp, body |
| 398 | |
| 399 | def update_volume_metadata_item(self, volume_id, id, meta_item): |
| 400 | """Update metadata item for the volume.""" |
| 401 | for k, v in meta_item.items(): |
| 402 | put_body = Element('meta', key=k) |
| 403 | put_body.append(Text(v)) |
| 404 | url = "volumes/%s/metadata/%s" % (str(volume_id), str(id)) |
| 405 | resp, body = self.put(url, str(Document(put_body)), self.headers) |
| 406 | body = xml_to_json(etree.fromstring(body)) |
| 407 | return resp, body |
| 408 | |
| 409 | def delete_volume_metadata_item(self, volume_id, id): |
| 410 | """Delete metadata item for the volume.""" |
| 411 | url = "volumes/%s/metadata/%s" % (str(volume_id), str(id)) |
| 412 | return self.delete(url) |