def exception_handler_v20(status_code, error_content): """Exception handler for API v2.0 client This routine generates the appropriate vnfsvc exception according to the contents of the response body :param status_code: HTTP error status code :param error_content: deserialized body of error response """ error_dict = None if isinstance(error_content, dict): error_dict = error_content.get('VNFSvcError') # Find real error type bad_vnfsvc_error_flag = False if error_dict: # If vnfsvc key is found, it will definitely contain # a 'message' and 'type' keys? try: error_type = error_dict['type'] error_message = error_dict['message'] if error_dict['detail']: error_message += "\n" + error_dict['detail'] except Exception: bad_vnfsvc_error_flag = True if not bad_vnfsvc_error_flag: # If corresponding exception is defined, use it. client_exc = getattr(exceptions, '%sClient' % error_type, None) # Otherwise look up per status-code client exception if not client_exc: client_exc = exceptions.HTTP_EXCEPTION_MAP.get(status_code) if client_exc: raise client_exc(message=error_message, status_code=status_code) else: raise exceptions.VNFSvcClientException(status_code=status_code, message=error_message) else: raise exceptions.VNFSvcClientException(status_code=status_code, message=error_dict) else: message = None if isinstance(error_content, dict): message = error_content.get('message') if message: raise exceptions.VNFSvcClientException(status_code=status_code, message=message) # If we end up here the exception was not a vnfsvc error msg = "%s-%s" % (status_code, error_content) raise exceptions.VNFSvcClientException(status_code=status_code, message=msg)
def find_resourceid_by_id(client, resource, resource_id, cmd_resource=None, parent_id=None): if not cmd_resource: cmd_resource = resource cmd_resource_plural = _get_resource_plural(cmd_resource, client) resource_plural = _get_resource_plural(resource, client) obj_lister = getattr(client, "list_%s" % cmd_resource_plural) # perform search by id only if we are passing a valid UUID match = re.match(UUID_PATTERN, resource_id) collection = resource_plural if match: if parent_id: data = obj_lister(parent_id, id=resource_id, fields='id') else: data = obj_lister(id=resource_id, fields='id') if data and data[collection]: return data[collection][0]['id'] not_found_message = (_("Unable to find %(resource)s with id " "'%(id)s'") % { 'resource': resource, 'id': resource_id }) # 404 is used to simulate server side behavior raise exceptions.VNFSvcClientException(message=not_found_message, status_code=404)
def _find_resourceid_by_name(client, resource, name, project_id=None, cmd_resource=None, parent_id=None): if not cmd_resource: cmd_resource = resource cmd_resource_plural = _get_resource_plural(cmd_resource, client) resource_plural = _get_resource_plural(resource, client) obj_lister = getattr(client, "list_%s" % cmd_resource_plural) params = {'name': name, 'fields': 'id'} if project_id: params['tenant_id'] = project_id if parent_id: data = obj_lister(parent_id, **params) else: data = obj_lister(**params) collection = resource_plural info = data[collection] if len(info) > 1: raise exceptions.VNFSvcClientNoUniqueMatch(resource=resource, name=name) elif len(info) == 0: not_found_message = (_("Unable to find %(resource)s with name " "'%(name)s'") % {'resource': resource, 'name': name}) # 404 is used to simulate server side behavior raise exceptions.VNFSvcClientException( message=not_found_message, status_code=404) else: return info[0]['id']
def get_auth_info(self): # This method is provided for backward compatibility only. if not isinstance(self.auth, BaseIdentityPlugin): msg = ('Auth info not available. Auth plugin is not an identity ' 'auth plugin.') raise exceptions.VNFSvcClientException(message=msg) access_info = self.auth.get_access(self.session) endpoint_url = self.auth.get_endpoint(self.session, service_type=self.service_type, region_name=self.region_name, interface=self.interface) return { 'auth_token': access_info.auth_token, 'auth_tenant_id': access_info.tenant_id, 'auth_user_id': access_info.user_id, 'endpoint_url': endpoint_url }