Example #1
0
def view_nodes(request):

    page_title_type = 'objects/'
    page_title_name = 'nodes'
    au = get_authenticated_user(request)
    (perpage, offset) = get_pag_params(request)

    payload = {}
    for k in request.GET:
        payload[k] = request.GET[k]

    uri = '/api/nodes'
    log.info('UI requesting data from API={0},payload={1}'.format(uri, payload))

    if payload:
        r = _api_get(request, uri, payload)
    else:
        r = _api_get(request, uri)

    if r:
        total = r['meta']['total']
        nodes = r['results']
    else:
        total = 0
        nodes = []

    # Used by the columns menu to determine what to show/hide.
    column_selectors = [ {'name': 'node_id', 'pretty_name': 'Node ID' },
                         {'name': 'node_name', 'pretty_name': 'Node Name' },
                         {'name': 'unique_id', 'pretty_name': 'Unique Id' },
                         {'name': 'status', 'pretty_name': 'Status' },
                         {'name': 'node_groups', 'pretty_name': 'Node Groups' },
                         {'name': 'updated_by', 'pretty_name': 'Updated By' },
                         {'name': 'updated', 'pretty_name': 'Date Updated' },
                         {'name': 'created', 'pretty_name': 'Date Created' },
    ]

    return {'layout': site_layout('max'),
            'page_title_type': page_title_type,
            'page_title_name': page_title_name,
            'perpage': perpage,
            'offset': offset,
            'total': total,
            'au': au,
            'column_selectors': column_selectors,
            'nodes': nodes,
           }
Example #2
0
def view_operating_systems(request):

    page_title_type = 'object/'
    page_title_name = 'operating_systems'
    au = get_authenticated_user(request)
    (perpage, offset) = get_pag_params(request)

    payload = {}
    for k in request.GET:
        payload[k] = request.GET[k]

    uri = '/api/operating_systems'
    log.info('UI requesting data from API={0},payload={1}'.format(uri, payload))

    if payload:
        r = _api_get(request, uri, payload)
    else:
        r = _api_get(request, uri)

    if r:
        total = r['meta']['total']
        operating_systems = r['results']
    else:
        total = 0
        operating_systems = []

    # Used by the columns menu to determine what to show/hide.
    column_selectors = [ {'name': 'operating_system_id', 'pretty_name': 'Operating system ID' },
                         {'name': 'variant', 'pretty_name': 'Variant' },
                         {'name': 'version_number', 'pretty_name': 'Version Number' },
                         {'name': 'architecture', 'pretty_name': 'Architecture' },
                         {'name': 'description', 'pretty_name': 'Description' },
                         {'name': 'updated_by', 'pretty_name': 'Updated By' },
                         {'name': 'updated', 'pretty_name': 'Date Updated' },
                         {'name': 'created', 'pretty_name': 'Date Created' },
    ]

    return {'layout': site_layout('max'),
            'page_title_type': page_title_type,
            'page_title_name': page_title_name,
            'perpage': perpage,
            'offset': offset,
            'total': total,
            'au': au,
            'column_selectors': column_selectors,
            'operating_systems': operating_systems,
           }
Example #3
0
def view_hardware_profiles(request):

    page_title_type = 'objects/'
    page_title_name = 'hardware_profiles'
    au = get_authenticated_user(request)
    (perpage, offset) = get_pag_params(request)

    payload = {}
    for k in request.GET:
        payload[k] = request.GET[k]

    uri = '/api/hardware_profiles'
    log.info('UI requesting data from API={0},payload={1}'.format(uri, payload))

    if payload:
        r = _api_get(request, uri, payload)
    else:
        r = _api_get(request, uri)

    if r:
        total = r['meta']['total']
        hardware_profiles = r['results']
    else:
        total = 0
        hardware_profiles = []

    # Used by the columns menu to determine what to show/hide.
    column_selectors = [ {'name': 'hardware_profile_id', 'pretty_name': 'Hardware Profile ID' },
                         {'name': 'manufacturer', 'pretty_name': 'Manufacturer' },
                         {'name': 'model', 'pretty_name': 'Model' },
                         {'name': 'updated_by', 'pretty_name': 'Updated By' },
                         {'name': 'updated', 'pretty_name': 'Date Updated' },
                         {'name': 'created', 'pretty_name': 'Date Created' },
    ]

    return {'layout': site_layout('max'),
            'page_title_type': page_title_type,
            'page_title_name': page_title_name,
            'perpage': perpage,
            'offset': offset,
            'total': total,
            'au': au,
            'column_selectors': column_selectors,
            'hardware_profiles': hardware_profiles,
           }
Example #4
0
def api_read_by_params(request, model_type):
    """Process get requests for /api/{object_type} route match."""

    c = camel_to_underscore(model_type)
    (perpage, offset) = get_pag_params(request)

    try:
        exact_get =  request.GET.get("exact_get", None)

        if request.params:
            s = ''
            # Filter on all the passed in terms
            q = DBSession.query(globals()[model_type])

            for k,v in request.GET.items():
                # Force unique_id to lower since mac addrs can be represented both ways.
                if k == 'unique_id':
                    v = v.lower()
                # FIXME: This is sub-par. Need a better way to distinguish
                # meta params from search params without having to
                # pre-define everything.
                if k == 'exact_get':
                    continue
                if k == 'start':
                    continue

                s+='{0}={1},'.format(k, v)
                if exact_get:
                    log.debug('Exact filtering on {0}={1}'.format(k, v))
                    if ',' in v:
                        q = q.filter(or_(getattr(globals()[model_type] ,k) == t for t in v.split(',')))
                    else:
                        q = q.filter(getattr(globals()[model_type] ,k)==v)
                else:
                    log.debug('Loose filtering on {0}={1}'.format(k, v))
                    if ',' in v:
                        log.info('Multiple values for key {0}={1}'.format(k, v))
                        multior = []
                        for t in v.split(','):
                            multior.append(((getattr(globals()[model_type] ,k).like(('%{0}%'.format(t))))))
                        q = q.filter(or_(*multior))
                    else:
                        q = q.filter(getattr(globals()[model_type] ,k).like('%{0}%'.format(v)))

            log.debug('Searching for {0} {1}'.format(c, s.rstrip(',')))

            total = q.count()
            myob = q.limit(perpage).offset(offset).all()
            results = {'meta': {'total': total}, 'results': myob}
            return results
        else:

            log.info('Displaying all {0}'.format(c))

            q = DBSession.query(globals()[model_type])
            total = q.count()
            myob =  q.limit(perpage).offset(offset).all()
            results = {'meta': {'total': total}, 'results': myob}
            return results

    # FIXME: Should AttributeError return something different?
    except (NoResultFound, AttributeError):
        return Response(content_type='application/json', status_int=404)

    except Exception, e:
        log.error('Error reading from {0} API={1},exception={2}'.format(c, request.url, e))
        return Response(str(e), content_type='application/json', status_int=500)