Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 3 | # Copyright 2012 IBM Corp. |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 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 |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 20 | |
| 21 | from lxml import etree |
| 22 | |
| 23 | from tempest.common.rest_client import RestClientXML |
| 24 | from tempest import exceptions |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 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 | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class VolumesClientXML(RestClientXML): |
| 33 | """ |
| 34 | Client class to send CRUD Volume API requests to a Cinder endpoint |
| 35 | """ |
| 36 | |
| 37 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 38 | super(VolumesClientXML, self).__init__(config, username, password, |
| 39 | auth_url, tenant_name) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 40 | self.service = self.config.volume.catalog_type |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 41 | self.build_interval = self.config.compute.build_interval |
| 42 | self.build_timeout = self.config.compute.build_timeout |
| 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 | """ |
| 68 | if volume['bootable'] == 'True': |
| 69 | volume['bootable'] = True |
| 70 | elif volume['bootable'] == 'False': |
| 71 | volume['bootable'] = False |
| 72 | else: |
| 73 | raise ValueError( |
| 74 | 'bootable flag is supposed to be either True or False,' |
| 75 | 'it is %s' % volume['bootable']) |
| 76 | return volume |
| 77 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 78 | def list_volumes(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """List all the volumes created.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 80 | url = 'volumes' |
| 81 | |
| 82 | if params: |
| 83 | url += '?%s' % urllib.urlencode(params) |
| 84 | |
| 85 | resp, body = self.get(url, self.headers) |
| 86 | body = etree.fromstring(body) |
| 87 | volumes = [] |
| 88 | if body is not None: |
| 89 | volumes += [self._parse_volume(vol) for vol in list(body)] |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 90 | for v in volumes: |
| 91 | v = self._check_if_bootable(v) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 92 | return resp, volumes |
| 93 | |
| 94 | def list_volumes_with_detail(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 95 | """List all the details of volumes.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 96 | url = 'volumes/detail' |
| 97 | |
| 98 | if params: |
| 99 | url += '?%s' % urllib.urlencode(params) |
| 100 | |
| 101 | resp, body = self.get(url, self.headers) |
| 102 | body = etree.fromstring(body) |
| 103 | volumes = [] |
| 104 | if body is not None: |
| 105 | volumes += [self._parse_volume(vol) for vol in list(body)] |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 106 | for v in volumes: |
| 107 | v = self._check_if_bootable(v) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 108 | return resp, volumes |
| 109 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 110 | def get_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 111 | """Returns the details of a single volume.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 112 | url = "volumes/%s" % str(volume_id) |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 113 | resp, body = self.get(url, self.headers) |
Nayna Patel | 5e76be1 | 2013-08-19 12:10:16 +0000 | [diff] [blame] | 114 | body = self._parse_volume(etree.fromstring(body)) |
| 115 | body = self._check_if_bootable(body) |
| 116 | return resp, body |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 117 | |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 118 | def create_volume(self, size, **kwargs): |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 119 | """Creates a new Volume. |
| 120 | |
| 121 | :param size: Size of volume in GB. (Required) |
| 122 | :param display_name: Optional Volume Name. |
| 123 | :param metadata: An optional dictionary of values for metadata. |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 124 | :param volume_type: Optional Name of volume_type for the volume |
| 125 | :param snapshot_id: When specified the volume is created from |
| 126 | this snapshot |
Giulio Fidente | 36836c4 | 2013-04-05 15:43:51 +0200 | [diff] [blame] | 127 | :param imageRef: When specified the volume is created from this |
| 128 | image |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 129 | """ |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 130 | # NOTE(afazekas): it should use a volume namespace |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 131 | volume = Element("volume", xmlns=XMLNS_11, size=size) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 132 | |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 133 | if 'metadata' in kwargs: |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 134 | _metadata = Element('metadata') |
| 135 | volume.append(_metadata) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 136 | for key, value in kwargs['metadata'].items(): |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 137 | meta = Element('meta') |
| 138 | meta.add_attr('key', key) |
| 139 | meta.append(Text(value)) |
| 140 | _metadata.append(meta) |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 141 | attr_to_add = kwargs.copy() |
| 142 | del attr_to_add['metadata'] |
| 143 | else: |
| 144 | attr_to_add = kwargs |
| 145 | |
| 146 | for key, value in attr_to_add.items(): |
| 147 | volume.add_attr(key, value) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 148 | |
| 149 | resp, body = self.post('volumes', str(Document(volume)), |
| 150 | self.headers) |
| 151 | body = xml_to_json(etree.fromstring(body)) |
| 152 | return resp, body |
| 153 | |
QingXin Meng | 611768a | 2013-09-18 00:51:33 -0700 | [diff] [blame^] | 154 | def update_volume(self, volume_id, **kwargs): |
| 155 | """Updates the Specified Volume.""" |
| 156 | put_body = Element("volume", xmlns=XMLNS_11, **kwargs) |
| 157 | |
| 158 | resp, body = self.put('volumes/%s' % volume_id, |
| 159 | str(Document(put_body)), |
| 160 | self.headers) |
| 161 | body = xml_to_json(etree.fromstring(body)) |
| 162 | return resp, body |
| 163 | |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 164 | def delete_volume(self, volume_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 165 | """Deletes the Specified Volume.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 166 | return self.delete("volumes/%s" % str(volume_id)) |
| 167 | |
| 168 | def wait_for_volume_status(self, volume_id, status): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 169 | """Waits for a Volume to reach a given status.""" |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 170 | resp, body = self.get_volume(volume_id) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 171 | volume_status = body['status'] |
| 172 | start = int(time.time()) |
| 173 | |
| 174 | while volume_status != status: |
| 175 | time.sleep(self.build_interval) |
| 176 | resp, body = self.get_volume(volume_id) |
| 177 | volume_status = body['status'] |
| 178 | if volume_status == 'error': |
| 179 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
| 180 | |
| 181 | if int(time.time()) - start >= self.build_timeout: |
| 182 | message = 'Volume %s failed to reach %s status within '\ |
Attila Fazekas | 786236c | 2013-01-31 16:06:51 +0100 | [diff] [blame] | 183 | 'the required time (%s s).' % (volume_id, |
| 184 | status, |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 185 | self.build_timeout) |
| 186 | raise exceptions.TimeoutException(message) |
| 187 | |
| 188 | def is_resource_deleted(self, id): |
| 189 | try: |
Attila Fazekas | f53172c | 2013-01-26 01:04:42 +0100 | [diff] [blame] | 190 | self.get_volume(id) |
Matthew Treinish | 9854d5b | 2012-09-20 10:22:13 -0400 | [diff] [blame] | 191 | except exceptions.NotFound: |
| 192 | return True |
| 193 | return False |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 194 | |
| 195 | def attach_volume(self, volume_id, instance_uuid, mountpoint): |
| 196 | """Attaches a volume to a given instance on a given mountpoint.""" |
| 197 | post_body = Element("os-attach", |
| 198 | instance_uuid=instance_uuid, |
| 199 | mountpoint=mountpoint |
| 200 | ) |
| 201 | url = 'volumes/%s/action' % str(volume_id) |
| 202 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 203 | if body: |
| 204 | body = xml_to_json(etree.fromstring(body)) |
| 205 | return resp, body |
| 206 | |
| 207 | def detach_volume(self, volume_id): |
| 208 | """Detaches a volume from an instance.""" |
| 209 | post_body = Element("os-detach") |
| 210 | url = 'volumes/%s/action' % str(volume_id) |
| 211 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 212 | if body: |
| 213 | body = xml_to_json(etree.fromstring(body)) |
| 214 | return resp, body |
| 215 | |
Ryan Hsu | a67f463 | 2013-08-29 16:03:06 -0700 | [diff] [blame] | 216 | def upload_volume(self, volume_id, image_name, disk_format): |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 217 | """Uploads a volume in Glance.""" |
| 218 | post_body = Element("os-volume_upload_image", |
Ryan Hsu | a67f463 | 2013-08-29 16:03:06 -0700 | [diff] [blame] | 219 | image_name=image_name, |
| 220 | disk_format=disk_format) |
anju tiwari | 789449a | 2013-08-29 16:56:17 +0530 | [diff] [blame] | 221 | url = 'volumes/%s/action' % str(volume_id) |
| 222 | resp, body = self.post(url, str(Document(post_body)), self.headers) |
| 223 | volume = xml_to_json(etree.fromstring(body)) |
| 224 | return resp, volume |