Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [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 copy |
Attila Fazekas | 5c06881 | 2013-02-14 15:29:44 +0100 | [diff] [blame] | 17 | import errno |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 18 | import json |
| 19 | import os |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 20 | import time |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 21 | import urllib |
| 22 | |
| 23 | from tempest.common import glance_http |
| 24 | from tempest.common.rest_client import RestClient |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 25 | from tempest import config |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 26 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 27 | from tempest.openstack.common import log as logging |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 28 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 | CONF = config.CONF |
| 30 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 31 | LOG = logging.getLogger(__name__) |
| 32 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 33 | |
| 34 | class ImageClientJSON(RestClient): |
| 35 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 36 | def __init__(self, auth_provider): |
| 37 | super(ImageClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 38 | self.service = CONF.images.catalog_type |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 39 | self._http = None |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 40 | |
| 41 | def _image_meta_from_headers(self, headers): |
| 42 | meta = {'properties': {}} |
| 43 | for key, value in headers.iteritems(): |
| 44 | if key.startswith('x-image-meta-property-'): |
| 45 | _key = key[22:] |
| 46 | meta['properties'][_key] = value |
| 47 | elif key.startswith('x-image-meta-'): |
| 48 | _key = key[13:] |
| 49 | meta[_key] = value |
| 50 | |
| 51 | for key in ['is_public', 'protected', 'deleted']: |
| 52 | if key in meta: |
| 53 | meta[key] = meta[key].strip().lower() in ('t', 'true', 'yes', |
| 54 | '1') |
| 55 | for key in ['size', 'min_ram', 'min_disk']: |
| 56 | if key in meta: |
| 57 | try: |
| 58 | meta[key] = int(meta[key]) |
| 59 | except ValueError: |
| 60 | pass |
| 61 | return meta |
| 62 | |
| 63 | def _image_meta_to_headers(self, fields): |
| 64 | headers = {} |
| 65 | fields_copy = copy.deepcopy(fields) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 66 | copy_from = fields_copy.pop('copy_from', None) |
| 67 | if copy_from is not None: |
| 68 | headers['x-glance-api-copy-from'] = copy_from |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 69 | for key, value in fields_copy.pop('properties', {}).iteritems(): |
| 70 | headers['x-image-meta-property-%s' % key] = str(value) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 71 | for key, value in fields_copy.pop('api', {}).iteritems(): |
| 72 | headers['x-glance-api-property-%s' % key] = str(value) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 73 | for key, value in fields_copy.iteritems(): |
| 74 | headers['x-image-meta-%s' % key] = str(value) |
| 75 | return headers |
| 76 | |
| 77 | def _get_file_size(self, obj): |
| 78 | """Analyze file-like object and attempt to determine its size. |
| 79 | |
| 80 | :param obj: file-like object, typically redirected from stdin. |
| 81 | :retval The file's size or None if it cannot be determined. |
| 82 | """ |
| 83 | # For large images, we need to supply the size of the |
| 84 | # image file. See LP Bugs #827660 and #845788. |
| 85 | if hasattr(obj, 'seek') and hasattr(obj, 'tell'): |
| 86 | try: |
| 87 | obj.seek(0, os.SEEK_END) |
| 88 | obj_size = obj.tell() |
| 89 | obj.seek(0) |
| 90 | return obj_size |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 91 | except IOError as e: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 92 | if e.errno == errno.ESPIPE: |
| 93 | # Illegal seek. This means the user is trying |
| 94 | # to pipe image data to the client, e.g. |
| 95 | # echo testdata | bin/glance add blah..., or |
| 96 | # that stdin is empty, or that a file-like |
| 97 | # object which doesn't support 'seek/tell' has |
| 98 | # been supplied. |
| 99 | return None |
| 100 | else: |
| 101 | raise |
| 102 | else: |
| 103 | # Cannot determine size of input image |
| 104 | return None |
| 105 | |
| 106 | def _get_http(self): |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 107 | dscv = CONF.identity.disable_ssl_certificate_validation |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 108 | return glance_http.HTTPClient(auth_provider=self.auth_provider, |
| 109 | filters=self.filters, |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 110 | insecure=dscv) |
| 111 | |
| 112 | def _create_with_data(self, headers, data): |
| 113 | resp, body_iter = self.http.raw_request('POST', '/v1/images', |
| 114 | headers=headers, body=data) |
| 115 | self._error_checker('POST', '/v1/images', headers, data, resp, |
| 116 | body_iter) |
| 117 | body = json.loads(''.join([c for c in body_iter])) |
| 118 | return resp, body['image'] |
| 119 | |
| 120 | def _update_with_data(self, image_id, headers, data): |
| 121 | url = '/v1/images/%s' % image_id |
| 122 | resp, body_iter = self.http.raw_request('PUT', url, headers=headers, |
| 123 | body=data) |
| 124 | self._error_checker('PUT', url, headers, data, |
| 125 | resp, body_iter) |
| 126 | body = json.loads(''.join([c for c in body_iter])) |
| 127 | return resp, body['image'] |
| 128 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 129 | @property |
| 130 | def http(self): |
| 131 | if self._http is None: |
| 132 | if CONF.service_available.glance: |
| 133 | self._http = self._get_http() |
| 134 | return self._http |
| 135 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 136 | def create_image(self, name, container_format, disk_format, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 137 | params = { |
| 138 | "name": name, |
| 139 | "container_format": container_format, |
| 140 | "disk_format": disk_format, |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 141 | } |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 142 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 143 | headers = {} |
| 144 | |
hi2suresh | 75a2030 | 2013-04-09 09:17:06 +0000 | [diff] [blame] | 145 | for option in ['is_public', 'location', 'properties', |
| 146 | 'copy_from', 'min_ram']: |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 147 | if option in kwargs: |
| 148 | params[option] = kwargs.get(option) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 149 | |
| 150 | headers.update(self._image_meta_to_headers(params)) |
| 151 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 152 | if 'data' in kwargs: |
| 153 | return self._create_with_data(headers, kwargs.get('data')) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 154 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 155 | resp, body = self.post('v1/images', None, headers) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 156 | body = json.loads(body) |
| 157 | return resp, body['image'] |
| 158 | |
| 159 | def update_image(self, image_id, name=None, container_format=None, |
| 160 | data=None): |
| 161 | params = {} |
| 162 | headers = {} |
| 163 | if name is not None: |
| 164 | params['name'] = name |
| 165 | |
| 166 | if container_format is not None: |
| 167 | params['container_format'] = container_format |
| 168 | |
| 169 | headers.update(self._image_meta_to_headers(params)) |
| 170 | |
| 171 | if data is not None: |
| 172 | return self._update_with_data(image_id, headers, data) |
| 173 | |
| 174 | url = 'v1/images/%s' % image_id |
| 175 | resp, body = self.put(url, data, headers) |
| 176 | body = json.loads(body) |
| 177 | return resp, body['image'] |
| 178 | |
| 179 | def delete_image(self, image_id): |
| 180 | url = 'v1/images/%s' % image_id |
ivan-zhu | 8f992be | 2013-07-31 14:56:58 +0800 | [diff] [blame] | 181 | return self.delete(url) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 182 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 183 | def image_list(self, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 184 | url = 'v1/images' |
| 185 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 186 | if len(kwargs) > 0: |
| 187 | url += '?%s' % urllib.urlencode(kwargs) |
| 188 | |
| 189 | resp, body = self.get(url) |
| 190 | body = json.loads(body) |
| 191 | return resp, body['images'] |
| 192 | |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 193 | def image_list_detail(self, properties=dict(), **kwargs): |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 194 | url = 'v1/images/detail' |
| 195 | |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 196 | params = {} |
| 197 | for key, value in properties.items(): |
| 198 | params['property-%s' % key] = value |
| 199 | |
| 200 | kwargs.update(params) |
| 201 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 202 | if len(kwargs) > 0: |
| 203 | url += '?%s' % urllib.urlencode(kwargs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 204 | |
| 205 | resp, body = self.get(url) |
| 206 | body = json.loads(body) |
| 207 | return resp, body['images'] |
| 208 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 209 | def get_image_meta(self, image_id): |
| 210 | url = 'v1/images/%s' % image_id |
| 211 | resp, __ = self.head(url) |
| 212 | body = self._image_meta_from_headers(resp) |
| 213 | return resp, body |
| 214 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 215 | def get_image(self, image_id): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 216 | url = 'v1/images/%s' % image_id |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 217 | resp, body = self.get(url) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 218 | return resp, body |
| 219 | |
| 220 | def is_resource_deleted(self, id): |
| 221 | try: |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 222 | self.get_image(id) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 223 | except exceptions.NotFound: |
| 224 | return True |
| 225 | return False |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 226 | |
| 227 | def get_image_membership(self, image_id): |
| 228 | url = 'v1/images/%s/members' % image_id |
| 229 | resp, body = self.get(url) |
| 230 | body = json.loads(body) |
| 231 | return resp, body |
| 232 | |
| 233 | def get_shared_images(self, member_id): |
| 234 | url = 'v1/shared-images/%s' % member_id |
| 235 | resp, body = self.get(url) |
| 236 | body = json.loads(body) |
| 237 | return resp, body |
| 238 | |
| 239 | def add_member(self, member_id, image_id, can_share=False): |
| 240 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
| 241 | body = None |
| 242 | if can_share: |
| 243 | body = json.dumps({'member': {'can_share': True}}) |
| 244 | resp, __ = self.put(url, body, self.headers) |
| 245 | return resp |
| 246 | |
| 247 | def delete_member(self, member_id, image_id): |
| 248 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
| 249 | resp, __ = self.delete(url) |
| 250 | return resp |
| 251 | |
| 252 | def replace_membership_list(self, image_id, member_list): |
| 253 | url = 'v1/images/%s/members' % image_id |
| 254 | body = json.dumps({'membership': member_list}) |
| 255 | resp, data = self.put(url, body, self.headers) |
| 256 | data = json.loads(data) |
| 257 | return resp, data |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 258 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 259 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 260 | def _get_image_status(self, image_id): |
| 261 | resp, meta = self.get_image_meta(image_id) |
| 262 | status = meta['status'] |
| 263 | return status |
| 264 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 265 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 266 | def wait_for_image_status(self, image_id, status): |
| 267 | """Waits for a Image to reach a given status.""" |
| 268 | start_time = time.time() |
| 269 | old_value = value = self._get_image_status(image_id) |
| 270 | while True: |
| 271 | dtime = time.time() - start_time |
| 272 | time.sleep(self.build_interval) |
| 273 | if value != old_value: |
| 274 | LOG.info('Value transition from "%s" to "%s"' |
| 275 | 'in %d second(s).', old_value, |
| 276 | value, dtime) |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 277 | if value == status: |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 278 | return value |
| 279 | |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 280 | if value == 'killed': |
| 281 | raise exceptions.ImageKilledException(image_id=image_id, |
Attila Fazekas | 9dfb907 | 2013-11-13 16:45:36 +0100 | [diff] [blame] | 282 | status=status) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 283 | if dtime > self.build_timeout: |
| 284 | message = ('Time Limit Exceeded! (%ds)' |
| 285 | 'while waiting for %s, ' |
| 286 | 'but we got %s.' % |
| 287 | (self.build_timeout, status, value)) |
| 288 | raise exceptions.TimeoutException(message) |
| 289 | time.sleep(self.build_interval) |
| 290 | old_value = value |
| 291 | value = self._get_image_status(image_id) |