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
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)
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
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
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)
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)
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)