Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 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 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 18 | import httplib2 |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 19 | import json |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 20 | import logging |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 21 | from lxml import etree |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 22 | import time |
Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 23 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 24 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import xml_to_json |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 26 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 27 | # redrive rate limited calls at most twice |
| 28 | MAX_RECURSION_DEPTH = 2 |
| 29 | |
| 30 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 31 | class RestClient(object): |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 32 | TYPE = "json" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 33 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 34 | def __init__(self, config, user, password, auth_url, tenant_name=None): |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 35 | self.log = logging.getLogger(__name__) |
David Kranz | 180fed1 | 2012-03-27 14:31:29 -0400 | [diff] [blame] | 36 | self.log.setLevel(getattr(logging, config.compute.log_level)) |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 37 | self.config = config |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 38 | self.user = user |
| 39 | self.password = password |
| 40 | self.auth_url = auth_url |
| 41 | self.tenant_name = tenant_name |
| 42 | |
| 43 | self.service = None |
| 44 | self.token = None |
| 45 | self.base_url = None |
| 46 | self.config = config |
| 47 | self.region = 0 |
| 48 | self.endpoint_url = 'publicURL' |
| 49 | self.strategy = self.config.identity.strategy |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 50 | self.headers = {'Content-Type': 'application/%s' % self.TYPE, |
| 51 | 'Accept': 'application/%s' % self.TYPE} |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 52 | self.build_interval = config.compute.build_interval |
| 53 | self.build_timeout = config.compute.build_timeout |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 54 | self.general_header_lc = set(('cache-control', 'connection', |
| 55 | 'date', 'pragma', 'trailer', |
| 56 | 'transfer-encoding', 'via', |
| 57 | 'warning')) |
| 58 | self.response_header_lc = set(('accept-ranges', 'age', 'etag', |
| 59 | 'location', 'proxy-authenticate', |
| 60 | 'retry-after', 'server', |
| 61 | 'vary', 'www-authenticate')) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 62 | |
| 63 | def _set_auth(self): |
| 64 | """ |
| 65 | Sets the token and base_url used in requests based on the strategy type |
| 66 | """ |
| 67 | |
| 68 | if self.strategy == 'keystone': |
| 69 | self.token, self.base_url = self.keystone_auth(self.user, |
| 70 | self.password, |
| 71 | self.auth_url, |
| 72 | self.service, |
| 73 | self.tenant_name) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 74 | else: |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 75 | self.token, self.base_url = self.basic_auth(self.user, |
| 76 | self.password, |
| 77 | self.auth_url) |
| 78 | |
| 79 | def clear_auth(self): |
| 80 | """ |
| 81 | Can be called to clear the token and base_url so that the next request |
| 82 | will fetch a new token and base_url |
| 83 | """ |
| 84 | |
| 85 | self.token = None |
| 86 | self.base_url = None |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 87 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 88 | def get_auth(self): |
| 89 | """Returns the token of the current request or sets the token if |
| 90 | none""" |
| 91 | |
| 92 | if not self.token: |
| 93 | self._set_auth() |
| 94 | |
| 95 | return self.token |
| 96 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 97 | def basic_auth(self, user, password, auth_url): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 98 | """ |
| 99 | Provides authentication for the target API |
| 100 | """ |
| 101 | |
| 102 | params = {} |
| 103 | params['headers'] = {'User-Agent': 'Test-Client', 'X-Auth-User': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 104 | 'X-Auth-Key': password} |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 105 | |
Jay Pipes | e9e24dd | 2012-12-13 00:09:34 -0500 | [diff] [blame] | 106 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=True) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 107 | resp, body = self.http_obj.request(auth_url, 'GET', **params) |
| 108 | try: |
| 109 | return resp['x-auth-token'], resp['x-server-management-url'] |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 110 | except Exception: |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 111 | raise |
| 112 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 113 | def keystone_auth(self, user, password, auth_url, service, tenant_name): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 114 | """ |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 115 | Provides authentication via Keystone |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 116 | """ |
| 117 | |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 118 | creds = { |
| 119 | 'auth': { |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 120 | 'passwordCredentials': { |
| 121 | 'username': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 122 | 'password': password, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 123 | }, |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 124 | 'tenantName': tenant_name, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Jay Pipes | e9e24dd | 2012-12-13 00:09:34 -0500 | [diff] [blame] | 128 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=True) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 129 | headers = {'Content-Type': 'application/json'} |
| 130 | body = json.dumps(creds) |
| 131 | resp, body = self.http_obj.request(auth_url, 'POST', |
| 132 | headers=headers, body=body) |
| 133 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 134 | if resp.status == 200: |
| 135 | try: |
| 136 | auth_data = json.loads(body)['access'] |
| 137 | token = auth_data['token']['id'] |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 138 | except Exception, e: |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 139 | print "Failed to obtain token for user: %s" % e |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 140 | raise |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 141 | |
| 142 | mgmt_url = None |
| 143 | for ep in auth_data['serviceCatalog']: |
Dan Prince | 8527c8a | 2012-12-14 14:00:31 -0500 | [diff] [blame] | 144 | if ep["type"] == service: |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 145 | mgmt_url = ep['endpoints'][self.region][self.endpoint_url] |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 146 | tenant_id = auth_data['token']['tenant']['id'] |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 147 | break |
| 148 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 149 | if mgmt_url is None: |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 150 | raise exceptions.EndpointNotFound(service) |
| 151 | |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 152 | if service == 'network': |
| 153 | # Keystone does not return the correct endpoint for |
| 154 | # quantum. Handle this separately. |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 155 | mgmt_url = (mgmt_url + self.config.network.api_version + |
| 156 | "/tenants/" + tenant_id) |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 157 | |
| 158 | return token, mgmt_url |
| 159 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 160 | elif resp.status == 401: |
Daryl Walleck | a22f57b | 2012-03-20 16:52:07 -0500 | [diff] [blame] | 161 | raise exceptions.AuthenticationFailure(user=user, |
| 162 | password=password) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 163 | |
| 164 | def post(self, url, body, headers): |
| 165 | return self.request('POST', url, headers, body) |
| 166 | |
Matthew Treinish | 426326e | 2012-11-30 13:17:00 -0500 | [diff] [blame] | 167 | def get(self, url, headers=None, wait=None): |
| 168 | return self.request('GET', url, headers, wait=wait) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 169 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 170 | def delete(self, url, headers=None): |
| 171 | return self.request('DELETE', url, headers) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 172 | |
| 173 | def put(self, url, body, headers): |
| 174 | return self.request('PUT', url, headers, body) |
| 175 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 176 | def head(self, url, headers=None): |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 177 | return self.request('HEAD', url, headers) |
| 178 | |
| 179 | def copy(self, url, headers=None): |
| 180 | return self.request('COPY', url, headers) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 181 | |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 182 | def _log(self, req_url, body, resp, resp_body): |
| 183 | self.log.error('Request URL: ' + req_url) |
| 184 | self.log.error('Request Body: ' + str(body)) |
| 185 | self.log.error('Response Headers: ' + str(resp)) |
| 186 | self.log.error('Response Body: ' + str(resp_body)) |
| 187 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 188 | def _parse_resp(self, body): |
| 189 | return json.loads(body) |
| 190 | |
Matthew Treinish | 426326e | 2012-11-30 13:17:00 -0500 | [diff] [blame] | 191 | def request(self, method, url, |
| 192 | headers=None, body=None, depth=0, wait=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 193 | """A simple HTTP request interface.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 194 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 195 | if (self.token is None) or (self.base_url is None): |
| 196 | self._set_auth() |
| 197 | |
Jay Pipes | e9e24dd | 2012-12-13 00:09:34 -0500 | [diff] [blame] | 198 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=True) |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 199 | if headers is None: |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 200 | headers = {} |
| 201 | headers['X-Auth-Token'] = self.token |
| 202 | |
| 203 | req_url = "%s/%s" % (self.base_url, url) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 204 | resp, resp_body = self.http_obj.request(req_url, method, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 205 | headers=headers, body=body) |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 206 | |
| 207 | #TODO(afazekas): Make sure we can validate all responses, and the |
| 208 | #http library does not do any action automatically |
| 209 | if (resp.status in set((204, 205, 304)) or resp.status < 200 or |
Armando Migliaccio | 4949439 | 2012-12-12 18:53:30 +0000 | [diff] [blame] | 210 | method.upper() == 'HEAD') and resp_body: |
Armando Migliaccio | 41de64f | 2012-12-12 13:44:34 +0000 | [diff] [blame] | 211 | raise exceptions.ResponseWithNonEmptyBody(status=resp.status) |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 212 | |
| 213 | #NOTE(afazekas): |
| 214 | # If the HTTP Status Code is 205 |
| 215 | # 'The response MUST NOT include an entity.' |
| 216 | # A HTTP entity has an entity-body and an 'entity-header'. |
| 217 | # In the HTTP response specification (Section 6) the 'entity-header' |
| 218 | # 'generic-header' and 'response-header' are in OR relation. |
| 219 | # All headers not in the above two group are considered as entity |
| 220 | # header in every interpretation. |
| 221 | |
| 222 | if (resp.status == 205 and |
| 223 | 0 != len(set(resp.keys()) - set(('status',)) - |
| 224 | self.response_header_lc - self.general_header_lc)): |
Armando Migliaccio | 41de64f | 2012-12-12 13:44:34 +0000 | [diff] [blame] | 225 | raise exceptions.ResponseWithEntity() |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 226 | |
| 227 | #NOTE(afazekas) |
| 228 | # Now the swift sometimes (delete not empty container) |
| 229 | # returns with non json error response, we can create new rest class |
| 230 | # for swift. |
| 231 | # Usually RFC2616 says error responses SHOULD contain an explanation. |
| 232 | # The warning is normal for SHOULD/SHOULD NOT case |
| 233 | |
| 234 | # Likely it will cause error |
| 235 | if not body and resp.status >= 400: |
| 236 | self.log.warning("status >= 400 response with empty body") |
| 237 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 238 | if resp.status == 401 or resp.status == 403: |
Daryl Walleck | ced8eb8 | 2012-03-19 13:52:37 -0500 | [diff] [blame] | 239 | self._log(req_url, body, resp, resp_body) |
| 240 | raise exceptions.Unauthorized() |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 241 | |
| 242 | if resp.status == 404: |
Matthew Treinish | 426326e | 2012-11-30 13:17:00 -0500 | [diff] [blame] | 243 | if not wait: |
| 244 | self._log(req_url, body, resp, resp_body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 245 | raise exceptions.NotFound(resp_body) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 246 | |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 247 | if resp.status == 400: |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 248 | resp_body = self._parse_resp(resp_body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 249 | self._log(req_url, body, resp, resp_body) |
David Kranz | 28e35c5 | 2012-07-10 10:14:38 -0400 | [diff] [blame] | 250 | raise exceptions.BadRequest(resp_body) |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 251 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 252 | if resp.status == 409: |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 253 | resp_body = self._parse_resp(resp_body) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 254 | self._log(req_url, body, resp, resp_body) |
| 255 | raise exceptions.Duplicate(resp_body) |
| 256 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 257 | if resp.status == 413: |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 258 | resp_body = self._parse_resp(resp_body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 259 | self._log(req_url, body, resp, resp_body) |
| 260 | if 'overLimit' in resp_body: |
| 261 | raise exceptions.OverLimit(resp_body['overLimit']['message']) |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 262 | elif 'limit' in resp_body['message']: |
| 263 | raise exceptions.OverLimit(resp_body['message']) |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 264 | elif depth < MAX_RECURSION_DEPTH: |
| 265 | delay = resp['Retry-After'] if 'Retry-After' in resp else 60 |
| 266 | time.sleep(int(delay)) |
| 267 | return self.request(method, url, headers, body, depth + 1) |
Jay Pipes | 9b04384 | 2012-01-23 23:34:26 -0500 | [diff] [blame] | 268 | else: |
| 269 | raise exceptions.RateLimitExceeded( |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 270 | message=resp_body['overLimitFault']['message'], |
| 271 | details=resp_body['overLimitFault']['details']) |
Brian Lamar | 12d9b29 | 2011-12-08 12:41:21 -0500 | [diff] [blame] | 272 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 273 | if resp.status in (500, 501): |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 274 | resp_body = self._parse_resp(resp_body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 275 | self._log(req_url, body, resp, resp_body) |
Daryl Walleck | f008703 | 2011-12-18 13:37:05 -0600 | [diff] [blame] | 276 | #I'm seeing both computeFault and cloudServersFault come back. |
| 277 | #Will file a bug to fix, but leave as is for now. |
| 278 | |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 279 | if 'cloudServersFault' in resp_body: |
| 280 | message = resp_body['cloudServersFault']['message'] |
Jay Pipes | edba062 | 2012-07-08 21:34:36 -0400 | [diff] [blame] | 281 | elif 'computeFault' in resp_body: |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 282 | message = resp_body['computeFault']['message'] |
Jay Pipes | edba062 | 2012-07-08 21:34:36 -0400 | [diff] [blame] | 283 | elif 'error' in resp_body: # Keystone errors |
| 284 | message = resp_body['error']['message'] |
| 285 | raise exceptions.IdentityError(message) |
Dan Prince | a4b709c | 2012-10-10 12:27:59 -0400 | [diff] [blame] | 286 | elif 'message' in resp_body: |
| 287 | message = resp_body['message'] |
| 288 | else: |
| 289 | message = resp_body |
| 290 | |
Daryl Walleck | f008703 | 2011-12-18 13:37:05 -0600 | [diff] [blame] | 291 | raise exceptions.ComputeFault(message) |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 292 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 293 | if resp.status >= 400: |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 294 | resp_body = self._parse_resp(resp_body) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 295 | self._log(req_url, body, resp, resp_body) |
| 296 | raise exceptions.TempestException(str(resp.status)) |
| 297 | |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 298 | return resp, resp_body |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 299 | |
| 300 | def wait_for_resource_deletion(self, id): |
| 301 | """Waits for a resource to be deleted""" |
| 302 | start_time = int(time.time()) |
| 303 | while True: |
| 304 | if self.is_resource_deleted(id): |
| 305 | return |
| 306 | if int(time.time()) - start_time >= self.build_timeout: |
| 307 | raise exceptions.TimeoutException |
| 308 | time.sleep(self.build_interval) |
| 309 | |
| 310 | def is_resource_deleted(self, id): |
| 311 | """ |
| 312 | Subclasses override with specific deletion detection. |
| 313 | """ |
| 314 | return False |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 315 | |
| 316 | |
| 317 | class RestClientXML(RestClient): |
| 318 | TYPE = "xml" |
| 319 | |
| 320 | def _parse_resp(self, body): |
| 321 | return xml_to_json(etree.fromstring(body)) |