Пример #1
0
def _paginate(context,
              query,
              model,
              session,
              filters,
              pagination_params,
              project_only=False):
    # NOTE(sigmavirus24) Retrieve the instance of the model represented by the
    # marker.
    try:
        marker = _marker_from(context, session, model,
                              pagination_params['marker'], project_only)
    except sa_exc.NoResultFound:
        raise exceptions.BadRequest(message='Marker "{}" does not exist'.
                                    format(pagination_params['marker']))
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    filters.setdefault('sort_keys', ['created_at', 'id'])
    filters.setdefault('sort_dir', 'asc')
    # Retrieve the results based on the marker and the limit
    try:
        results = db_utils.paginate_query(
            query,
            model,
            limit=pagination_params['limit'],
            sort_keys=filters['sort_keys'],
            sort_dir=filters['sort_dir'],
            marker=marker,
        ).all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except db_exc.InvalidSortKey as invalid_key:
        raise exceptions.BadRequest(
            message='"{}" is an invalid sort key'.format(invalid_key.key))
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    try:
        links = _link_params_for(
            query,
            model,
            filters,
            pagination_params,
            marker,
            results,
        )
    except Exception as err:
        raise exceptions.UnknownException(message=err)

    return results, links
Пример #2
0
def hosts_get_by_region(context, region_id, filters):
    """Get all hosts for this region.

    :param region_id: ID for the region
    :param filters: filters wich contains differnt keys/values to match.
    Supported filters are by name, ip_address, id and cell_id.
    """
    host_devices = with_polymorphic(models.Device, [models.Host])
    query = model_query(context, host_devices, project_only=True)
    query = query.filter_by(region_id=region_id)

    if "name" in filters:
        query = query.filter_by(name=filters["name"])
    if "ip_address" in filters:
        query = query.filter_by(ip_address=filters["ip_address"])
    if "id" in filters:
        query = query.filter_by(id=filters["id"])
    if "cell" in filters:
        query = query.filter_by(cell_id=filters["cell"])
    if "device_type" in filters:
        query = query.filter_by(device_type=filters["device_type"])

    try:
        result = query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
    return result
Пример #3
0
def projects_get_all(context):
    """Get all the projects."""
    query = model_query(context, models.Project)
    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #4
0
def projects_get_by_id(context, project_id):
    """Get project by its id."""
    query = model_query(context, models.Project)
    query = query.filter_by(id=project_id)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #5
0
def projects_get_by_name(context, project_name):
    """Get all projects that match the given name."""
    query = model_query(context, models.Project)
    query = query.filter(models.Project.name.like(project_name))
    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #6
0
def get_user_info(context, username):
    """Get user info."""
    query = model_query(context, models.User, project_only=True)
    query = query.filter_by(username=username)
    try:
        return query.one()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #7
0
def cells_get_all(context, region):
    """Get all cells."""
    query = model_query(context, models.Cell, project_only=True)
    if region is not None:
        query = query.filter_by(region_id=region)

    try:
        return query.all()
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #8
0
def projects_get_by_name(context, project_name, filters, pagination_params):
    """Get all projects that match the given name."""
    query = model_query(context, models.Project)
    query = query.filter(models.Project.name.like(project_name))
    if "vars" in filters:
        query = add_var_filters_to_query(query, filters)
    try:
        return _paginate(context, query, models.Project, session, filters,
                         pagination_params)
    except sa_exc.NoResultFound:
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
Пример #9
0
def hosts_get_by_id(context, host_id):
    """Get details for the host with given id."""
    host_devices = with_polymorphic(models.Device, '*')
    query = model_query(context, host_devices, project_only=True).\
        filter_by(id=host_id)
    try:
        result = query.one()
        LOG.info("Result by host id %s" % result)
    except sa_exc.NoResultFound:
        LOG.error("No result found for host with id %s" % host_id)
        raise exceptions.NotFound()
    except Exception as err:
        raise exceptions.UnknownException(message=err)
    return result