def post_resp(self,
               url,
               query_params=None,
               data=None,
               content_type='application/x-yaml'):
     """ Thin wrapper of requests post """
     if not query_params:
         query_params = {}
     if not data:
         data = {}
     try:
         headers = {
             'X-Context-Marker': self.context.context_marker,
             'content-type': content_type,
             'X-Auth-Token': self.get_token()
         }
         self.debug('Post request url: ' + url)
         self.debug('Query Params: ' + str(query_params))
         # This could use keystoneauth1 session, but that library handles
         # responses strangely (wraps all 400/500 in a keystone exception)
         response = requests.post(
             url, data=data, params=query_params, headers=headers)
         # handle some cases where the response code is sufficient to know
         # what needs to be done
         if response.status_code == 401:
             raise UnauthenticatedClientError()
         if response.status_code == 403:
             raise UnauthorizedClientError()
         return response
     except requests.exceptions.RequestException as e:
         self.error(str(e))
         raise ClientError(str(e))
 def _get_ks_session(self):
     self.logger.debug('Accessing keystone for keystone session')
     try:
         auth = v3.Password(**self.context.keystone_auth)
         return session.Session(auth=auth)
     except AuthorizationFailure as e:
         self.logger.error('Could not authorize against keystone: %s',
                           str(e))
         raise ClientError(str(e))
Example #3
0
    def get_endpoint(self):
        """Lookup the endpoint for the client. Cache it.

        Uses a keystone session to find an endpoint for the specified
        service_type at the specified interface (public, internal, admin)
        """
        if self.endpoint is None:
            self.logger.debug('Accessing keystone for %s endpoint',
                              self.service_type)
            try:
                self.endpoint = self._get_ks_session().get_endpoint(
                    interface=self.interface, service_type=self.service_type)
            except EndpointNotFound as e:
                self.logger.error('Could not find %s interface for %s',
                                  self.interface, self.service_type)
                raise ClientError(str(e))
        return self.endpoint
 def get_resp(self, url, query_params=None):
     """ Thin wrapper of requests get """
     if not query_params:
         query_params = {}
     try:
         headers = {
             'X-Context-Marker': self.context.context_marker,
             'X-Auth-Token': self.get_token()
         }
         self.debug('url: ' + url)
         self.debug('Query Params: ' + str(query_params))
         response = requests.get(url, params=query_params, headers=headers)
         # handle some cases where the response code is sufficient to know
         # what needs to be done
         if response.status_code == 401:
             raise UnauthenticatedClientError()
         if response.status_code == 403:
             raise UnauthorizedClientError()
         return response
     except requests.exceptions.RequestException as e:
         self.error(str(e))
         raise ClientError(str(e))