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 os |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 19 | import time |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 20 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 21 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 22 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 23 | import six |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 24 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 25 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 26 | from tempest.common import glance_http |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 27 | from tempest.common import service_client |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 28 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 29 | from tempest.lib.common.utils import misc as misc_utils |
| 30 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 31 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 32 | LOG = logging.getLogger(__name__) |
| 33 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 34 | |
Ken'ichi Ohmichi | 69dcf44 | 2015-11-30 11:48:01 +0000 | [diff] [blame] | 35 | class ImagesClient(service_client.ServiceClient): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 36 | |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 37 | def __init__(self, auth_provider, catalog_type, region, endpoint_type=None, |
| 38 | build_interval=None, build_timeout=None, |
| 39 | disable_ssl_certificate_validation=None, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 40 | ca_certs=None, trace_requests=None): |
Ken'ichi Ohmichi | 69dcf44 | 2015-11-30 11:48:01 +0000 | [diff] [blame] | 41 | super(ImagesClient, self).__init__( |
Ken'ichi Ohmichi | 0690ea4 | 2015-01-02 07:03:51 +0000 | [diff] [blame] | 42 | auth_provider, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 43 | catalog_type, |
| 44 | region, |
| 45 | endpoint_type=endpoint_type, |
| 46 | build_interval=build_interval, |
| 47 | build_timeout=build_timeout, |
| 48 | disable_ssl_certificate_validation=( |
| 49 | disable_ssl_certificate_validation), |
| 50 | ca_certs=ca_certs, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 51 | trace_requests=trace_requests) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 52 | self._http = None |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 53 | self.dscv = disable_ssl_certificate_validation |
| 54 | self.ca_certs = ca_certs |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 55 | |
| 56 | def _image_meta_from_headers(self, headers): |
| 57 | meta = {'properties': {}} |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 58 | for key, value in six.iteritems(headers): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 59 | if key.startswith('x-image-meta-property-'): |
| 60 | _key = key[22:] |
| 61 | meta['properties'][_key] = value |
| 62 | elif key.startswith('x-image-meta-'): |
| 63 | _key = key[13:] |
| 64 | meta[_key] = value |
| 65 | |
| 66 | for key in ['is_public', 'protected', 'deleted']: |
| 67 | if key in meta: |
| 68 | meta[key] = meta[key].strip().lower() in ('t', 'true', 'yes', |
| 69 | '1') |
| 70 | for key in ['size', 'min_ram', 'min_disk']: |
| 71 | if key in meta: |
| 72 | try: |
| 73 | meta[key] = int(meta[key]) |
| 74 | except ValueError: |
| 75 | pass |
| 76 | return meta |
| 77 | |
| 78 | def _image_meta_to_headers(self, fields): |
| 79 | headers = {} |
| 80 | fields_copy = copy.deepcopy(fields) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 81 | copy_from = fields_copy.pop('copy_from', None) |
| 82 | if copy_from is not None: |
| 83 | headers['x-glance-api-copy-from'] = copy_from |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 84 | for key, value in six.iteritems(fields_copy.pop('properties', {})): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 85 | headers['x-image-meta-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 86 | for key, value in six.iteritems(fields_copy.pop('api', {})): |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 87 | headers['x-glance-api-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 88 | for key, value in six.iteritems(fields_copy): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 89 | headers['x-image-meta-%s' % key] = str(value) |
| 90 | return headers |
| 91 | |
| 92 | def _get_file_size(self, obj): |
| 93 | """Analyze file-like object and attempt to determine its size. |
| 94 | |
| 95 | :param obj: file-like object, typically redirected from stdin. |
| 96 | :retval The file's size or None if it cannot be determined. |
| 97 | """ |
| 98 | # For large images, we need to supply the size of the |
| 99 | # image file. See LP Bugs #827660 and #845788. |
| 100 | if hasattr(obj, 'seek') and hasattr(obj, 'tell'): |
| 101 | try: |
| 102 | obj.seek(0, os.SEEK_END) |
| 103 | obj_size = obj.tell() |
| 104 | obj.seek(0) |
| 105 | return obj_size |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 106 | except IOError as e: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 107 | if e.errno == errno.ESPIPE: |
| 108 | # Illegal seek. This means the user is trying |
| 109 | # to pipe image data to the client, e.g. |
| 110 | # echo testdata | bin/glance add blah..., or |
| 111 | # that stdin is empty, or that a file-like |
| 112 | # object which doesn't support 'seek/tell' has |
| 113 | # been supplied. |
| 114 | return None |
| 115 | else: |
| 116 | raise |
| 117 | else: |
| 118 | # Cannot determine size of input image |
| 119 | return None |
| 120 | |
| 121 | def _get_http(self): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 122 | return glance_http.HTTPClient(auth_provider=self.auth_provider, |
| 123 | filters=self.filters, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 124 | insecure=self.dscv, |
| 125 | ca_certs=self.ca_certs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 126 | |
| 127 | def _create_with_data(self, headers, data): |
| 128 | resp, body_iter = self.http.raw_request('POST', '/v1/images', |
| 129 | headers=headers, body=data) |
| 130 | self._error_checker('POST', '/v1/images', headers, data, resp, |
| 131 | body_iter) |
| 132 | body = json.loads(''.join([c for c in body_iter])) |
John Warren | 6620725 | 2015-07-31 15:51:02 -0400 | [diff] [blame] | 133 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 134 | |
| 135 | def _update_with_data(self, image_id, headers, data): |
| 136 | url = '/v1/images/%s' % image_id |
| 137 | resp, body_iter = self.http.raw_request('PUT', url, headers=headers, |
| 138 | body=data) |
| 139 | self._error_checker('PUT', url, headers, data, |
| 140 | resp, body_iter) |
| 141 | body = json.loads(''.join([c for c in body_iter])) |
John Warren | 6620725 | 2015-07-31 15:51:02 -0400 | [diff] [blame] | 142 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 143 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 144 | @property |
| 145 | def http(self): |
| 146 | if self._http is None: |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 147 | self._http = self._get_http() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 148 | return self._http |
| 149 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 150 | def create_image(self, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 151 | headers = {} |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 152 | data = kwargs.pop('data', None) |
| 153 | headers.update(self._image_meta_to_headers(kwargs)) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 154 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 155 | if data is not None: |
| 156 | return self._create_with_data(headers, data) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 157 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 158 | resp, body = self.post('v1/images', None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 159 | self.expected_success(201, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 160 | body = json.loads(body) |
John Warren | 6620725 | 2015-07-31 15:51:02 -0400 | [diff] [blame] | 161 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 162 | |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 163 | def update_image(self, image_id, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 164 | headers = {} |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 165 | data = kwargs.pop('data', None) |
| 166 | headers.update(self._image_meta_to_headers(kwargs)) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 167 | |
| 168 | if data is not None: |
| 169 | return self._update_with_data(image_id, headers, data) |
| 170 | |
| 171 | url = 'v1/images/%s' % image_id |
Ghanshyam | 86a31e1 | 2015-12-16 15:42:47 +0900 | [diff] [blame] | 172 | resp, body = self.put(url, None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 173 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 174 | body = json.loads(body) |
John Warren | 6620725 | 2015-07-31 15:51:02 -0400 | [diff] [blame] | 175 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 176 | |
| 177 | def delete_image(self, image_id): |
| 178 | url = 'v1/images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 179 | resp, body = self.delete(url) |
| 180 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 181 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 182 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 183 | def list_images(self, detail=False, **kwargs): |
| 184 | """Return a list of all images filtered by input parameters. |
| 185 | |
| 186 | Available params: see http://developer.openstack.org/ |
| 187 | api-ref-image-v1.html#listImage-v1 |
| 188 | |
| 189 | Most parameters except the following are passed to the API without |
| 190 | any changes. |
| 191 | :param changes_since: The name is changed to changes-since |
| 192 | """ |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 193 | url = 'v1/images' |
| 194 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 195 | if detail: |
| 196 | url += '/detail' |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 197 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 198 | properties = kwargs.pop('properties', {}) |
| 199 | for key, value in six.iteritems(properties): |
| 200 | kwargs['property-%s' % key] = value |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 201 | |
Ghanshyam | e60db48 | 2016-01-13 09:50:12 +0900 | [diff] [blame] | 202 | if kwargs.get('changes_since'): |
| 203 | kwargs['changes-since'] = kwargs.pop('changes_since') |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 204 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 205 | if len(kwargs) > 0: |
| 206 | url += '?%s' % urllib.urlencode(kwargs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 207 | |
| 208 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 209 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 210 | body = json.loads(body) |
John Warren | 6620725 | 2015-07-31 15:51:02 -0400 | [diff] [blame] | 211 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 212 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 213 | def get_image_meta(self, image_id): |
| 214 | url = 'v1/images/%s' % image_id |
| 215 | resp, __ = self.head(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 216 | self.expected_success(200, resp.status) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 217 | body = self._image_meta_from_headers(resp) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 218 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 219 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 220 | def show_image(self, image_id): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 221 | url = 'v1/images/%s' % image_id |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 222 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 223 | self.expected_success(200, resp.status) |
David Kranz | d7e97b4 | 2015-02-16 09:37:31 -0500 | [diff] [blame] | 224 | return service_client.ResponseBodyData(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 225 | |
| 226 | def is_resource_deleted(self, id): |
| 227 | try: |
Attila Fazekas | 9fa2947 | 2014-08-18 09:48:00 +0200 | [diff] [blame] | 228 | self.get_image_meta(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 229 | except lib_exc.NotFound: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 230 | return True |
| 231 | return False |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 232 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 233 | @property |
| 234 | def resource_type(self): |
| 235 | """Returns the primary type of resource this client works with.""" |
| 236 | return 'image_meta' |
| 237 | |
Ken'ichi Ohmichi | f0df53c | 2015-05-22 01:16:50 +0000 | [diff] [blame] | 238 | def list_image_members(self, image_id): |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 239 | url = 'v1/images/%s/members' % image_id |
| 240 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 241 | self.expected_success(200, resp.status) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 242 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 243 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 244 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 245 | def list_shared_images(self, tenant_id): |
| 246 | """List shared images with the specified tenant""" |
| 247 | url = 'v1/shared-images/%s' % tenant_id |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 248 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 249 | self.expected_success(200, resp.status) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 250 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 251 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 252 | |
Ghanshyam | 8b1603b | 2015-12-02 16:04:53 +0900 | [diff] [blame] | 253 | def add_member(self, member_id, image_id, **kwargs): |
| 254 | """Add a member to an image. |
| 255 | |
| 256 | Available params: see http://developer.openstack.org/ |
| 257 | api-ref-image-v1.html#addMember-v1 |
| 258 | """ |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 259 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
Ghanshyam | 8b1603b | 2015-12-02 16:04:53 +0900 | [diff] [blame] | 260 | body = json.dumps({'member': kwargs}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 261 | resp, __ = self.put(url, body) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 262 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 263 | return service_client.ResponseBody(resp) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 264 | |
| 265 | def delete_member(self, member_id, image_id): |
| 266 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
| 267 | resp, __ = self.delete(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 268 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 269 | return service_client.ResponseBody(resp) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 270 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 271 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 272 | def _get_image_status(self, image_id): |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 273 | meta = self.get_image_meta(image_id) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 274 | status = meta['status'] |
| 275 | return status |
| 276 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 277 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 278 | def wait_for_image_status(self, image_id, status): |
| 279 | """Waits for a Image to reach a given status.""" |
| 280 | start_time = time.time() |
| 281 | old_value = value = self._get_image_status(image_id) |
| 282 | while True: |
| 283 | dtime = time.time() - start_time |
| 284 | time.sleep(self.build_interval) |
| 285 | if value != old_value: |
| 286 | LOG.info('Value transition from "%s" to "%s"' |
| 287 | 'in %d second(s).', old_value, |
| 288 | value, dtime) |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 289 | if value == status: |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 290 | return value |
| 291 | |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 292 | if value == 'killed': |
| 293 | raise exceptions.ImageKilledException(image_id=image_id, |
Attila Fazekas | 9dfb907 | 2013-11-13 16:45:36 +0100 | [diff] [blame] | 294 | status=status) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 295 | if dtime > self.build_timeout: |
| 296 | message = ('Time Limit Exceeded! (%ds)' |
| 297 | 'while waiting for %s, ' |
| 298 | 'but we got %s.' % |
| 299 | (self.build_timeout, status, value)) |
Matt Riedemann | 7cec346 | 2014-06-25 12:48:43 -0700 | [diff] [blame] | 300 | caller = misc_utils.find_test_caller() |
| 301 | if caller: |
| 302 | message = '(%s) %s' % (caller, message) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 303 | raise exceptions.TimeoutException(message) |
| 304 | time.sleep(self.build_interval) |
| 305 | old_value = value |
| 306 | value = self._get_image_status(image_id) |