示例#1
0
def server_list(request, search_opts=None, detailed=True):
    nova_client = get_novaclient_with_locked_status(request)
    page_size = utils.get_page_size(request)
    paginate = False
    if search_opts is None:
        search_opts = {}

    limit = search_opts.get('limit', None)
    page_size = base.get_request_page_size(request, limit)

    if 'paginate' in search_opts:
        paginate = search_opts.pop('paginate')
        if paginate:
            search_opts['limit'] = page_size + 1

    all_tenants = search_opts.get('all_tenants', False)
    if all_tenants:
        search_opts['all_tenants'] = True
    else:
        search_opts['project_id'] = request.user.tenant_id

    servers = [
        Server(s, request)
        for s in nova_client.servers.list(detailed, search_opts)
    ]

    has_more_data = False
    if paginate and len(servers) > page_size:
        servers.pop(-1)
        has_more_data = True
    elif paginate and len(servers) == getattr(settings, 'API_RESULT_LIMIT',
                                              1000):
        has_more_data = True
    return (servers, has_more_data)
示例#2
0
文件: fm.py 项目: haydemtzl/stx-gui
def event_log_list(request, search_opts=None):
    paginate = False

    # If expand is set to true then all the data of the alarm is returned not
    # just a subset.
    expand = False

    if search_opts is None:
        search_opts = {}

    limit = search_opts.get('limit', None)
    marker = search_opts.get('marker', None)
    page_size = base.get_request_page_size(request, limit)

    if 'paginate' in search_opts:
        paginate = search_opts.pop('paginate')
        if paginate:
            limit = page_size + 1

    query = None
    alarms = False
    logs = False
    include_suppress = False

    if "evtType" in search_opts:
        evtType = search_opts.pop('evtType')
        if evtType == FM_ALARM:
            alarms = True
        elif evtType == FM_LOG:
            logs = True

    if "suppression" in search_opts:
        suppression = search_opts.pop('suppression')

        if suppression == FM_SUPPRESS_SHOW:
            include_suppress = True
        elif suppression == FM_SUPPRESS_HIDE:
            include_suppress = False

    if "expand" in search_opts:
        expand = True

    logs = fmclient(request)\
        .event_log.list(q=query,
                        limit=limit,
                        marker=marker,
                        alarms=alarms,
                        logs=logs,
                        include_suppress=include_suppress,
                        expand=expand)

    has_more_data = False
    if paginate and len(logs) > page_size:
        logs.pop(-1)
        has_more_data = True
    elif paginate and len(logs) > getattr(settings, 'API_RESULT_LIMIT', 1000):
        has_more_data = True

    return [EventLog(n) for n in logs], has_more_data
示例#3
0
文件: fm.py 项目: haydemtzl/stx-gui
def alarm_list(request, search_opts=None):
    paginate = False
    include_suppress = False

    # If expand is set to true then all the data of the alarm is returned not
    # just a subset.
    expand = False

    if search_opts is None:
        search_opts = {}

    limit = search_opts.get('limit', None)
    marker = search_opts.get('marker', None)
    sort_key = search_opts.get('sort_key', None)
    sort_dir = search_opts.get('sort_dir', None)
    page_size = base.get_request_page_size(request, limit)

    if "suppression" in search_opts:
        suppression = search_opts.pop('suppression')

        if suppression == FM_SUPPRESS_SHOW:
            include_suppress = True
        elif suppression == FM_SUPPRESS_HIDE:
            include_suppress = False

    if "expand" in search_opts:
        expand = True

    if 'paginate' in search_opts:
        paginate = search_opts.pop('paginate')
        if paginate:
            limit = page_size + 1

    alarms = fmclient(request).alarm.list(
        limit=limit, marker=marker, sort_key=sort_key, sort_dir=sort_dir,
        include_suppress=include_suppress, expand=expand)

    has_more_data = False
    if paginate and len(alarms) > page_size:
        alarms.pop(-1)
        has_more_data = True
    elif paginate and len(alarms) > getattr(settings,
                                            'API_RESULT_LIMIT', 1000):
        has_more_data = True

    if paginate:
        return [Alarm(n) for n in alarms], has_more_data
    else:
        return [Alarm(n) for n in alarms]
示例#4
0
def image_list_detailed(request,
                        marker=None,
                        sort_dir='desc',
                        sort_key='created_at',
                        limit=None,
                        filters=None,
                        paginate=False,
                        reversed_order=False,
                        **kwargs):
    """Thin layer above glanceclient, for handling pagination issues.

    It provides iterating both forward and backward on top of ascetic
    OpenStack pagination API - which natively supports only iterating forward
    through the entries. Thus in order to retrieve list of objects at previous
    page, a request with the reverse entries order had to be made to Glance,
    using the first object id on current page as the marker - restoring
    the original items ordering before sending them back to the UI.

    :param request:

        The request object coming from browser to be passed further into
        Glance service.

    :param marker:

        The id of an object which defines a starting point of a query sent to
        Glance service.

    :param sort_dir:

        The direction by which the resulting image list throughout all pages
        (if pagination is enabled) will be sorted. Could be either 'asc'
        (ascending) or 'desc' (descending), defaults to 'desc'.

    :param sort_key:

        The name of key by by which the resulting image list throughout all
        pages (if pagination is enabled) will be sorted. Defaults to
        'created_at'.

    :param filters:

        A dictionary of filters passed as is to Glance service.

    :param paginate:

        Whether the pagination is enabled. If it is, then the number of
        entries on a single page of images table is limited to the specific
        number stored in browser cookies.

    :param reversed_order:

        Set this flag to True when it's necessary to get a reversed list of
        images from Glance (used for navigating the images list back in UI).
    """
    result_limit = getattr(settings, 'API_RESULT_LIMIT', 1000)
    page_size = base.get_request_page_size(request, limit)

    if paginate:
        request_size = page_size + 1
    else:
        request_size = result_limit

    _normalize_list_input(filters, **kwargs)
    kwargs = {'filters': filters or {}}

    if marker:
        kwargs['marker'] = marker
    kwargs['sort_key'] = sort_key

    if not reversed_order:
        kwargs['sort_dir'] = sort_dir
    else:
        kwargs['sort_dir'] = 'desc' if sort_dir == 'asc' else 'asc'

    images_iter = glanceclient(request).images.list(page_size=request_size,
                                                    limit=result_limit,
                                                    **kwargs)
    has_prev_data = False
    has_more_data = False
    if paginate:
        images = list(itertools.islice(images_iter, request_size))
        # first and middle page condition
        if len(images) > page_size:
            images.pop(-1)
            has_more_data = True
            # middle page condition
            if marker is not None:
                has_prev_data = True
        # first page condition when reached via prev back
        elif reversed_order and marker is not None:
            has_more_data = True
        # last page condition
        elif marker is not None:
            has_prev_data = True

        # restore the original ordering here
        if reversed_order:
            images = sorted(images,
                            key=lambda image:
                            (getattr(image, sort_key) or '').lower(),
                            reverse=(sort_dir == 'desc'))
    else:
        images = list(images_iter)

    # TODO(jpichon): Do it better
    wrapped_images = []
    for image in images:
        wrapped_images.append(Image(image))

    return wrapped_images, has_more_data, has_prev_data