Example #1
0
    def request(self, url, method, **kwargs):
        kwargs.setdefault('authenticated', False)

        # NOTE(thingee): v1 and v2 require the project id in the url. Prepend
        # it if we're doing discovery. We figure out if we're doing discovery
        # if there is no project id already specified in the path. parts is
        # a list where index 1 is the version discovered and index 2 might be
        # an empty string or a project id.
        endpoint = self.get_endpoint()
        parts = urlparse.urlsplit(endpoint).path.split('/')
        project_id = self.get_project_id()
        if (parts[1] in ['v1', 'v2'] and parts[2] == ''
                and project_id is not None):
            url = '{0}{1}{2}'.format(endpoint, project_id, url)

        # Note(tpatil): The standard call raises errors from
        # keystoneclient, here we need to raise the cinderclient 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)

        return resp, body
Example #2
0
    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:
            kwargs.setdefault("timeout", self.timeout)
        self.http_log_req((url, method), kwargs)
        resp = requests.request(method, url, verify=self.verify_cert, **kwargs)
        self.http_log_resp(resp)

        if 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
Example #3
0
    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 osprofiler_web:
            kwargs['headers'].update(osprofiler_web.get_trace_id_headers())

        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:
            kwargs.setdefault('timeout', self.timeout)
        self.http_log_req((
            url,
            method,
        ), kwargs)
        resp = requests.request(method, url, verify=self.verify_cert, **kwargs)
        self.http_log_resp(resp)

        body = None
        if resp.text:
            try:
                body = json.loads(resp.text)
            except ValueError as e:
                self._logger.debug("Load http response text error: %s", e)

        if resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #4
0
    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.auth_ref = access.AccessInfo.factory(resp, body)
                self.service_catalog = self.auth_ref.service_catalog

                if extract_token:
                    self.auth_token = self.auth_ref.auth_token

                management_url = self.service_catalog.url_for(
                    region_name=self.region_name,
                    endpoint_type=self.endpoint_type,
                    service_type=self.service_type)
                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['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 osprofiler_web:
            kwargs['headers'].update(osprofiler_web.get_trace_id_headers())

        if 'body' in kwargs:
            kwargs['headers']['Content-Type'] = 'application/json'
            kwargs['data'] = json.dumps(kwargs['body'])
            del kwargs['body']

        if self.timeout:
            kwargs.setdefault('timeout', self.timeout)
        self.http_log_req((url, method,), kwargs)
        resp = requests.request(
            method,
            url,
            verify=self.verify_cert,
            **kwargs)
        self.http_log_resp(resp)

        body = None
        if resp.text:
            try:
                body = json.loads(resp.text)
            except ValueError as e:
                self._logger.debug("Load http response text error: %s", e)

        if resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #6
0
    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.auth_ref = access.AccessInfo.factory(resp, body)
                self.service_catalog = self.auth_ref.service_catalog

                if extract_token:
                    self.auth_token = self.auth_ref.auth_token

                management_url = self.service_catalog.url_for(
                    region_name=self.region_name,
                    endpoint_type=self.endpoint_type,
                    service_type=self.service_type)
                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['location']
        else:
            raise exceptions.from_response(resp, body)
Example #7
0
    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 osprofiler_web:
            kwargs["headers"].update(osprofiler_web.get_trace_id_headers())

        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:
            kwargs.setdefault("timeout", self.timeout)
        self.http_log_req((url, method), kwargs)
        resp = requests.request(method, url, verify=self.verify_cert, **kwargs)
        self.http_log_resp(resp)

        body = None
        if resp.text:
            try:
                body = json.loads(resp.text)
            except ValueError as e:
                self._logger.debug("Load http response text error: %s", e)

        if resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #8
0
    def request(self, url, method, **kwargs):
        kwargs.setdefault('authenticated', False)

        # NOTE(thingee): v1 and v2 require the project id in the url. Prepend
        # it if we're doing discovery. We figure out if we're doing discovery
        # if there is no project id already specified in the path. parts is
        # a list where index 1 is the version discovered and index 2 might be
        # an empty string or a project id.
        endpoint = self.get_endpoint()
        parts = urlparse.urlsplit(endpoint).path.split('/')
        project_id = self.get_project_id()
        if (parts[1] in ['v1', 'v2'] and parts[2] == ''
                and project_id is not None):
            url = '{0}{1}{2}'.format(endpoint, project_id, url)

        # Note(tpatil): The standard call raises errors from
        # keystoneclient, here we need to raise the cinderclient 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)

        return resp, body
Example #9
0
    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:
            kwargs.setdefault('timeout', self.timeout)
        self.http_log_req((
            url,
            method,
        ), kwargs)
        resp = requests.request(method, url, verify=self.verify_cert, **kwargs)
        self.http_log_resp(resp)

        if 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
Example #10
0
    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,
            **kwargs)
        self.http_log_resp(resp)

        if 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 test_from_response_overlimit_without_header(self):
     response = requests.Response()
     response.status_code = 413
     response.headers = {}
     body = {'keys': ({})}
     ex = exceptions.from_response(response, body)
     self.assertEqual(0, ex.retry_after)
     self.assertIs(exceptions.OverLimit, type(ex))
 def test_from_response_overlimit_without_header(self):
     response = requests.Response()
     response.status_code = 413
     response.headers = {}
     body = {'keys': ({})}
     ex = exceptions.from_response(response, body)
     self.assertEqual(0, ex.retry_after)
     self.assertIs(exceptions.OverLimit, type(ex))
