Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -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 | |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 18 | import time |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from lxml import etree |
| 22 | |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 23 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | from tempest import exceptions |
| 25 | from tempest.services.compute.xml.common import Document |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 26 | from tempest.services.compute.xml.common import Element |
| 27 | from tempest.services.compute.xml.common import Text |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 28 | from tempest.services.compute.xml.common import xml_to_json |
| 29 | from tempest.services.compute.xml.common import XMLNS_11 |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class VolumesExtensionsClientXML(RestClientXML): |
| 33 | |
| 34 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 35 | super(VolumesExtensionsClientXML, self).__init__(config, |
| 36 | username, password, |
| 37 | auth_url, tenant_name) |
| 38 | self.service = self.config.compute.catalog_type |
| 39 | self.build_interval = self.config.compute.build_interval |
| 40 | self.build_timeout = self.config.compute.build_timeout |
| 41 | |
| 42 | def _parse_volume(self, body): |
| 43 | vol = dict((attr, body.get(attr)) for attr in body.keys()) |
| 44 | |
| 45 | for child in body.getchildren(): |
| 46 | tag = child.tag |
| 47 | if tag.startswith("{"): |
| 48 | ns, tag = tag.split("}", 1) |
| 49 | if tag == 'metadata': |
| 50 | vol['metadata'] = dict((meta.get('key'), |
| 51 | meta.text) for meta in list(child)) |
| 52 | else: |
| 53 | vol[tag] = xml_to_json(child) |
| 54 | return vol |
| 55 | |
| 56 | def list_volumes(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 57 | """List all the volumes created.""" |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 58 | url = 'os-volumes' |
| 59 | |
| 60 | if params: |
| 61 | url += '?%s' % urllib.urlencode(params) |
| 62 | |
| 63 | resp, body = self.get(url, self.headers) |
| 64 | body = etree.fromstring(body) |
| 65 | volumes = [] |
| 66 | if body is not None: |
| 67 | volumes += [self._parse_volume(vol) for vol in list(body)] |
| 68 | return resp, volumes |
| 69 | |
| 70 | def list_volumes_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 71 | """List all the details of volumes.""" |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 72 | url = 'os-volumes/detail' |
| 73 | |
| 74 | if params: |
| 75 | url += '?%s' % urllib.urlencode(params) |
| 76 | |
| 77 | resp, body = self.get(url, self.headers) |
| 78 | body = etree.fromstring(body) |
| 79 | volumes = [] |
| 80 | if body is not None: |
| 81 | volumes += [self._parse_volume(vol) for vol in list(body)] |
| 82 | return resp, volumes |
| 83 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame^] | 84 | def get_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 85 | """Returns the details of a single volume.""" |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 86 | url = "os-volumes/%s" % str(volume_id) |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame^] | 87 | resp, body = self.get(url, self.headers) |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 88 | body = etree.fromstring(body) |
| 89 | return resp, self._parse_volume(body) |
| 90 | |
| 91 | def create_volume(self, size, display_name=None, metadata=None): |
| 92 | """Creates a new Volume. |
| 93 | |
| 94 | :param size: Size of volume in GB. (Required) |
| 95 | :param display_name: Optional Volume Name. |
| 96 | :param metadata: An optional dictionary of values for metadata. |
| 97 | """ |
| 98 | volume = Element("volume", |
| 99 | xmlns=XMLNS_11, |
| 100 | size=size) |
| 101 | if display_name: |
| 102 | volume.add_attr('display_name', display_name) |
| 103 | |
| 104 | if metadata: |
| 105 | _metadata = Element('metadata') |
| 106 | volume.append(_metadata) |
| 107 | for key, value in metadata.items(): |
| 108 | meta = Element('meta') |
| 109 | meta.add_attr('key', key) |
| 110 | meta.append(Text(value)) |
| 111 | _metadata.append(meta) |
| 112 | |
| 113 | resp, body = self.post('os-volumes', str(Document(volume)), |
| 114 | self.headers) |
| 115 | body = xml_to_json(etree.fromstring(body)) |
| 116 | return resp, body |
| 117 | |
| 118 | def delete_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 119 | """Deletes the Specified Volume.""" |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 120 | return self.delete("os-volumes/%s" % str(volume_id)) |
| 121 | |
| 122 | def wait_for_volume_status(self, volume_id, status): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 123 | """Waits for a Volume to reach a given status.""" |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 124 | resp, body = self.get_volume(volume_id) |
| 125 | volume_name = body['displayName'] |
| 126 | volume_status = body['status'] |
| 127 | start = int(time.time()) |
| 128 | |
| 129 | while volume_status != status: |
| 130 | time.sleep(self.build_interval) |
| 131 | resp, body = self.get_volume(volume_id) |
| 132 | volume_status = body['status'] |
| 133 | if volume_status == 'error': |
| 134 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
| 135 | |
| 136 | if int(time.time()) - start >= self.build_timeout: |
| 137 | message = 'Volume %s failed to reach %s status within '\ |
| 138 | 'the required time (%s s).' % (volume_name, status, |
| 139 | self.build_timeout) |
| 140 | raise exceptions.TimeoutException(message) |
| 141 | |
| 142 | def is_resource_deleted(self, id): |
| 143 | try: |
Attila Fazekas | f53172c | 2013-01-26 01:04:42 +0100 | [diff] [blame] | 144 | self.get_volume(id) |
Matthew Treinish | 4e08690 | 2012-08-17 17:52:22 -0400 | [diff] [blame] | 145 | except exceptions.NotFound: |
| 146 | return True |
| 147 | return False |