def _raise_response(self, response): msg = _('Request to Datera cluster returned bad status:' ' %(status)s | %(reason)s') % { 'status': response.status_code, 'reason': response.reason} LOG.error(msg) raise exception.DateraAPIException(msg)
def wrapper(*args, **kwargs): obj = args[0] api_versions = _get_supported_api_versions(obj) api_version = None index = -1 while True: try: api_version = api_versions[index] except (IndexError, KeyError): msg = _("No compatible API version found for this product: " "api_versions -> %(api_version)s, %(func)s") LOG.error(msg, api_version=api_version, func=func) raise exception.DateraAPIException(msg % { 'api_version': api_version, 'func': func }) # Py27 try: name = "_" + "_".join( (func.func_name, api_version.replace(".", "_"))) # Py3+ except AttributeError: name = "_" + "_".join( (func.__name__, api_version.replace(".", "_"))) try: if obj.do_profile: LOG.info("Trying method: %s", name) call_id = uuid.uuid4() LOG.debug("Profiling method: %s, id %s", name, call_id) t1 = time.time() obj.thread_local.trace_id = call_id result = getattr(obj, name)(*args[1:], **kwargs) if obj.do_profile: t2 = time.time() timedelta = round(t2 - t1, 3) LOG.debug("Profile for method %s, id %s: %ss", name, call_id, timedelta) return result except AttributeError as e: # If we find the attribute name in the error message # then we continue otherwise, raise to prevent masking # errors if name not in six.text_type(e): raise else: LOG.info(e) index -= 1 except exception.DateraAPIException as e: if "UnsupportedVersionError" in six.text_type(e): index -= 1 else: raise
def _request(self, connection_string, method, payload, header, cert_data): LOG.debug("Endpoint for Datera API call: %s", connection_string) try: response = getattr(requests, method)(connection_string, data=payload, headers=header, verify=False, cert=cert_data) return response except requests.exceptions.RequestException as ex: msg = _( 'Failed to make a request to Datera cluster endpoint due ' 'to the following reason: %s') % six.text_type( ex.message) LOG.error(msg) raise exception.DateraAPIException(msg)
def wrapper(*args, **kwargs): obj = args[0] api_versions = _get_supported_api_versions(obj) api_version = None index = -1 while True: try: api_version = api_versions[index] except (IndexError, KeyError): msg = _("No compatible API version found for this product: " "api_versions -> %(api_version)s, %(func)s") LOG.error(msg, api_version=api_version, func=func) raise exception.DateraAPIException(msg % (api_version, func)) # Py27 try: name = "_" + "_".join( (func.func_name, api_version.replace(".", "_"))) # Py3+ except AttributeError: name = "_" + "_".join( (func.__name__, api_version.replace(".", "_"))) try: LOG.info(_LI("Trying method: %s"), name) return getattr(obj, name)(*args[1:], **kwargs) except AttributeError as e: # If we find the attribute name in the error message # then we continue otherwise, raise to prevent masking # errors if name not in six.text_type(e): raise else: LOG.info(e) index -= 1 except exception.DateraAPIException as e: if "UnsupportedVersionError" in six.text_type(e): index -= 1 else: raise
def _issue_api_request(self, resource_type, method='get', resource=None, body=None, action=None, sensitive=False): """All API requests to Datera cluster go through this method. :param resource_type: the type of the resource :param method: the request verb :param resource: the identifier of the resource :param body: a dict with options for the action_type :param action: the action to perform :returns: a dict of the response from the Datera cluster """ host = self.configuration.san_ip port = self.configuration.datera_api_port api_token = self.configuration.datera_api_token api_version = self.configuration.datera_api_version payload = json.dumps(body, ensure_ascii=False) payload.encode('utf-8') if not sensitive: LOG.debug("Payload for Datera API call: %s", payload) header = { 'Content-Type': 'application/json; charset=utf-8', 'auth-token': self.auth_token } protocol = 'http' if self.configuration.driver_use_ssl: protocol = 'https' # TODO(thingee): Auth method through Auth-Token is deprecated. Remove # this and client cert verification stuff in the Liberty release. if api_token: header['Auth-Token'] = api_token client_cert = self.configuration.driver_client_cert client_cert_key = self.configuration.driver_client_cert_key cert_data = None if client_cert: protocol = 'https' cert_data = (client_cert, client_cert_key) connection_string = '%s://%s:%s/v%s/%s' % (protocol, host, port, api_version, resource_type) if resource is not None: connection_string += '/%s' % resource if action is not None: connection_string += '/%s' % action LOG.debug("Endpoint for Datera API call: %s", connection_string) try: response = getattr(requests, method)(connection_string, data=payload, headers=header, verify=False, cert=cert_data) except requests.exceptions.RequestException as ex: msg = _('Failed to make a request to Datera cluster endpoint due ' 'to the following reason: %s') % six.text_type(ex.message) LOG.error(msg) raise exception.DateraAPIException(msg) data = response.json() if not sensitive: LOG.debug("Results of Datera API call: %s", data) if not response.ok: if response.status_code == 404: raise exception.NotFound(data['message']) elif response.status_code in [403, 401]: raise exception.NotAuthorized() else: msg = _('Request to Datera cluster returned bad status:' ' %(status)s | %(reason)s') % { 'status': response.status_code, 'reason': response.reason } LOG.error(msg) raise exception.DateraAPIException(msg) return data