Ejemplo n.º 1
0
def _get_data(request, env, roles, virt, group_by_host=False):
    """Returns processed repository data, filtering nodes based on given args
    """
    roles = [role for role in roles.split(',') if role]
    data = {
        'filter_env': env, 'filter_roles': roles, 'filter_virt': virt,
        'show_list': SHOW_LIST_VIEW, 'show_virt': SHOW_VIRT_VIEW,
        'show_graph': SHOW_GRAPH_VIEW, 'show_links': SHOW_LINKS,
        'query_string': request.META['QUERY_STRING']
    }
    data['roles'] = get_roles()
    roles_groups = get_role_groups(data['roles'])
    data['roles_groups'] = roles_groups
    data['virt_roles'] = ['host', 'guest']
    # Get environments before we filter nodes
    data['nodes'] = get_nodes()
    data['nodes_extended'] = get_nodes_extended(data['nodes'])
    data['environments'] = get_environments(data['nodes_extended'])
    if group_by_host:
        data['nodes_extended'] = group_nodes_by_host(data['nodes_extended'],
                                                     roles=data['filter_roles'],
                                                     env=data['filter_env'])
    elif data['filter_env'] or data['filter_roles'] or data['filter_virt']:
        data['nodes_extended'] = filter_nodes(data['nodes_extended'],
                                              data['filter_env'],
                                              data['filter_roles'],
                                              data['filter_virt'])
    inject_plugin_data(data['nodes_extended'])
    if not data['nodes_extended']:
        add_message(request, WARNING,
                    "There are no nodes that fit the supplied criteria.")
    return data
Ejemplo n.º 2
0
def plugins(request, name, method, plugin_type='list'):
    """Plugin interface view which either response with the page created by the
    plugin method, or returns a 404 HTTP Error

    """
    try:
        plugin = PLUGINS[name]
    except KeyError:
        raise Http404("Requested plugin '{0}' not found".format(name))
    try:
        func = getattr(plugin, method)
    except AttributeError:
        raise Http404("Plugin '{0}' has no method '{1}'".format(name, method))
    if not getattr(func, '__is_view__', False):
        raise Http404("Plugin method '{0}.{1}' ""is not defined "
                      "as a view".format(name, method))
    nodes = get_nodes()
    nodes = get_nodes_extended(nodes)
    if plugin_type in ('v', 'virt'):
        if func.__p_type__ != 'virt':
            raise Http404("Plugin '{0}.{1}' has wrong "
                          "type".format(name, method))
        nodes = group_nodes_by_host(nodes, roles=None)
    elif func.__p_type__ != 'list':
        raise Http404("Plugin '{0}.{1}' has wrong type".format(name, method))
    inject_plugin_data(nodes)
    try:
        result = func(request, nodes)
    except TypeError:
        raise Http404("Failed running plugin '{0}.{1}'".format(name, method))
    if not isinstance(result, HttpResponse):
        raise Http404("Plugin '{0}.{1}' returned unexpected result: "
                      "{2}".format(name, method, result))
    else:
        return result
Ejemplo n.º 3
0
def _get_data(request, env, roles, virt, group_by_host=False):
    """Returns processed repository data, filtering nodes based on given args
    """
    data = {'filter_env': env, 'filter_roles': roles, 'filter_virt': virt}
    data['roles'] = get_roles()
    roles_groups = get_role_groups(data['roles'])
    data['roles_groups'] = roles_groups
    data['virt_roles'] = ['host', 'guest']
    # Get environments before we filter nodes
    data['nodes'] = get_nodes()
    data['nodes_extended'] = get_nodes_extended(data['nodes'])
    data['environments'] = get_environments(data['nodes_extended'])
    roles_to_filter = '' if group_by_host else data['filter_roles']
    if data['filter_env'] or roles_to_filter or data['filter_virt']:
        data['nodes_extended'] = filter_nodes(data['nodes_extended'],
                                              data['filter_env'],
                                              roles_to_filter,
                                              data['filter_virt'])
    if group_by_host:
        data['nodes_extended'] = group_nodes_by_host(
            data['nodes_extended'], roles=data['filter_roles'])
    inject_plugin_data(data['nodes_extended'])
    if not data['nodes_extended']:
        add_message(request, WARNING,
                    "There are no nodes that fit the supplied criteria.")
    return data
Ejemplo n.º 4
0
def get_nodes(request):
    """Returns node files. If 'extended' is given, the extended version is
    returned

    """
    if request.GET.get('extended'):
        data = chef.get_nodes_extended()
    else:
        data = chef.get_nodes()
    data = chef.filter_nodes(data, request.GET.get('env'))
    return HttpResponse(json.dumps(data), content_type="application/json")
Ejemplo n.º 5
0
 def test_get_nodes(self):
     """Should return all nodes when calling get_nodes()"""
     data = chef.get_nodes()
     self.assertEqual(len(data), TOTAL_NODES)
     self.assertTrue(data[1]['name'].startswith('testnode'))
Ejemplo n.º 6
0
 def test_get_nodes(self):
     """Should return all nodes when calling get_nodes()"""
     data = chef.get_nodes()
     self.assertEqual(len(data), TOTAL_NODES)
     self.assertEqual(data[1]['name'], "testnode2")