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 | |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 18 | import hashlib |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 19 | import httplib2 |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 20 | import json |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 21 | import logging |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 22 | from lxml import etree |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 23 | import re |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 24 | import time |
Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 25 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 26 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 27 | from tempest.services.compute.xml.common import xml_to_json |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 28 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 29 | # redrive rate limited calls at most twice |
| 30 | MAX_RECURSION_DEPTH = 2 |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 31 | TOKEN_CHARS_RE = re.compile('^[-A-Za-z0-9+/=]*$') |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 32 | |
| 33 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 34 | class RestClient(object): |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 35 | TYPE = "json" |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 36 | LOG = logging.getLogger(__name__) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 37 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 38 | def __init__(self, config, user, password, auth_url, tenant_name=None): |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 39 | self.config = config |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 40 | self.user = user |
| 41 | self.password = password |
| 42 | self.auth_url = auth_url |
| 43 | self.tenant_name = tenant_name |
| 44 | |
| 45 | self.service = None |
| 46 | self.token = None |
| 47 | self.base_url = None |
Attila Fazekas | cadcb1f | 2013-01-21 23:10:53 +0100 | [diff] [blame] | 48 | self.region = {'compute': self.config.identity.region} |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 49 | self.endpoint_url = 'publicURL' |
| 50 | self.strategy = self.config.identity.strategy |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 51 | self.headers = {'Content-Type': 'application/%s' % self.TYPE, |
| 52 | 'Accept': 'application/%s' % self.TYPE} |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 53 | self.build_interval = config.compute.build_interval |
| 54 | self.build_timeout = config.compute.build_timeout |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 55 | self.general_header_lc = set(('cache-control', 'connection', |
| 56 | 'date', 'pragma', 'trailer', |
| 57 | 'transfer-encoding', 'via', |
| 58 | 'warning')) |
| 59 | self.response_header_lc = set(('accept-ranges', 'age', 'etag', |
| 60 | 'location', 'proxy-authenticate', |
| 61 | 'retry-after', 'server', |
| 62 | 'vary', 'www-authenticate')) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 63 | |
| 64 | def _set_auth(self): |
| 65 | """ |
| 66 | Sets the token and base_url used in requests based on the strategy type |
| 67 | """ |
| 68 | |
| 69 | if self.strategy == 'keystone': |
| 70 | self.token, self.base_url = self.keystone_auth(self.user, |
| 71 | self.password, |
| 72 | self.auth_url, |
| 73 | self.service, |
| 74 | self.tenant_name) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 75 | else: |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 76 | self.token, self.base_url = self.basic_auth(self.user, |
| 77 | self.password, |
| 78 | self.auth_url) |
| 79 | |
| 80 | def clear_auth(self): |
| 81 | """ |
| 82 | Can be called to clear the token and base_url so that the next request |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 83 | will fetch a new token and base_url. |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 84 | """ |
| 85 | |
| 86 | self.token = None |
| 87 | self.base_url = None |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 88 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 89 | def get_auth(self): |
| 90 | """Returns the token of the current request or sets the token if |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 91 | none. |
| 92 | """ |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 93 | |
| 94 | if not self.token: |
| 95 | self._set_auth() |
| 96 | |
| 97 | return self.token |
| 98 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 99 | def basic_auth(self, user, password, auth_url): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 100 | """ |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 101 | Provides authentication for the target API. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 102 | """ |
| 103 | |
| 104 | params = {} |
| 105 | params['headers'] = {'User-Agent': 'Test-Client', 'X-Auth-User': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 106 | 'X-Auth-Key': password} |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 107 | |
Jay Pipes | cd8eaec | 2013-01-16 21:03:48 -0500 | [diff] [blame] | 108 | dscv = self.config.identity.disable_ssl_certificate_validation |
| 109 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=dscv) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 110 | resp, body = self.http_obj.request(auth_url, 'GET', **params) |
| 111 | try: |
| 112 | return resp['x-auth-token'], resp['x-server-management-url'] |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 113 | except Exception: |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 114 | raise |
| 115 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 116 | def keystone_auth(self, user, password, auth_url, service, tenant_name): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 117 | """ |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 118 | Provides authentication via Keystone. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 119 | """ |
| 120 | |
Jay Pipes | 7c88eb2 | 2013-01-16 21:32:43 -0500 | [diff] [blame] | 121 | # Normalize URI to ensure /tokens is in it. |
| 122 | if 'tokens' not in auth_url: |
| 123 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 124 | |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 125 | creds = { |
| 126 | 'auth': { |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 127 | 'passwordCredentials': { |
| 128 | 'username': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 129 | 'password': password, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 130 | }, |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 131 | 'tenantName': tenant_name, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Jay Pipes | cd8eaec | 2013-01-16 21:03:48 -0500 | [diff] [blame] | 135 | dscv = self.config.identity.disable_ssl_certificate_validation |
| 136 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=dscv) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 137 | headers = {'Content-Type': 'application/json'} |
| 138 | body = json.dumps(creds) |
| 139 | resp, body = self.http_obj.request(auth_url, 'POST', |
| 140 | headers=headers, body=body) |
| 141 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 142 | if resp.status == 200: |
| 143 | try: |
| 144 | auth_data = json.loads(body)['access'] |
| 145 | token = auth_data['token']['id'] |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 146 | except Exception, e: |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 147 | print "Failed to obtain token for user: %s" % e |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 148 | raise |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 149 | |
| 150 | mgmt_url = None |
| 151 | for ep in auth_data['serviceCatalog']: |
Dan Prince | 8527c8a | 2012-12-14 14:00:31 -0500 | [diff] [blame] | 152 | if ep["type"] == service: |
K Jonathan Harker | d6ba4b4 | 2012-12-18 13:50:47 -0800 | [diff] [blame] | 153 | for _ep in ep['endpoints']: |
| 154 | if service in self.region and \ |
| 155 | _ep['region'] == self.region[service]: |
| 156 | mgmt_url = _ep[self.endpoint_url] |
| 157 | if not mgmt_url: |
| 158 | mgmt_url = ep['endpoints'][0][self.endpoint_url] |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 159 | tenant_id = auth_data['token']['tenant']['id'] |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 160 | break |
| 161 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 162 | if mgmt_url is None: |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 163 | raise exceptions.EndpointNotFound(service) |
| 164 | |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 165 | if service == 'network': |
| 166 | # Keystone does not return the correct endpoint for |
| 167 | # quantum. Handle this separately. |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 168 | mgmt_url = (mgmt_url + self.config.network.api_version + |
| 169 | "/tenants/" + tenant_id) |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 170 | |
| 171 | return token, mgmt_url |
| 172 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 173 | elif resp.status == 401: |
Daryl Walleck | a22f57b | 2012-03-20 16:52:07 -0500 | [diff] [blame] | 174 | raise exceptions.AuthenticationFailure(user=user, |
| 175 | password=password) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 176 | |
| 177 | def post(self, url, body, headers): |
| 178 | return self.request('POST', url, headers, body) |
| 179 | |
Matthew Treinish | 426326e | 2012-11-30 13:17:00 -0500 | [diff] [blame] | 180 | def get(self, url, headers=None, wait=None): |
| 181 | return self.request('GET', url, headers, wait=wait) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 182 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 183 | def delete(self, url, headers=None): |
| 184 | return self.request('DELETE', url, headers) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 185 | |
| 186 | def put(self, url, body, headers): |
| 187 | return self.request('PUT', url, headers, body) |
| 188 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 189 | def head(self, url, headers=None): |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 190 | return self.request('HEAD', url, headers) |
| 191 | |
| 192 | def copy(self, url, headers=None): |
| 193 | return self.request('COPY', url, headers) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 194 | |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 195 | def _log_request(self, method, req_url, headers, body): |
| 196 | self.LOG.info('Request: ' + method + ' ' + req_url) |
| 197 | if headers: |
| 198 | print_headers = headers |
| 199 | if 'X-Auth-Token' in headers and headers['X-Auth-Token']: |
| 200 | token = headers['X-Auth-Token'] |
| 201 | if len(token) > 64 and TOKEN_CHARS_RE.match(token): |
| 202 | print_headers = headers.copy() |
| 203 | print_headers['X-Auth-Token'] = "<Token omitted>" |
| 204 | self.LOG.debug('Request Headers: ' + str(print_headers)) |
| 205 | if body: |
| 206 | str_body = str(body) |
| 207 | length = len(str_body) |
| 208 | self.LOG.debug('Request Body: ' + str_body[:2048]) |
| 209 | if length >= 2048: |
| 210 | self.LOG.debug("Large body (%d) md5 summary: %s", length, |
| 211 | hashlib.md5(str_body).hexdigest()) |
| 212 | |
| 213 | def _log_response(self, resp, resp_body): |
| 214 | status = resp['status'] |
| 215 | self.LOG.info("Response Status: " + status) |
| 216 | headers = resp.copy() |
| 217 | del headers['status'] |
| 218 | if len(headers): |
| 219 | self.LOG.debug('Response Headers: ' + str(headers)) |
| 220 | if resp_body: |
| 221 | str_body = str(resp_body) |
| 222 | length = len(str_body) |
| 223 | self.LOG.debug('Response Body: ' + str_body[:2048]) |
| 224 | if length >= 2048: |
| 225 | self.LOG.debug("Large body (%d) md5 summary: %s", length, |
| 226 | hashlib.md5(str_body).hexdigest()) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 227 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 228 | def _parse_resp(self, body): |
| 229 | return json.loads(body) |
| 230 | |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 231 | def response_checker(self, method, url, headers, body, resp, resp_body): |
| 232 | if (resp.status in set((204, 205, 304)) or resp.status < 200 or |
| 233 | method.upper() == 'HEAD') and resp_body: |
| 234 | raise exceptions.ResponseWithNonEmptyBody(status=resp.status) |
| 235 | #NOTE(afazekas): |
| 236 | # If the HTTP Status Code is 205 |
| 237 | # 'The response MUST NOT include an entity.' |
| 238 | # A HTTP entity has an entity-body and an 'entity-header'. |
| 239 | # In the HTTP response specification (Section 6) the 'entity-header' |
| 240 | # 'generic-header' and 'response-header' are in OR relation. |
| 241 | # All headers not in the above two group are considered as entity |
| 242 | # header in every interpretation. |
| 243 | |
| 244 | if (resp.status == 205 and |
| 245 | 0 != len(set(resp.keys()) - set(('status',)) - |
| 246 | self.response_header_lc - self.general_header_lc)): |
| 247 | raise exceptions.ResponseWithEntity() |
| 248 | #NOTE(afazekas) |
| 249 | # Now the swift sometimes (delete not empty container) |
| 250 | # returns with non json error response, we can create new rest class |
| 251 | # for swift. |
| 252 | # Usually RFC2616 says error responses SHOULD contain an explanation. |
| 253 | # The warning is normal for SHOULD/SHOULD NOT case |
| 254 | |
| 255 | # Likely it will cause error |
| 256 | if not body and resp.status >= 400: |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 257 | self.LOG.warning("status >= 400 response with empty body") |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 258 | |
Matthew Treinish | 426326e | 2012-11-30 13:17:00 -0500 | [diff] [blame] | 259 | def request(self, method, url, |
| 260 | headers=None, body=None, depth=0, wait=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 261 | """A simple HTTP request interface.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 262 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 263 | if (self.token is None) or (self.base_url is None): |
| 264 | self._set_auth() |
| 265 | |
Jay Pipes | cd8eaec | 2013-01-16 21:03:48 -0500 | [diff] [blame] | 266 | dscv = self.config.identity.disable_ssl_certificate_validation |
| 267 | self.http_obj = httplib2.Http(disable_ssl_certificate_validation=dscv) |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 268 | if headers is None: |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 269 | headers = {} |
| 270 | headers['X-Auth-Token'] = self.token |
| 271 | |
| 272 | req_url = "%s/%s" % (self.base_url, url) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 273 | self._log_request(method, req_url, headers, body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 274 | resp, resp_body = self.http_obj.request(req_url, method, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 275 | headers=headers, body=body) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 276 | self._log_response(resp, resp_body) |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 277 | self.response_checker(method, url, headers, body, resp, resp_body) |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 278 | |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 279 | self._error_checker(method, url, headers, body, resp, resp_body, depth, |
| 280 | wait) |
| 281 | |
| 282 | return resp, resp_body |
| 283 | |
| 284 | def _error_checker(self, method, url, |
| 285 | headers, body, resp, resp_body, depth=0, wait=None): |
| 286 | |
| 287 | # NOTE(mtreinish): Check for httplib response from glance_http. The |
| 288 | # object can't be used here because importing httplib breaks httplib2. |
| 289 | # If another object from a class not imported were passed here as |
| 290 | # resp this could possibly fail |
| 291 | if str(type(resp)) == "<type 'instance'>": |
| 292 | ctype = resp.getheader('content-type') |
| 293 | else: |
| 294 | try: |
| 295 | ctype = resp['content-type'] |
| 296 | # NOTE(mtreinish): Keystone delete user responses doesn't have a |
| 297 | # content-type header. (They don't have a body) So just pretend it |
| 298 | # is set. |
| 299 | except KeyError: |
| 300 | ctype = 'application/json' |
| 301 | |
| 302 | JSON_ENC = ['application/json; charset=UTF-8', 'application/json', |
| 303 | 'application/json; charset=utf-8'] |
| 304 | # NOTE(mtreinish): This is for compatibility with Glance and swift |
| 305 | # APIs. These are the return content types that Glance api v1 |
| 306 | # (and occasionally swift) are using. |
| 307 | TXT_ENC = ['text/plain; charset=UTF-8', 'text/html; charset=UTF-8', |
| 308 | 'text/plain; charset=utf-8'] |
| 309 | XML_ENC = ['application/xml', 'application/xml; charset=UTF-8'] |
| 310 | |
| 311 | if ctype in JSON_ENC or ctype in XML_ENC: |
| 312 | parse_resp = True |
| 313 | elif ctype in TXT_ENC: |
| 314 | parse_resp = False |
| 315 | else: |
| 316 | raise exceptions.RestClientException(str(resp.status)) |
| 317 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 318 | if resp.status == 401 or resp.status == 403: |
Daryl Walleck | ced8eb8 | 2012-03-19 13:52:37 -0500 | [diff] [blame] | 319 | raise exceptions.Unauthorized() |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 320 | |
| 321 | if resp.status == 404: |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 322 | raise exceptions.NotFound(resp_body) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 323 | |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 324 | if resp.status == 400: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 325 | if parse_resp: |
| 326 | resp_body = self._parse_resp(resp_body) |
David Kranz | 28e35c5 | 2012-07-10 10:14:38 -0400 | [diff] [blame] | 327 | raise exceptions.BadRequest(resp_body) |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 328 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 329 | if resp.status == 409: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 330 | if parse_resp: |
| 331 | resp_body = self._parse_resp(resp_body) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 332 | raise exceptions.Duplicate(resp_body) |
| 333 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 334 | if resp.status == 413: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 335 | if parse_resp: |
| 336 | resp_body = self._parse_resp(resp_body) |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 337 | #Checking whether Absolute/Rate limit |
| 338 | return self.check_over_limit(resp_body, method, url, headers, body, |
| 339 | depth, wait) |
Brian Lamar | 12d9b29 | 2011-12-08 12:41:21 -0500 | [diff] [blame] | 340 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 341 | if resp.status in (500, 501): |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 342 | message = resp_body |
| 343 | if parse_resp: |
| 344 | resp_body = self._parse_resp(resp_body) |
| 345 | #I'm seeing both computeFault and cloudServersFault come back. |
| 346 | #Will file a bug to fix, but leave as is for now. |
| 347 | if 'cloudServersFault' in resp_body: |
| 348 | message = resp_body['cloudServersFault']['message'] |
| 349 | elif 'computeFault' in resp_body: |
| 350 | message = resp_body['computeFault']['message'] |
| 351 | elif 'error' in resp_body: # Keystone errors |
| 352 | message = resp_body['error']['message'] |
| 353 | raise exceptions.IdentityError(message) |
| 354 | elif 'message' in resp_body: |
| 355 | message = resp_body['message'] |
Dan Prince | a4b709c | 2012-10-10 12:27:59 -0400 | [diff] [blame] | 356 | |
Daryl Walleck | f008703 | 2011-12-18 13:37:05 -0600 | [diff] [blame] | 357 | raise exceptions.ComputeFault(message) |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 358 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 359 | if resp.status >= 400: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 360 | if parse_resp: |
| 361 | resp_body = self._parse_resp(resp_body) |
Attila Fazekas | 9652403 | 2013-01-29 19:52:49 +0100 | [diff] [blame] | 362 | raise exceptions.RestClientException(str(resp.status)) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 363 | |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 364 | def check_over_limit(self, resp_body, method, url, |
| 365 | headers, body, depth, wait): |
| 366 | self.is_absolute_limit(resp_body['overLimit']) |
| 367 | return self.is_rate_limit_retry_max_recursion_depth( |
| 368 | resp_body['overLimit'], method, url, headers, |
| 369 | body, depth, wait) |
| 370 | |
| 371 | def is_absolute_limit(self, resp_body): |
| 372 | if 'exceeded' in resp_body['message']: |
| 373 | raise exceptions.OverLimit(resp_body['message']) |
| 374 | else: |
| 375 | return |
| 376 | |
| 377 | def is_rate_limit_retry_max_recursion_depth(self, resp_body, method, |
| 378 | url, headers, body, depth, |
| 379 | wait): |
| 380 | if 'retryAfter' in resp_body: |
| 381 | if depth < MAX_RECURSION_DEPTH: |
| 382 | delay = resp_body['retryAfter'] |
| 383 | time.sleep(int(delay)) |
| 384 | return self.request(method, url, headers=headers, |
| 385 | body=body, |
| 386 | depth=depth + 1, wait=wait) |
| 387 | else: |
| 388 | raise exceptions.RateLimitExceeded( |
| 389 | message=resp_body['overLimitFault']['message'], |
| 390 | details=resp_body['overLimitFault']['details']) |
| 391 | else: |
| 392 | raise exceptions.OverLimit(resp_body['message']) |
| 393 | |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 394 | def wait_for_resource_deletion(self, id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 395 | """Waits for a resource to be deleted.""" |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 396 | start_time = int(time.time()) |
| 397 | while True: |
| 398 | if self.is_resource_deleted(id): |
| 399 | return |
| 400 | if int(time.time()) - start_time >= self.build_timeout: |
| 401 | raise exceptions.TimeoutException |
| 402 | time.sleep(self.build_interval) |
| 403 | |
| 404 | def is_resource_deleted(self, id): |
| 405 | """ |
| 406 | Subclasses override with specific deletion detection. |
| 407 | """ |
Attila Fazekas | d236b4e | 2013-01-26 00:44:12 +0100 | [diff] [blame] | 408 | message = ('"%s" does not implement is_resource_deleted' |
| 409 | % self.__class__.__name__) |
| 410 | raise NotImplementedError(message) |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 411 | |
| 412 | |
| 413 | class RestClientXML(RestClient): |
| 414 | TYPE = "xml" |
| 415 | |
| 416 | def _parse_resp(self, body): |
| 417 | return xml_to_json(etree.fromstring(body)) |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 418 | |
| 419 | def check_over_limit(self, resp_body, method, url, |
| 420 | headers, body, depth, wait): |
| 421 | self.is_absolute_limit(resp_body) |
| 422 | return self.is_rate_limit_retry_max_recursion_depth( |
| 423 | resp_body, method, url, headers, |
| 424 | body, depth, wait) |