Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
| 3 | # Copyright 2012 IBM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import time |
| 19 | |
| 20 | from lxml import etree |
| 21 | |
| 22 | from tempest.common.rest_client import RestClientXML |
| 23 | from tempest import exceptions |
| 24 | from tempest.services.nova.xml.common import xml_to_json |
| 25 | from tempest.services.nova.xml.common import XMLNS_11 |
| 26 | from tempest.services.nova.xml.common import Element |
| 27 | from tempest.services.nova.xml.common import Text |
| 28 | from tempest.services.nova.xml.common import Document |
| 29 | |
| 30 | |
| 31 | class VolumesClientXML(RestClientXML): |
| 32 | """ |
| 33 | Client class to send CRUD Volume API requests to a Cinder endpoint |
| 34 | """ |
| 35 | |
| 36 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 37 | super(VolumesClientXML, self).__init__(config, username, password, |
| 38 | auth_url, tenant_name) |
| 39 | self.service = self.config.compute.catalog_type |
| 40 | self.build_interval = self.config.compute.build_interval |
| 41 | self.build_timeout = self.config.compute.build_timeout |
| 42 | |
| 43 | def _parse_volume(self, body): |
| 44 | vol = dict((attr, body.get(attr)) for attr in body.keys()) |
| 45 | |
| 46 | for child in body.getchildren(): |
| 47 | tag = child.tag |
| 48 | if tag.startswith("{"): |
| 49 | ns, tag = tag.split("}", 1) |
| 50 | if tag == 'metadata': |
| 51 | vol['metadata'] = dict((meta.get('key'), |
| 52 | meta.text) for meta in list(child)) |
| 53 | else: |
| 54 | vol[tag] = xml_to_json(child) |
| 55 | return vol |
| 56 | |
| 57 | def list_volumes(self, params=None): |
| 58 | """List all the volumes created""" |
| 59 | url = 'volumes' |
| 60 | |
| 61 | if params: |
| 62 | url += '?%s' % urllib.urlencode(params) |
| 63 | |
| 64 | resp, body = self.get(url, self.headers) |
| 65 | body = etree.fromstring(body) |
| 66 | volumes = [] |
| 67 | if body is not None: |
| 68 | volumes += [self._parse_volume(vol) for vol in list(body)] |
| 69 | return resp, volumes |
| 70 | |
| 71 | def list_volumes_with_detail(self, params=None): |
| 72 | """List all the details of volumes""" |
| 73 | url = 'volumes/detail' |
| 74 | |
| 75 | if params: |
| 76 | url += '?%s' % urllib.urlencode(params) |
| 77 | |
| 78 | resp, body = self.get(url, self.headers) |
| 79 | body = etree.fromstring(body) |
| 80 | volumes = [] |
| 81 | if body is not None: |
| 82 | volumes += [self._parse_volume(vol) for vol in list(body)] |
| 83 | return resp, volumes |
| 84 | |
| 85 | def get_volume(self, volume_id): |
| 86 | """Returns the details of a single volume""" |
| 87 | url = "volumes/%s" % str(volume_id) |
| 88 | resp, body = self.get(url, self.headers) |
| 89 | body = etree.fromstring(body) |
| 90 | return resp, self._parse_volume(body) |
| 91 | |
| 92 | def create_volume(self, size, display_name=None, metadata=None): |
| 93 | """Creates a new Volume. |
| 94 | |
| 95 | :param size: Size of volume in GB. (Required) |
| 96 | :param display_name: Optional Volume Name. |
| 97 | :param metadata: An optional dictionary of values for metadata. |
| 98 | """ |
| 99 | volume = Element("volume", |
| 100 | xmlns=XMLNS_11, |
| 101 | size=size) |
| 102 | if display_name: |
| 103 | volume.add_attr('display_name', display_name) |
| 104 | |
| 105 | if metadata: |
| 106 | _metadata = Element('metadata') |
| 107 | volume.append(_metadata) |
| 108 | for key, value in metadata.items(): |
| 109 | meta = Element('meta') |
| 110 | meta.add_attr('key', key) |
| 111 | meta.append(Text(value)) |
| 112 | _metadata.append(meta) |
| 113 | |
| 114 | resp, body = self.post('volumes', str(Document(volume)), |
| 115 | self.headers) |
| 116 | body = xml_to_json(etree.fromstring(body)) |
| 117 | return resp, body |
| 118 | |
| 119 | def delete_volume(self, volume_id): |
| 120 | """Deletes the Specified Volume""" |
| 121 | return self.delete("volumes/%s" % str(volume_id)) |
| 122 | |
| 123 | def wait_for_volume_status(self, volume_id, status): |
| 124 | """Waits for a Volume to reach a given status""" |
| 125 | resp, body = self.get_volume(volume_id) |
| 126 | volume_name = body['displayName'] |
| 127 | volume_status = body['status'] |
| 128 | start = int(time.time()) |
| 129 | |
| 130 | while volume_status != status: |
| 131 | time.sleep(self.build_interval) |
| 132 | resp, body = self.get_volume(volume_id) |
| 133 | volume_status = body['status'] |
| 134 | if volume_status == 'error': |
| 135 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
| 136 | |
| 137 | if int(time.time()) - start >= self.build_timeout: |
| 138 | message = 'Volume %s failed to reach %s status within '\ |
| 139 | 'the required time (%s s).' % (volume_name, status, |
| 140 | self.build_timeout) |
| 141 | raise exceptions.TimeoutException(message) |
| 142 | |
| 143 | def is_resource_deleted(self, id): |
| 144 | try: |
| 145 | self.get_volume(id) |
| 146 | except exceptions.NotFound: |
| 147 | return True |
| 148 | return False |