Example #13
0
 def test_from_response_no_body_message(self):
     # Tests that we get ClientException back since we don't have 500 mapped
     response = requests.Response()
     response.status_code = 500
     body = {'keys': ({})}
     ex = exceptions.from_response(response, body)
     self.assertIs(exceptions.ClientException, type(ex))
     self.assertEqual('n/a', ex.message)
 def test_from_response_overlimit_gmt(self, mock_utcnow):
     response = requests.Response()
     response.status_code = 413
     response.headers = {"Retry-After": "Thu, 30 Jun 2016 12:43:20 GMT"}
     body = {'keys': ({})}
     ex = exceptions.from_response(response, body)
     self.assertEqual(85, ex.retry_after)
     self.assertIs(exceptions.OverLimit, type(ex))
     self.assertTrue(mock_utcnow.called)
 def test_from_response_overlimit_gmt(self, mock_utcnow):
     response = requests.Response()
     response.status_code = 413
     response.headers = {"Retry-After": "Thu, 30 Jun 2016 12:43:20 GMT"}
     body = {'keys': ({})}
     ex = exceptions.from_response(response, body)
     self.assertEqual(85, ex.retry_after)
     self.assertIs(exceptions.OverLimit, type(ex))
     self.assertTrue(mock_utcnow.called)
Example #16
0
 def request(self, *args, **kwargs):
     kwargs.setdefault('authenticated', False)
     # Note(tpatil): The standard call raises errors from
     # keystoneclient, here we need to raise the cinderclient errors.
     raise_exc = kwargs.pop('raise_exc', True)
     resp, body = super(SessionClient, self).request(*args,
                                                        raise_exc=False,
                                                        **kwargs)
     if raise_exc and resp.status_code >= 400:
         raise exceptions.from_response(resp, body)
     return resp, body
Example #17
0
    def request(self, *args, **kwargs):
        kwargs.setdefault('authenticated', False)
        # Note(tpatil): The standard call raises errors from
        # keystoneclient, here we need to raise the cinderclient errors.
        raise_exc = kwargs.pop('raise_exc', True)
        resp, body = super(SessionClient, self).request(*args,
                                                        raise_exc=False,
                                                        **kwargs)
        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #18
0
    def request(self, *args, **kwargs):
        kwargs.setdefault("headers", kwargs.get("headers", {}))
        api_versions.update_headers(kwargs["headers"], self.api_version)
        kwargs.setdefault("authenticated", False)
        # Note(tpatil): The standard call raises errors from
        # keystoneauth, here we need to raise the cinderclient errors.
        raise_exc = kwargs.pop("raise_exc", True)
        resp, body = super(SessionClient, self).request(*args, 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)

        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #19
0
    def request(self, *args, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        api_versions.update_headers(kwargs["headers"], self.api_version)
        kwargs.setdefault('authenticated', False)

        if self.global_request_id:
            kwargs['headers'].setdefault(REQ_ID_HEADER, self.global_request_id)

        # Note(tpatil): The standard call raises errors from
        # keystoneauth, here we need to raise the cinderclient errors.
        raise_exc = kwargs.pop('raise_exc', True)
        resp, body = super(SessionClient, self).request(*args,
                                                        raise_exc=False,
                                                        **kwargs)

        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #20
0
    def request(self, *args, **kwargs):
        kwargs.setdefault('headers', kwargs.get('headers', {}))
        api_versions.update_headers(kwargs["headers"], self.api_version)
        kwargs.setdefault('authenticated', False)
        # Note(tpatil): The standard call raises errors from
        # keystoneauth, here we need to raise the cinderclient errors.
        raise_exc = kwargs.pop('raise_exc', True)
        resp, body = super(SessionClient, self).request(*args,
                                                        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)

        if raise_exc and resp.status_code >= 400:
            raise exceptions.from_response(resp, body)

        return resp, body
Example #21
0
    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_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)
Example #22
0
    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_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)
Example #23
0
    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.auth_ref = access.create(resp=resp, body=body)
                self.service_catalog = self.auth_ref.service_catalog

                if extract_token:
                    self.auth_token = self.auth_ref.auth_token

                management_url = self.service_catalog.url_for(
                    region_name=self.region_name,
                    interface=self.endpoint_type,
                    service_type=self.service_type,
                    service_name=self.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 ValueError:
                # ValueError is raised when you pass an invalid response to
                # access.create. This should never happen in reality if the
                # status code is 200.
                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)
Example #24
0
    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.auth_ref = access.create(resp=resp, body=body)
                self.service_catalog = self.auth_ref.service_catalog

                if extract_token:
                    self.auth_token = self.auth_ref.auth_token

                management_url = self.service_catalog.url_for(
                    region_name=self.region_name,
                    interface=self.endpoint_type,
                    service_type=self.service_type,
                    service_name=self.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 ValueError:
                # ValueError is raised when you pass an invalid response to
                # access.create. This should never happen in reality if the
                # status code is 200.
                raise exceptions.AuthorizationFailure()
            except exceptions.EndpointNotFound:
                print("Could not find any suitable endpoint. Correct region?")
                raise

        elif resp.status_code == 305:
            return resp["location"]
        else:
            raise exceptions.from_response(resp, body)
Example #25
0
    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:
                mgmt_header = 'x-server-management-url'
                self.management_url = resp[mgmt_header].rstrip('/')
                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)
Example #26
0
    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
Example #27
0
    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