def _authenticate(self, url, body, root_key='access'): """Authenticate and extract the service catalog.""" # Make sure we follow redirects when trying to reach Keystone tmp_follow_all_redirects = self.client.follow_all_redirects self.client.follow_all_redirects = True try: resp, body = self.client._time_request(url, "POST", body=body) finally: self.client.follow_all_redirects = tmp_follow_all_redirects if resp.status == 200: # content must always present try: return ServiceCatalog(body, region=self.region, service_type=self.service_type, service_name=self.service_name, service_url=self.service_url, root_key=root_key) 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 == 305: return resp['location'] else: raise exceptions.from_response(resp, body)
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(ReddwarfHTTPClient, self).request(*args, **kwargs) # Save this in case anyone wants it. self.last_response = (resp, body) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError: 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 request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT self.morph_request(kwargs) resp, body = super(ReddwarfHTTPClient, self).request(*args, **kwargs) # Save this in case anyone wants it. self.last_response = (resp, body) self.http_log(args, kwargs, resp, body) if body: try: body = self.morph_response_body(body) except exceptions.ResponseFormatError: # Acceptable only if the response status is an error code. # Otherwise its the API or client misbehaving. self.raise_error_from_status(resp, None) raise # Not accepted! else: body = None if resp.status in expected_errors: raise exceptions.from_response(resp, body) return resp, body
def request(self, *args, **kwargs): #TODO(tim.simpson): Copy and pasted from novaclient, since we raise # extra exception subclasses not raised there. 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 in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body
def request(self, *args, **kwargs): #TODO(tim.simpson): Copy and pasted from novaclient, since we raise # extra exception subclasses not raised there. 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) # Save this in case anyone wants it. self.last_response = (resp, body) self.http_log(args, kwargs, resp, body) if body: try: body = json.loads(body) except ValueError: 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 delete(self, instance): """ Delete the specified instance. :param instance_id: The instance id to delete """ resp, body = self.api.client.delete("/instances/%s" % base.getid(instance)) if resp.status in (422, 500): raise exceptions.from_response(resp, body)
def raise_error_from_status(self, resp, body): if resp.status in expected_errors: raise exceptions.from_response(resp, body)
def raise_error_from_status(self, resp, body): if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body)
def check_for_exceptions(resp, body): if resp.status in (400, 422, 500): raise exceptions.from_response(resp, body)