def _check_keystone_versions(url): try: httpclient = client.HTTPClient(user=None, password=None, projectid=None, auth_url=None) resp, body = httpclient.request(url, "GET", headers={'Accept': 'application/json'}) if resp.status in (200, 204): # in some cases we get No Content try: print "Keystone found at %s" % url if 'version' in body: version = body['version'] # Stable/diablo incorrect format _display_version_info(version, url) return True if 'versions' in body: # Correct format for version in body['versions']['values']: _display_version_info(version, url) return True print "Unrecognized response from %s" % url except KeyError: raise exceptions.AuthorizationFailure() elif resp.status == 305: return _check_keystone_versions(resp['location']) else: raise exceptions.from_response(resp, body) except: return False
def request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['body'] = json.dumps(kwargs['body']) self.http_log_req(args, kwargs) resp, body = super(HTTPClient, self).request(*args, **kwargs) self.http_log_resp(resp, body) if body: # NOTE(alaski): Because force_exceptions_to_status_code=True # httplib2 returns a connection refused event as a 400 response. # To determine if it is a bad request or refused connection we need # to check the body. httplib2 tests check for 'Connection refused' # or 'actively refused' in the body, so that's what we'll do. if resp.status == 400: if 'Connection refused' in body or 'actively refused' in body: raise exceptions.ConnectionRefused(body) try: body = json.loads(body) except ValueError: pass else: body = None if resp.status >= 400: raise exceptions.from_response(resp, body) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) kwargs['headers']['wrs-header'] = 'true' # NOTE(dbelova): osprofiler_web.get_trace_id_headers does not add any # headers in case if osprofiler is not initialized. if osprofiler_web: kwargs['headers'].update(osprofiler_web.get_trace_id_headers()) # NOTE(jamielennox): The standard call raises errors from # keystoneauth1, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('user_agent', 'python-novaclient') kwargs.setdefault('auth', self.auth) kwargs.setdefault('authenticated', False) try: kwargs['json'] = kwargs.pop('body') except KeyError: pass headers = kwargs.setdefault('headers', {}) headers.setdefault('Accept', 'application/json') endpoint_filter = kwargs.setdefault('endpoint_filter', {}) endpoint_filter.setdefault('interface', self.interface) endpoint_filter.setdefault('service_type', self.service_type) endpoint_filter.setdefault('region_name', self.region_name) resp = self.session.request(url, method, raise_exc=False, **kwargs) body = None if resp.text: try: body = resp.json() except ValueError: pass if resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _extract_service_catalog(self, url, resp, body, extract_token=True): """See what the auth service told us and process the response. We may get redirected to another site, fail or actually get back a service catalog with a token and our endpoints.""" if resp.status == 200: # content must always present try: self.auth_url = url self.service_catalog = \ service_catalog.ServiceCatalog(body) if extract_token: self.auth_token = self.service_catalog.get_token() self.management_url = self.service_catalog.url_for( attr='region', filter_value=self.region_name) return None except KeyError: raise exceptions.AuthorizationFailure() except exceptions.EndpointNotFound: print "Could not find any suitable endpoint. Correct region?" raise elif resp.status == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
def _extract_service_catalog(self, url, resp, body, extract_token=True): """See what the auth service told us and process the response. We may get redirected to another site, fail or actually get back a service catalog with a token and our endpoints.""" if resp.status == 200: # content must always present try: self.auth_url = url self.service_catalog = \ service_catalog.ServiceCatalog(body) if extract_token: self.auth_token = self.service_catalog.get_token() self.management_url = self.service_catalog.url_for( attr='region', filter_value=self.region_name, endpoint_type=self.endpoint_name) return None except KeyError: raise exceptions.AuthorizationFailure() except exceptions.EndpointNotFound: print "Could not find any suitable endpoint. Correct region?" raise elif resp.status == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
def _extract_service_catalog(self, url, resp, body, extract_token=True): """See what the auth service told us and process the response. We may get redirected to another site, fail or actually get back a service catalog with a token and our endpoints. """ # content must always present if resp.status_code == 200 or resp.status_code == 201: try: self.auth_url = url self.service_catalog = \ service_catalog.ServiceCatalog(body) if extract_token: self.auth_token = self.service_catalog.get_token() self.tenant_id = self.service_catalog.get_tenant_id() self.management_url = self.get_service_url(self.service_type) return None except exceptions.AmbiguousEndpoints: print(_("Found more than one valid endpoint. Use a more " "restrictive filter")) raise except KeyError: raise exceptions.AuthorizationFailure() except exceptions.EndpointNotFound: print(_("Could not find any suitable endpoint. Correct " "region?")) raise elif resp.status_code == 305: return resp.headers['location'] else: raise exceptions.from_response(resp, body, url)
def _extract_service_catalog(self, url, resp, body, extract_token=True): """See what the auth service told us and process the response. We may get redirected to another site, fail or actually get back a service catalog with a token and our endpoints. """ # content must always present if resp.status_code == 200 or resp.status_code == 201: try: self.auth_url = url self.service_catalog = \ service_catalog.ServiceCatalog(body) if extract_token: self.auth_token = self.service_catalog.get_token() self.tenant_id = self.service_catalog.get_tenant_id() self.management_url = self.get_service_url(self.service_type) return None except exceptions.AmbiguousEndpoints: print( _("Found more than one valid endpoint. Use a more " "restrictive filter")) raise except KeyError: raise exceptions.AuthorizationFailure() except exceptions.EndpointNotFound: print( _("Could not find any suitable endpoint. Correct " "region?")) raise elif resp.status_code == 305: return resp.headers['location'] else: raise exceptions.from_response(resp, body, url)
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) # NOTE(dbelova): osprofiler_web.get_trace_id_headers does not add any # headers in case if osprofiler is not initialized. if osprofiler_web: kwargs['headers'].update(osprofiler_web.get_trace_id_headers()) # NOTE(jamielennox): The standard call raises errors from # keystoneauth1, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _extract_service_catalog(self, url, resp, body, extract_token=True): """See what the auth service told us and process the response. We may get redirected to another site, fail or actually get back a service catalog with a token and our endpoints.""" if resp.status_code == 200: # content must always present try: self.auth_url = url self.service_catalog = service_catalog.ServiceCatalog(body) if extract_token: self.auth_token = self.service_catalog.get_token() management_url = self.service_catalog.url_for( attr="region", filter_value=self.region_name, endpoint_type=self.endpoint_type, service_type=self.service_type, service_name=self.service_name, volume_service_name=self.volume_service_name, ) self.management_url = management_url.rstrip("/") return None except exceptions.AmbiguousEndpoints: print "Found more than one valid endpoint. Use a more " "restrictive filter" raise except KeyError: raise exceptions.AuthorizationFailure() except exceptions.EndpointNotFound: print "Could not find any suitable endpoint. Correct region?" raise elif resp.status_code == 305: return resp.headers["location"] else: raise exceptions.from_response(resp, body)
def request(self, url, method, **kwargs): kwargs.setdefault("headers", kwargs.get("headers", {})) kwargs["headers"]["User-Agent"] = self.USER_AGENT kwargs["headers"]["Accept"] = "application/json" if "body" in kwargs: kwargs["headers"]["Content-Type"] = "application/json" kwargs["data"] = json.dumps(kwargs["body"]) del kwargs["body"] self.http_log_req((url, method), kwargs) resp = requests.request(method, url, verify=self.verify_cert, config=self.requests_config, **kwargs) self.http_log_resp(resp) if resp.text: # TODO(dtroyer): verify the note below in a requests context # NOTE(alaski): Because force_exceptions_to_status_code=True # httplib2 returns a connection refused event as a 400 response. # To determine if it is a bad request or refused connection we need # to check the body. httplib2 tests check for 'Connection refused' # or 'actively refused' in the body, so that's what we'll do. if resp.status_code == 400: if "Connection refused" in resp.text or "actively refused" in resp.text: raise exceptions.ConnectionRefused(resp.text) try: body = json.loads(resp.text) except ValueError: pass body = None else: body = None if resp.status_code >= 400: raise exceptions.from_response(resp, body) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['data'] = json.dumps(kwargs['body']) del kwargs['body'] api_versions.update_headers(kwargs["headers"], self.api_version) if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) kwargs['verify'] = self.verify_cert self.http_log_req(method, url, kwargs) request_func = requests.request session = self._get_session(url) if session: request_func = session.request resp = request_func( method, url, **kwargs) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) self.http_log_resp(resp) if resp.text: # TODO(dtroyer): verify the note below in a requests context # NOTE(alaski): Because force_exceptions_to_status_code=True # httplib2 returns a connection refused event as a 400 response. # To determine if it is a bad request or refused connection we need # to check the body. httplib2 tests check for 'Connection refused' # or 'actively refused' in the body, so that's what we'll do. if resp.status_code == 400: if ('Connection refused' in resp.text or 'actively refused' in resp.text): raise exceptions.ConnectionRefused(resp.text) try: body = json.loads(resp.text) except ValueError: body = None else: body = None self.last_request_id = (resp.headers.get('x-openstack-request-id') if resp.headers else None) if resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['data'] = json.dumps(kwargs.pop('body')) api_versions.update_headers(kwargs["headers"], self.api_version) if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) kwargs['verify'] = self.verify_cert self.http_log_req(method, url, kwargs) request_func = requests.request session = self._get_session(url) if session: request_func = session.request resp = request_func( method, url, **kwargs) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) self.http_log_resp(resp) if resp.text: # TODO(dtroyer): verify the note below in a requests context # NOTE(alaski): Because force_exceptions_to_status_code=True # httplib2 returns a connection refused event as a 400 response. # To determine if it is a bad request or refused connection we need # to check the body. httplib2 tests check for 'Connection refused' # or 'actively refused' in the body, so that's what we'll do. if resp.status_code == 400: if ('Connection refused' in resp.text or 'actively refused' in resp.text): raise exceptions.ConnectionRefused(resp.text) try: body = json.loads(resp.text) except ValueError: body = None else: body = None self.last_request_id = (resp.headers.get('x-openstack-request-id') if resp.headers else None) if resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['data'] = json.dumps(kwargs['body']) del kwargs['body'] if self.timeout is not None: kwargs.setdefault('timeout', self.timeout) kwargs['verify'] = self.verify_cert self.http_log_req(method, url, kwargs) # print("after http log") #print("method :%s",kwargs) request_func = requests.request session = self._get_session(url) if session: request_func = session.request # print("before req func ") resp = request_func( method, url, **kwargs) self.http_log_resp(resp) # print("after log resp") if resp.text: # TODO(dtroyer): verify the note below in a requests context # NOTE(alaski): Because force_exceptions_to_status_code=True # httplib2 returns a connection refused event as a 400 response. # To determine if it is a bad request or refused connection we need # to check the body. httplib2 tests check for 'Connection refused' # or 'actively refused' in the body, so that's what we'll do. if resp.status_code == 400: if ('Connection refused' in resp.text or 'actively refused' in resp.text): raise exceptions.ConnectionRefused(resp.text) try: body = json.loads(resp.text) except ValueError: body = None else: body = None if resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): # NOTE(jamielennox): The standard call raises errors from # keystoneclient, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): # NOTE(jamielennox): The standard call raises errors from # keystoneclient, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _test_from_response(self, body, expected_message): data = { 'status_code': 404, 'headers': { 'content-type': 'application/json', 'x-openstack-request-id': ( 'req-d9df03b0-4150-4b53-8157-7560ccf39f75'), } } response = test_utils.TestResponse(data) fake_url = 'http://localhost:8774/v2.1/fake/flavors/test' error = exceptions.from_response(response, body, fake_url, 'GET') self.assertIsInstance(error, exceptions.NotFound) self.assertEqual(expected_message, error.message)
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) # NOTE(jamielennox): The standard call raises errors from # keystoneclient, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) # NOTE(jamielennox): The standard call raises errors from # keystoneclient, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) api_versions.check_headers(resp, self.api_version) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _v1_auth(self, url): headers = {'X-Auth-User': self.user, 'X-Auth-Key': self.apikey} if self.projectid: headers['X-Auth-Project-Id'] = self.projectid resp, body = self.request(url, 'GET', headers=headers) if resp.status in (200, 204): # in some cases we get No Content try: self.management_url = resp['x-server-management-url'] self.auth_token = resp['x-auth-token'] self.auth_url = url except KeyError: raise exceptions.AuthorizationFailure() elif resp.status == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) # NOTE(jamielennox): The standard call raises errors from # keystoneauth1, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def request(self, url, method, **kwargs): # NOTE(jamielennox): The standard call raises errors from # keystoneclient, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) start_time = time.time() resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) end_time = time.time() self.times.append(('%s %s' % (method, url), start_time, end_time)) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _v1_auth(self, url): if self.proxy_token: raise exceptions.NoTokenLookupException() headers = {'X-Auth-User': self.user, 'X-Auth-Key': self.password} if self.projectid: headers['X-Auth-Project-Id'] = self.projectid resp, body = self.request(url, 'GET', headers=headers) if resp.status in (200, 204): # in some cases we get No Content try: self.management_url = resp['x-server-management-url'] self.auth_token = resp['x-auth-token'] self.auth_url = url except KeyError: raise exceptions.AuthorizationFailure() elif resp.status == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
def _v1_auth(self, url): if self.proxy_token: raise exceptions.NoTokenLookupException() headers = {'X-Auth-User': self.user, 'X-Auth-Key': self.password} if self.projectid: headers['X-Auth-Project-Id'] = self.projectid resp, body = self._time_request(url, 'GET', headers=headers) if resp.status_code in (200, 204): # in some cases we get No Content try: mgmt_header = 'x-server-management-url' self.management_url = resp.headers[mgmt_header].rstrip('/') self.auth_token = resp.headers['x-auth-token'] self.auth_url = url except (KeyError, TypeError): raise exceptions.AuthorizationFailure() elif resp.status_code == 305: return resp.headers['location'] else: raise exceptions.from_response(resp, body, url)
def _v1_auth(self, url): if self.proxy_token: raise exceptions.NoTokenLookupException() headers = {'X-Auth-User': self.user, 'X-Auth-Key': self._get_password()} if self.projectid: headers['X-Auth-Project-Id'] = self.projectid resp, body = self._time_request(url, 'GET', headers=headers) if resp.status_code in (200, 204): # in some cases we get No Content try: mgmt_header = 'x-server-management-url' self.management_url = resp.headers[mgmt_header].rstrip('/') self.auth_token = resp.headers['x-auth-token'] self.auth_url = url except (KeyError, TypeError): raise exceptions.AuthorizationFailure() elif resp.status_code == 305: return resp.headers['location'] else: raise exceptions.from_response(resp, body, url)
def request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = 'application/json' if 'body' in kwargs: kwargs['headers']['Content-Type'] = 'application/json' kwargs['body'] = json.dumps(kwargs['body']) resp, body = super(HTTPClient, self).request(*args, **kwargs) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError: pass else: body = None if resp.status >= 400: raise exceptions.from_response(resp, body) return resp, body
def request(self, url, method, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) api_versions.update_headers(kwargs["headers"], self.api_version) # NOTE(jamielennox): The standard call raises errors from # keystoneauth1, where we need to raise the novaclient errors. raise_exc = kwargs.pop('raise_exc', True) with utils.record_time(self.times, self.timings, method, url): resp, body = super(SessionClient, self).request(url, method, raise_exc=False, **kwargs) # if service name is None then use service_type for logging service = self.service_name or self.service_type _log_request_id(self.logger, resp, service) # TODO(andreykurilin): uncomment this line, when we will be able to # check only nova-related calls # api_versions.check_headers(resp, self.api_version) if raise_exc and resp.status_code >= 400: raise exceptions.from_response(resp, body, url, method) return resp, body
def _v2_auth(self, url): body = {"passwordCredentials": {"username": self.user, "password": self.apikey}} if self.projectid: body['passwordCredentials']['tenantId'] = self.projectid token_url = urlparse.urljoin(url, "tokens") resp, body = self.request(token_url, "POST", body=body) if resp.status == 200: # content must always present try: self.auth_url = url self.service_catalog = \ service_catalog.ServiceCatalog(body) self.auth_token = self.service_catalog.token.id self.management_url = self.service_catalog.url_for('nova', 'public') except KeyError: raise exceptions.AuthorizationFailure() elif resp.status == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
def fake_exception(status_code=404, message=None, details=None): resp = mock.Mock() resp.status_code = status_code resp.headers = None body = {"error": {"message": message, "details": details}} return nova_exceptions.from_response(resp, body, None)
kwargs['body'] = json.dumps(kwargs['body']) resp, body = super(HTTPClient, self).request(*args, **kwargs) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError, e: pass else: body = None if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body def _cs_request(self, url, method, **kwargs): if not self.management_url: self.authenticate() # Perform the request once. If we get a 401 back then it # might be because the auth token expired, so try to # re-authenticate and try again. If it still fails, bail. try: kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token if self.projectid: kwargs['headers']['X-Auth-Project-Id'] = self.projectid
def fake_exception(status_code=404, message=None, details=None): resp = mock.Mock() resp.status_code = status_code resp.headers = None body = {'error': {'message': message, 'details': details}} return nova_exceptions.from_response(resp, body, None)
kwargs['body'] = json.dumps(kwargs['body']) resp, body = super(HTTPClient, self).request(*args, **kwargs) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError, e: pass else: body = None if resp.status in (400, 401, 403, 404, 408, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body def _cs_request(self, url, method, **kwargs): if not self.management_url: self.authenticate() # Perform the request once. If we get a 401 back then it # might be because the auth token expired, so try to # re-authenticate and try again. If it still fails, bail. try: kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token if self.projectid: kwargs['headers']['X-Auth-Project-Id'] = self.projectid