Example #1
0
def exception_handler_v10(status_code, error_content):
    """Exception handler for API v1.0 client.

    This routine generates the appropriate Apmec 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('ApmecError')
    # Find real error type
    bad_apmec_error_flag = False
    if error_dict:
        # If Apmec 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_apmec_error_flag = True
        if not bad_apmec_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.ApmecClientException(
                    status_code=status_code, message=error_message)
        else:
            raise exceptions.ApmecClientException(status_code=status_code,
                                                   message=error_dict)
    else:
        message = None
        if isinstance(error_content, dict):
            message = error_content.get('message')
        if message:
            raise exceptions.ApmecClientException(status_code=status_code,
                                                   message=message)

    # If we end up here the exception was not a apmec error
    msg = "%s-%s" % (status_code, error_content)
    raise exceptions.ApmecClientException(status_code=status_code,
                                           message=msg)
Example #2
0
def args2body_vim(config_param, vim):
    """Create additional args to vim body

    :param vim: vim request object
    :return: vim body with args populated
    """
    vim['vim_project'] = {
        'name': config_param.pop('project_name', ''),
        'project_domain_name': config_param.pop('project_domain_name', '')
    }
    if not vim['vim_project']['name']:
        raise exceptions.ApmecClientException(message='Project name '
                                              'must be specified',
                                              status_code=404)
    vim['auth_cred'] = {
        'username': config_param.pop('username', ''),
        'password': config_param.pop('password', ''),
        'user_domain_name': config_param.pop('user_domain_name', '')
    }
Example #3
0
def _find_resourceid_by_name(client, resource, name):
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % resource_plural)
    data = obj_lister(name=name, fields='id')
    collection = resource_plural
    info = data[collection]
    if len(info) > 1:
        raise exceptions.ApmecClientNoUniqueMatch(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.ApmecClientException(message=not_found_message,
                                              status_code=404)
    else:
        return info[0]['id']
Example #4
0
def find_resourceid_by_id(client, resource, resource_id):
    resource_plural = _get_resource_plural(resource, client)
    obj_lister = getattr(client, "list_%s" % resource_plural)
    if resource == 'event':
        match = resource_id.isdigit() and resource_id != 0
    else:
        match = re.match(UUID_PATTERN, resource_id)
    collection = resource_plural
    if match:
        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.ApmecClientException(message=not_found_message,
                                          status_code=404)
Example #5
0
 def args2body(self, parsed_args):
     body = {self.resource: {}}
     if parsed_args.config_file:
         with open(parsed_args.config_file) as f:
             vim_config = f.read()
             try:
                 config_param = yaml.load(vim_config,
                                          Loader=yaml.SafeLoader)
             except yaml.YAMLError as e:
                 raise exceptions.InvalidInput(e)
     vim_obj = body[self.resource]
     try:
         auth_url = config_param.pop('auth_url')
     except KeyError:
         raise exceptions.ApmecClientException(message='Auth URL must be '
                                               'specified',
                                               status_code=404)
     vim_obj['auth_url'] = vim_utils.validate_auth_url(auth_url).geturl()
     vim_obj['type'] = config_param.pop('type', 'openstack')
     vim_utils.args2body_vim(config_param, vim_obj)
     apmecV10.update_dict(parsed_args, body[self.resource],
                          ['tenant_id', 'name', 'description',
                           'is_default'])
     return body
Example #6
0
def validate_auth_url(url):
    url_parts = urlparse.urlparse(url)
    if not url_parts.scheme or not url_parts.netloc:
        raise exceptions.ApmecClientException(message='Invalid auth URL')
    return url_parts