Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 4 | # Copyright 2013 IBM Corp. |
Jay Pipes | 3f981df | 2012-03-27 18:59:44 -0400 | [diff] [blame] | 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | # not use this file except in compliance with the License. You may obtain |
| 9 | # a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | # License for the specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 19 | import collections |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 20 | import hashlib |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | import json |
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 | |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 26 | from tempest.common import http |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 27 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 28 | from tempest.openstack.common import log as logging |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 29 | from tempest.services.compute.xml.common import xml_to_json |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 30 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 31 | # redrive rate limited calls at most twice |
| 32 | MAX_RECURSION_DEPTH = 2 |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 33 | TOKEN_CHARS_RE = re.compile('^[-A-Za-z0-9+/=]*$') |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 34 | |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 35 | # All the successful HTTP status codes from RFC 2616 |
| 36 | HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206) |
| 37 | |
Eoghan Glynn | a559897 | 2012-03-01 09:27:17 -0500 | [diff] [blame] | 38 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 39 | class RestClient(object): |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 40 | TYPE = "json" |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 41 | LOG = logging.getLogger(__name__) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 42 | |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 43 | def __init__(self, config, user, password, auth_url, tenant_name=None, |
| 44 | auth_version='v2'): |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 45 | self.config = config |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 46 | self.user = user |
| 47 | self.password = password |
| 48 | self.auth_url = auth_url |
| 49 | self.tenant_name = tenant_name |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 50 | self.auth_version = auth_version |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 51 | |
| 52 | self.service = None |
| 53 | self.token = None |
| 54 | self.base_url = None |
Arata Notsu | 8f44039 | 2013-09-13 16:14:20 +0900 | [diff] [blame] | 55 | self.region = {} |
| 56 | for cfgname in dir(self.config): |
| 57 | # Find all config.FOO.catalog_type and assume FOO is a service. |
| 58 | cfg = getattr(self.config, cfgname) |
| 59 | catalog_type = getattr(cfg, 'catalog_type', None) |
| 60 | if not catalog_type: |
| 61 | continue |
| 62 | service_region = getattr(cfg, 'region', None) |
| 63 | if not service_region: |
| 64 | service_region = self.config.identity.region |
| 65 | self.region[catalog_type] = service_region |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 66 | self.endpoint_url = 'publicURL' |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 67 | self.headers = {'Content-Type': 'application/%s' % self.TYPE, |
| 68 | 'Accept': 'application/%s' % self.TYPE} |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 69 | self.build_interval = config.compute.build_interval |
| 70 | self.build_timeout = config.compute.build_timeout |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 71 | self.general_header_lc = set(('cache-control', 'connection', |
| 72 | 'date', 'pragma', 'trailer', |
| 73 | 'transfer-encoding', 'via', |
| 74 | 'warning')) |
| 75 | self.response_header_lc = set(('accept-ranges', 'age', 'etag', |
| 76 | 'location', 'proxy-authenticate', |
| 77 | 'retry-after', 'server', |
| 78 | 'vary', 'www-authenticate')) |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 79 | dscv = self.config.identity.disable_ssl_certificate_validation |
Mate Lakat | 23a58a3 | 2013-08-23 02:06:22 +0100 | [diff] [blame] | 80 | self.http_obj = http.ClosingHttp( |
| 81 | disable_ssl_certificate_validation=dscv) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 82 | |
DennyZhang | 7be7500 | 2013-09-19 06:55:11 -0500 | [diff] [blame] | 83 | def __str__(self): |
| 84 | STRING_LIMIT = 80 |
| 85 | str_format = ("config:%s, user:%s, password:%s, " |
| 86 | "auth_url:%s, tenant_name:%s, auth_version:%s, " |
| 87 | "service:%s, base_url:%s, region:%s, " |
| 88 | "endpoint_url:%s, build_interval:%s, build_timeout:%s" |
| 89 | "\ntoken:%s..., \nheaders:%s...") |
| 90 | return str_format % (self.config, self.user, self.password, |
| 91 | self.auth_url, self.tenant_name, |
| 92 | self.auth_version, self.service, |
| 93 | self.base_url, self.region, self.endpoint_url, |
| 94 | self.build_interval, self.build_timeout, |
| 95 | str(self.token)[0:STRING_LIMIT], |
| 96 | str(self.headers)[0:STRING_LIMIT]) |
| 97 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 98 | def _set_auth(self): |
| 99 | """ |
| 100 | Sets the token and base_url used in requests based on the strategy type |
| 101 | """ |
| 102 | |
Li Ma | 216550f | 2013-06-12 11:26:08 -0700 | [diff] [blame] | 103 | if self.auth_version == 'v3': |
| 104 | auth_func = self.identity_auth_v3 |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 105 | else: |
Li Ma | 216550f | 2013-06-12 11:26:08 -0700 | [diff] [blame] | 106 | auth_func = self.keystone_auth |
| 107 | |
| 108 | self.token, self.base_url = ( |
| 109 | auth_func(self.user, self.password, self.auth_url, |
| 110 | self.service, self.tenant_name)) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 111 | |
| 112 | def clear_auth(self): |
| 113 | """ |
| 114 | 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] | 115 | will fetch a new token and base_url. |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 116 | """ |
| 117 | |
| 118 | self.token = None |
| 119 | self.base_url = None |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 120 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 121 | def get_auth(self): |
| 122 | """Returns the token of the current request or sets the token if |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 123 | none. |
| 124 | """ |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 125 | |
| 126 | if not self.token: |
| 127 | self._set_auth() |
| 128 | |
| 129 | return self.token |
| 130 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 131 | def basic_auth(self, user, password, auth_url): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 132 | """ |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 133 | Provides authentication for the target API. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 134 | """ |
| 135 | |
| 136 | params = {} |
| 137 | params['headers'] = {'User-Agent': 'Test-Client', 'X-Auth-User': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 138 | 'X-Auth-Key': password} |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 139 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 140 | resp, body = self.http_obj.request(auth_url, 'GET', **params) |
| 141 | try: |
| 142 | return resp['x-auth-token'], resp['x-server-management-url'] |
Matthew Treinish | 05d9fb9 | 2012-12-07 16:14:05 -0500 | [diff] [blame] | 143 | except Exception: |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 144 | raise |
| 145 | |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 146 | def keystone_auth(self, user, password, auth_url, service, tenant_name): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 147 | """ |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 148 | Provides authentication via Keystone using v2 identity API. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 149 | """ |
| 150 | |
Jay Pipes | 7c88eb2 | 2013-01-16 21:32:43 -0500 | [diff] [blame] | 151 | # Normalize URI to ensure /tokens is in it. |
| 152 | if 'tokens' not in auth_url: |
| 153 | auth_url = auth_url.rstrip('/') + '/tokens' |
| 154 | |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 155 | creds = { |
| 156 | 'auth': { |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 157 | 'passwordCredentials': { |
| 158 | 'username': user, |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 159 | 'password': password, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 160 | }, |
Zhongyue Luo | 30a563f | 2012-09-30 23:43:50 +0900 | [diff] [blame] | 161 | 'tenantName': tenant_name, |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 165 | headers = {'Content-Type': 'application/json'} |
| 166 | body = json.dumps(creds) |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 167 | self._log_request('POST', auth_url, headers, body) |
| 168 | resp, resp_body = self.http_obj.request(auth_url, 'POST', |
| 169 | headers=headers, body=body) |
| 170 | self._log_response(resp, resp_body) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 171 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 172 | if resp.status == 200: |
| 173 | try: |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 174 | auth_data = json.loads(resp_body)['access'] |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 175 | token = auth_data['token']['id'] |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 176 | except Exception as e: |
| 177 | print("Failed to obtain token for user: %s" % e) |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 178 | raise |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 179 | |
| 180 | mgmt_url = None |
| 181 | for ep in auth_data['serviceCatalog']: |
Dan Prince | 8527c8a | 2012-12-14 14:00:31 -0500 | [diff] [blame] | 182 | if ep["type"] == service: |
K Jonathan Harker | d6ba4b4 | 2012-12-18 13:50:47 -0800 | [diff] [blame] | 183 | for _ep in ep['endpoints']: |
| 184 | if service in self.region and \ |
| 185 | _ep['region'] == self.region[service]: |
| 186 | mgmt_url = _ep[self.endpoint_url] |
| 187 | if not mgmt_url: |
| 188 | mgmt_url = ep['endpoints'][0][self.endpoint_url] |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 189 | break |
| 190 | |
Zhongyue Luo | e471d6e | 2012-09-17 17:02:43 +0800 | [diff] [blame] | 191 | if mgmt_url is None: |
Adam Gandelman | e2d46b4 | 2012-01-03 17:40:44 -0800 | [diff] [blame] | 192 | raise exceptions.EndpointNotFound(service) |
| 193 | |
Rohit Karajgi | d2a28af | 2012-05-23 03:44:59 -0700 | [diff] [blame] | 194 | return token, mgmt_url |
| 195 | |
Jay Pipes | 7f75763 | 2011-12-02 15:53:32 -0500 | [diff] [blame] | 196 | elif resp.status == 401: |
Daryl Walleck | a22f57b | 2012-03-20 16:52:07 -0500 | [diff] [blame] | 197 | raise exceptions.AuthenticationFailure(user=user, |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 198 | password=password, |
| 199 | tenant=tenant_name) |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 200 | raise exceptions.IdentityError('Unexpected status code {0}'.format( |
| 201 | resp.status)) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 202 | |
Brant Knudson | c7ca334 | 2013-03-28 21:08:50 -0500 | [diff] [blame] | 203 | def identity_auth_v3(self, user, password, auth_url, service, |
| 204 | project_name, domain_id='default'): |
| 205 | """Provides authentication using Identity API v3.""" |
| 206 | |
| 207 | req_url = auth_url.rstrip('/') + '/auth/tokens' |
| 208 | |
| 209 | creds = { |
| 210 | "auth": { |
| 211 | "identity": { |
| 212 | "methods": ["password"], |
| 213 | "password": { |
| 214 | "user": { |
| 215 | "name": user, "password": password, |
| 216 | "domain": {"id": domain_id} |
| 217 | } |
| 218 | } |
| 219 | }, |
| 220 | "scope": { |
| 221 | "project": { |
| 222 | "domain": {"id": domain_id}, |
| 223 | "name": project_name |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | headers = {'Content-Type': 'application/json'} |
| 230 | body = json.dumps(creds) |
| 231 | resp, body = self.http_obj.request(req_url, 'POST', |
| 232 | headers=headers, body=body) |
| 233 | |
| 234 | if resp.status == 201: |
| 235 | try: |
| 236 | token = resp['x-subject-token'] |
| 237 | except Exception: |
| 238 | self.LOG.exception("Failed to obtain token using V3" |
| 239 | " authentication (auth URL is '%s')" % |
| 240 | req_url) |
| 241 | raise |
| 242 | |
| 243 | catalog = json.loads(body)['token']['catalog'] |
| 244 | |
| 245 | mgmt_url = None |
| 246 | for service_info in catalog: |
| 247 | if service_info['type'] != service: |
| 248 | continue # this isn't the entry for us. |
| 249 | |
| 250 | endpoints = service_info['endpoints'] |
| 251 | |
| 252 | # Look for an endpoint in the region if configured. |
| 253 | if service in self.region: |
| 254 | region = self.region[service] |
| 255 | |
| 256 | for ep in endpoints: |
| 257 | if ep['region'] != region: |
| 258 | continue |
| 259 | |
| 260 | mgmt_url = ep['url'] |
| 261 | # FIXME(blk-u): this isn't handling endpoint type |
| 262 | # (public, internal, admin). |
| 263 | break |
| 264 | |
| 265 | if not mgmt_url: |
| 266 | # Didn't find endpoint for region, use the first. |
| 267 | |
| 268 | ep = endpoints[0] |
| 269 | mgmt_url = ep['url'] |
| 270 | # FIXME(blk-u): this isn't handling endpoint type |
| 271 | # (public, internal, admin). |
| 272 | |
| 273 | break |
| 274 | |
| 275 | return token, mgmt_url |
| 276 | |
| 277 | elif resp.status == 401: |
| 278 | raise exceptions.AuthenticationFailure(user=user, |
| 279 | password=password) |
| 280 | else: |
| 281 | self.LOG.error("Failed to obtain token using V3 authentication" |
| 282 | " (auth URL is '%s'), the response status is %s" % |
| 283 | (req_url, resp.status)) |
| 284 | raise exceptions.AuthenticationFailure(user=user, |
| 285 | password=password) |
| 286 | |
Attila Fazekas | 54a4286 | 2013-07-28 22:31:06 +0200 | [diff] [blame] | 287 | def expected_success(self, expected_code, read_code): |
| 288 | assert_msg = ("This function only allowed to use for HTTP status" |
| 289 | "codes which explicitly defined in the RFC 2616. {0}" |
| 290 | " is not a defined Success Code!").format(expected_code) |
| 291 | assert expected_code in HTTP_SUCCESS, assert_msg |
| 292 | |
| 293 | # NOTE(afazekas): the http status code above 400 is processed by |
| 294 | # the _error_checker method |
| 295 | if read_code < 400 and read_code != expected_code: |
| 296 | pattern = """Unexpected http success status code {0}, |
| 297 | The expected status code is {1}""" |
| 298 | details = pattern.format(read_code, expected_code) |
| 299 | raise exceptions.InvalidHttpSuccessCode(details) |
| 300 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 301 | def post(self, url, body, headers): |
| 302 | return self.request('POST', url, headers, body) |
| 303 | |
Attila Fazekas | b8aa759 | 2013-01-26 01:25:45 +0100 | [diff] [blame] | 304 | def get(self, url, headers=None): |
| 305 | return self.request('GET', url, headers) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 306 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 307 | def delete(self, url, headers=None): |
| 308 | return self.request('DELETE', url, headers) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 309 | |
rajalakshmi-ganesan | ab42672 | 2013-02-08 15:49:15 +0530 | [diff] [blame] | 310 | def patch(self, url, body, headers): |
| 311 | return self.request('PATCH', url, headers, body) |
| 312 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 313 | def put(self, url, body, headers): |
| 314 | return self.request('PUT', url, headers, body) |
| 315 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 316 | def head(self, url, headers=None): |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 317 | return self.request('HEAD', url, headers) |
| 318 | |
| 319 | def copy(self, url, headers=None): |
| 320 | return self.request('COPY', url, headers) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 321 | |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 322 | def get_versions(self): |
| 323 | resp, body = self.get('') |
| 324 | body = self._parse_resp(body) |
| 325 | body = body['versions'] |
| 326 | versions = map(lambda x: x['id'], body) |
| 327 | return resp, versions |
| 328 | |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 329 | def _log_request(self, method, req_url, headers, body): |
| 330 | self.LOG.info('Request: ' + method + ' ' + req_url) |
| 331 | if headers: |
| 332 | print_headers = headers |
| 333 | if 'X-Auth-Token' in headers and headers['X-Auth-Token']: |
| 334 | token = headers['X-Auth-Token'] |
| 335 | if len(token) > 64 and TOKEN_CHARS_RE.match(token): |
| 336 | print_headers = headers.copy() |
| 337 | print_headers['X-Auth-Token'] = "<Token omitted>" |
| 338 | self.LOG.debug('Request Headers: ' + str(print_headers)) |
| 339 | if body: |
| 340 | str_body = str(body) |
| 341 | length = len(str_body) |
| 342 | self.LOG.debug('Request Body: ' + str_body[:2048]) |
| 343 | if length >= 2048: |
| 344 | self.LOG.debug("Large body (%d) md5 summary: %s", length, |
| 345 | hashlib.md5(str_body).hexdigest()) |
| 346 | |
| 347 | def _log_response(self, resp, resp_body): |
| 348 | status = resp['status'] |
| 349 | self.LOG.info("Response Status: " + status) |
| 350 | headers = resp.copy() |
| 351 | del headers['status'] |
Matthew Treinish | e542391 | 2013-08-13 18:07:31 -0400 | [diff] [blame] | 352 | if headers.get('x-compute-request-id'): |
| 353 | self.LOG.info("Nova request id: %s" % |
| 354 | headers.pop('x-compute-request-id')) |
| 355 | elif headers.get('x-openstack-request-id'): |
| 356 | self.LOG.info("Glance request id %s" % |
| 357 | headers.pop('x-openstack-request-id')) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 358 | if len(headers): |
| 359 | self.LOG.debug('Response Headers: ' + str(headers)) |
| 360 | if resp_body: |
| 361 | str_body = str(resp_body) |
| 362 | length = len(str_body) |
| 363 | self.LOG.debug('Response Body: ' + str_body[:2048]) |
| 364 | if length >= 2048: |
| 365 | self.LOG.debug("Large body (%d) md5 summary: %s", length, |
| 366 | hashlib.md5(str_body).hexdigest()) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 367 | |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 368 | def _parse_resp(self, body): |
| 369 | return json.loads(body) |
| 370 | |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 371 | def response_checker(self, method, url, headers, body, resp, resp_body): |
| 372 | if (resp.status in set((204, 205, 304)) or resp.status < 200 or |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 373 | method.upper() == 'HEAD') and resp_body: |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 374 | raise exceptions.ResponseWithNonEmptyBody(status=resp.status) |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 375 | # NOTE(afazekas): |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 376 | # If the HTTP Status Code is 205 |
| 377 | # 'The response MUST NOT include an entity.' |
| 378 | # A HTTP entity has an entity-body and an 'entity-header'. |
| 379 | # In the HTTP response specification (Section 6) the 'entity-header' |
| 380 | # 'generic-header' and 'response-header' are in OR relation. |
| 381 | # All headers not in the above two group are considered as entity |
| 382 | # header in every interpretation. |
| 383 | |
| 384 | if (resp.status == 205 and |
| 385 | 0 != len(set(resp.keys()) - set(('status',)) - |
| 386 | self.response_header_lc - self.general_header_lc)): |
| 387 | raise exceptions.ResponseWithEntity() |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 388 | # NOTE(afazekas) |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 389 | # Now the swift sometimes (delete not empty container) |
| 390 | # returns with non json error response, we can create new rest class |
| 391 | # for swift. |
| 392 | # Usually RFC2616 says error responses SHOULD contain an explanation. |
| 393 | # The warning is normal for SHOULD/SHOULD NOT case |
| 394 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 395 | # Likely it will cause an error |
| 396 | if not resp_body and resp.status >= 400: |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 397 | self.LOG.warning("status >= 400 response with empty body") |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 398 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 399 | def _request(self, method, url, |
| 400 | headers=None, body=None): |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 401 | """A simple HTTP request interface.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 402 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 403 | req_url = "%s/%s" % (self.base_url, url) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 404 | self._log_request(method, req_url, headers, body) |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 405 | resp, resp_body = self.http_obj.request(req_url, method, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 406 | headers=headers, body=body) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame] | 407 | self._log_response(resp, resp_body) |
Attila Fazekas | 836e478 | 2013-01-29 15:40:13 +0100 | [diff] [blame] | 408 | self.response_checker(method, url, headers, body, resp, resp_body) |
Attila Fazekas | 72c7a5f | 2012-12-03 17:17:23 +0100 | [diff] [blame] | 409 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 410 | return resp, resp_body |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 411 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 412 | def request(self, method, url, |
| 413 | headers=None, body=None): |
| 414 | retry = 0 |
| 415 | if (self.token is None) or (self.base_url is None): |
| 416 | self._set_auth() |
| 417 | |
| 418 | if headers is None: |
| 419 | headers = {} |
| 420 | headers['X-Auth-Token'] = self.token |
| 421 | |
| 422 | resp, resp_body = self._request(method, url, |
| 423 | headers=headers, body=body) |
| 424 | |
| 425 | while (resp.status == 413 and |
| 426 | 'retry-after' in resp and |
| 427 | not self.is_absolute_limit( |
| 428 | resp, self._parse_resp(resp_body)) and |
| 429 | retry < MAX_RECURSION_DEPTH): |
| 430 | retry += 1 |
| 431 | delay = int(resp['retry-after']) |
| 432 | time.sleep(delay) |
| 433 | resp, resp_body = self._request(method, url, |
| 434 | headers=headers, body=body) |
| 435 | self._error_checker(method, url, headers, body, |
| 436 | resp, resp_body) |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 437 | return resp, resp_body |
| 438 | |
| 439 | def _error_checker(self, method, url, |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 440 | headers, body, resp, resp_body): |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 441 | |
| 442 | # NOTE(mtreinish): Check for httplib response from glance_http. The |
| 443 | # object can't be used here because importing httplib breaks httplib2. |
| 444 | # If another object from a class not imported were passed here as |
| 445 | # resp this could possibly fail |
| 446 | if str(type(resp)) == "<type 'instance'>": |
| 447 | ctype = resp.getheader('content-type') |
| 448 | else: |
| 449 | try: |
| 450 | ctype = resp['content-type'] |
| 451 | # NOTE(mtreinish): Keystone delete user responses doesn't have a |
| 452 | # content-type header. (They don't have a body) So just pretend it |
| 453 | # is set. |
| 454 | except KeyError: |
| 455 | ctype = 'application/json' |
| 456 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 457 | # It is not an error response |
| 458 | if resp.status < 400: |
| 459 | return |
| 460 | |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 461 | JSON_ENC = ['application/json; charset=UTF-8', 'application/json', |
| 462 | 'application/json; charset=utf-8'] |
| 463 | # NOTE(mtreinish): This is for compatibility with Glance and swift |
| 464 | # APIs. These are the return content types that Glance api v1 |
| 465 | # (and occasionally swift) are using. |
Joe H. Rahme | 2eccc33 | 2013-08-29 15:50:35 +0200 | [diff] [blame] | 466 | TXT_ENC = ['text/plain', 'text/plain; charset=UTF-8', |
| 467 | 'text/html; charset=UTF-8', 'text/plain; charset=utf-8'] |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 468 | XML_ENC = ['application/xml', 'application/xml; charset=UTF-8'] |
| 469 | |
| 470 | if ctype in JSON_ENC or ctype in XML_ENC: |
| 471 | parse_resp = True |
| 472 | elif ctype in TXT_ENC: |
| 473 | parse_resp = False |
| 474 | else: |
| 475 | raise exceptions.RestClientException(str(resp.status)) |
| 476 | |
Rohit Karajgi | 6b1e154 | 2012-05-14 05:55:54 -0700 | [diff] [blame] | 477 | if resp.status == 401 or resp.status == 403: |
Daryl Walleck | ced8eb8 | 2012-03-19 13:52:37 -0500 | [diff] [blame] | 478 | raise exceptions.Unauthorized() |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 479 | |
| 480 | if resp.status == 404: |
Daryl Walleck | 8a707db | 2012-01-25 00:46:24 -0600 | [diff] [blame] | 481 | raise exceptions.NotFound(resp_body) |
Jay Pipes | 5135bfc | 2012-01-05 15:46:49 -0500 | [diff] [blame] | 482 | |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 483 | if resp.status == 400: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 484 | if parse_resp: |
| 485 | resp_body = self._parse_resp(resp_body) |
David Kranz | 28e35c5 | 2012-07-10 10:14:38 -0400 | [diff] [blame] | 486 | raise exceptions.BadRequest(resp_body) |
Daryl Walleck | adea1fa | 2011-11-15 18:36:39 -0600 | [diff] [blame] | 487 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 488 | if resp.status == 409: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 489 | if parse_resp: |
| 490 | resp_body = self._parse_resp(resp_body) |
Anju5 | c3e510c | 2013-10-18 06:40:29 +0530 | [diff] [blame] | 491 | raise exceptions.Conflict(resp_body) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 492 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 493 | if resp.status == 413: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 494 | if parse_resp: |
| 495 | resp_body = self._parse_resp(resp_body) |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 496 | if self.is_absolute_limit(resp, resp_body): |
| 497 | raise exceptions.OverLimit(resp_body) |
| 498 | else: |
| 499 | raise exceptions.RateLimitExceeded(resp_body) |
Brian Lamar | 12d9b29 | 2011-12-08 12:41:21 -0500 | [diff] [blame] | 500 | |
Wangpan | a9b54c6 | 2013-02-28 11:04:32 +0800 | [diff] [blame] | 501 | if resp.status == 422: |
| 502 | if parse_resp: |
| 503 | resp_body = self._parse_resp(resp_body) |
| 504 | raise exceptions.UnprocessableEntity(resp_body) |
| 505 | |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 506 | if resp.status in (500, 501): |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 507 | message = resp_body |
| 508 | if parse_resp: |
| 509 | resp_body = self._parse_resp(resp_body) |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 510 | # I'm seeing both computeFault and cloudServersFault come back. |
| 511 | # Will file a bug to fix, but leave as is for now. |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 512 | if 'cloudServersFault' in resp_body: |
| 513 | message = resp_body['cloudServersFault']['message'] |
| 514 | elif 'computeFault' in resp_body: |
| 515 | message = resp_body['computeFault']['message'] |
| 516 | elif 'error' in resp_body: # Keystone errors |
| 517 | message = resp_body['error']['message'] |
| 518 | raise exceptions.IdentityError(message) |
| 519 | elif 'message' in resp_body: |
| 520 | message = resp_body['message'] |
Dan Prince | a4b709c | 2012-10-10 12:27:59 -0400 | [diff] [blame] | 521 | |
Anju5 | c3e510c | 2013-10-18 06:40:29 +0530 | [diff] [blame] | 522 | raise exceptions.ServerFault(message) |
Daryl Walleck | ed8bef3 | 2011-12-05 23:02:08 -0600 | [diff] [blame] | 523 | |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 524 | if resp.status >= 400: |
Matthew Treinish | 7e5a3ec | 2013-02-08 13:53:58 -0500 | [diff] [blame] | 525 | if parse_resp: |
| 526 | resp_body = self._parse_resp(resp_body) |
Attila Fazekas | 9652403 | 2013-01-29 19:52:49 +0100 | [diff] [blame] | 527 | raise exceptions.RestClientException(str(resp.status)) |
David Kranz | 5a23d86 | 2012-02-14 09:48:55 -0500 | [diff] [blame] | 528 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 529 | def is_absolute_limit(self, resp, resp_body): |
| 530 | if (not isinstance(resp_body, collections.Mapping) or |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 531 | 'retry-after' not in resp): |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 532 | return True |
| 533 | over_limit = resp_body.get('overLimit', None) |
| 534 | if not over_limit: |
| 535 | return True |
| 536 | return 'exceed' in over_limit.get('message', 'blabla') |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 537 | |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 538 | def wait_for_resource_deletion(self, id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 539 | """Waits for a resource to be deleted.""" |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 540 | start_time = int(time.time()) |
| 541 | while True: |
| 542 | if self.is_resource_deleted(id): |
| 543 | return |
| 544 | if int(time.time()) - start_time >= self.build_timeout: |
| 545 | raise exceptions.TimeoutException |
| 546 | time.sleep(self.build_interval) |
| 547 | |
| 548 | def is_resource_deleted(self, id): |
| 549 | """ |
| 550 | Subclasses override with specific deletion detection. |
| 551 | """ |
Attila Fazekas | d236b4e | 2013-01-26 00:44:12 +0100 | [diff] [blame] | 552 | message = ('"%s" does not implement is_resource_deleted' |
| 553 | % self.__class__.__name__) |
| 554 | raise NotImplementedError(message) |
Dan Smith | ba6cb16 | 2012-08-14 07:22:42 -0700 | [diff] [blame] | 555 | |
| 556 | |
| 557 | class RestClientXML(RestClient): |
| 558 | TYPE = "xml" |
| 559 | |
| 560 | def _parse_resp(self, body): |
| 561 | return xml_to_json(etree.fromstring(body)) |
rajalakshmi-ganesan | 0275a0d | 2013-01-11 18:26:05 +0530 | [diff] [blame] | 562 | |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 563 | def is_absolute_limit(self, resp, resp_body): |
| 564 | if (not isinstance(resp_body, collections.Mapping) or |
Pavel Sedlák | e267eba | 2013-04-03 15:56:36 +0200 | [diff] [blame] | 565 | 'retry-after' not in resp): |
Attila Fazekas | 55f6d8c | 2013-03-10 10:32:54 +0100 | [diff] [blame] | 566 | return True |
| 567 | return 'exceed' in resp_body.get('message', 'blabla') |