Example #1
0
    def test_filter_nodes_env(self):
        """Should filter nodes belonging to a given environment"""
        data = chef.filter_nodes(chef.get_nodes_extended(), 'production')
        self.assertEqual(len(data), 8)

        data = chef.filter_nodes(chef.get_nodes_extended(), 'staging')
        self.assertEqual(len(data), 1)

        data = chef.filter_nodes(chef.get_nodes_extended(), 'non_existing_env')
        self.assertEqual(len(data), 0)
Example #2
0
    def test_filter_nodes_virt(self):
        """Should filter nodes acording to their virt value"""
        total_guests = 7
        total_hosts = 3
        data = chef.filter_nodes(chef.get_nodes_extended(), virt_roles='guest')
        self.assertEqual(len(data), total_guests)

        data = chef.filter_nodes(chef.get_nodes_extended(), virt_roles='host')
        self.assertEqual(len(data), total_hosts)

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 virt_roles='host,guest')
        self.assertEqual(len(data), TOTAL_NODES)
Example #3
0
    def test_filter_nodes_combined(self):
        """Should filter nodes acording to their virt value"""
        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 env='production',
                                 roles='loadbalancer,webserver',
                                 virt_roles='guest')
        self.assertEqual(len(data), 3)
        self.assertEqual(data[0]['name'], "testnode1")
        self.assertEqual(data[1]['name'], "testnode2")
        self.assertEqual(data[2]['name'], "testnode7")

        data = chef.filter_nodes(chef.get_nodes_extended(), env='staging',
                                 roles='webserver', virt_roles='guest')
        self.assertEqual(len(data), 1)
        self.assertEqual(data[0]['name'], "testnode4")
Example #4
0
 def test_group_by_hosts_with_role(self):
     """Should group guests by hosts when giving a role filter"""
     data = chef.group_nodes_by_host(chef.get_nodes_extended(),
                                     roles='loadbalancer')
     self.assertEqual(len(data), 1)
     self.assertEqual(data[0]['name'], 'testnode9')
     self.assertEqual(len(data[0]['virtualization']['guests']), 3)
Example #5
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
Example #6
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
Example #7
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
Example #8
0
 def test_get_environments(self):
     """Should return a list of all chef_environment values found"""
     data = chef.get_environments(chef.get_nodes_extended())
     self.assertEqual(len(data), 3)
     expected = [{'counts': 1, 'name': 'none'},
                 {'counts': 8, 'name': 'production'},
                 {'counts': 1, 'name': 'staging'}]
     self.assertEqual(data, expected)
Example #9
0
 def test_group_by_hosts_with_env(self):
     """Should group guests in env by hosts when env is given"""
     data = chef.group_nodes_by_host(chef.get_nodes_extended(),
                                     env='staging')
     self.assertEqual(len(data), 1)
     for host in data:
         self.assertEqual(host['virtualization']['role'], 'host')
     expected_vms = ['testnode4']
     self.assertEqual(len(data[0]['virtualization']['guests']), 3)
Example #10
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")
Example #11
0
    def test_filter_nodes_roles(self):
        """Should filter nodes acording to their virt value"""
        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='dbserver')
        self.assertEqual(len(data), 2)
        self.assertEqual(data[0]['name'], "testnode3.mydomain.com")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='loadbalancer')
        self.assertEqual(len(data), 1)
        self.assertEqual(data[0]['name'], "testnode1")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='webserver')
        self.assertEqual(len(data), 4)
        self.assertEqual(data[0]['name'], "testnode2")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='webserver,dbserver')
        self.assertEqual(len(data), 6)
        self.assertEqual(data[1]['name'], "testnode3.mydomain.com")
Example #12
0
 def test_group_by_hosts_without_filter_by_role(self):
     """Should group guests by hosts when no role is given"""
     data = chef.group_nodes_by_host(chef.get_nodes_extended(), roles='')
     self.assertEqual(len(data), 2)
     self.assertEqual(data[0]['name'], 'testnode5')
     vms = data[0]['virtualization']['guests']
     expected_vms = ['testnode7', 'testnode8']
     self.assertEqual(len(vms), len(expected_vms))
     for vm in vms:
         fqdn = vm['fqdn']
         self.assertTrue(fqdn in expected_vms)
         expected_vms.remove(fqdn)
Example #13
0
 def test_group_by_hosts(self):
     """Should group all guests by hosts when called without arguments"""
     data = chef.group_nodes_by_host(chef.get_nodes_extended())
     self.assertEqual(len(data), 3)
     for host in data:
         self.assertEqual(host['virtualization']['role'], 'host')
     self.assertEqual(data[2]['name'], "testnode9")
     vms = data[2]['virtualization']['guests']
     expected_vms = ['testnode1', 'testnode2', 'testnode4']
     self.assertEqual(len(vms), len(expected_vms))
     for vm in vms:
         fqdn = vm['fqdn']
         self.assertTrue(fqdn in expected_vms)
         expected_vms.remove(fqdn)
Example #14
0
 def test_filter_nodes_all(self):
     """Should return all nodes when empty filters are are given"""
     data = chef.filter_nodes(chef.get_nodes_extended(), '', '')
     self.assertEqual(len(data), TOTAL_NODES)