def test_report_view(self): host_user = self.create_node('AwesomeCo', 'host-user', 'Relation') host = self.create_node('sweet-host.nordu.net', 'host', 'Logical') host_node = host.get_node() set_noclook_auto_manage(host_node, True) set_user(self.user, host.get_node(), host_user.handle_id) url = reverse('host_users_report') resp = self.client.get(url) self.assertContains(resp, host.node_name)
def test_visualize_json(self): host_user = self.create_node('AwesomeCo', 'host-user', 'Relation') host = self.create_node('sweet-host.nordu.dev', 'host', 'Logical') helpers.set_user(self.user, host.get_node(), host_user.handle_id) url = reverse('visualize_json', args=[host.handle_id]) resp = self.client.get(url) json = resp.json() self.assertEqual(2, len(json['nodes'])) self.assertEqual(1, len(json['edges'])) self.assertIn(str(host_user.handle_id), json['edges'].keys()) self.assertIn(host.node_name, json['nodes'][str(host.handle_id)]['label'])
def edit_pdu(request, handle_id): # Get needed data from node nh, pdu = helpers.get_nh_node(handle_id) location = pdu.get_location() relations = pdu.get_relations() depends_on = pdu.get_dependencies() host_services = pdu.get_host_services() ports = pdu.get_ports() dependency_categories = 'service' ports_form = forms.BulkPortsForm(request.POST or None) if request.POST: form = forms.EditPDUForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, pdu.handle_id, form) # Host specific updates if form.cleaned_data['relationship_user']: user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user']) helpers.set_user(request.user, pdu, user_nh.handle_id) if form.cleaned_data['relationship_owner']: owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_owner']) helpers.set_owner(request.user, pdu, owner_nh.handle_id) # You can not set location and depends on at the same time if form.cleaned_data['relationship_depends_on']: depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_depends_on']) helpers.set_depends_on(request.user, pdu, depends_on_nh.handle_id) elif form.cleaned_data['relationship_location']: _handle_location(request.user, pdu, form.cleaned_data['relationship_location']) if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']: helpers.remove_rogue_service_marker(request.user, pdu.handle_id) if ports_form.is_valid() and not ports_form.cleaned_data['no_ports']: data = ports_form.cleaned_data helpers.bulk_create_ports(pdu, request.user, **data) _handle_ports(pdu, form.cleaned_data['relationship_ports'], request.user) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditPDUForm(pdu.data) return render(request, 'noclook/edit/edit_pdu.html', {'node_handle': nh, 'node': pdu, 'form': form, 'location': location, 'relations': relations, 'depends_on': depends_on, 'ports': ports, 'host_services': host_services, 'dependency_categories': dependency_categories, 'ports_form': ports_form})
def edit_host(request, handle_id): # Get needed data from node nh, host = helpers.get_nh_node(handle_id) location = host.get_location() relations = host.get_relations() depends_on = host.get_dependencies() host_services = host.get_host_services() ports = host.get_ports() dependency_categories = 'service,host' if request.POST: form = forms.EditHostForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, host.handle_id, form) # Host specific updates if form.cleaned_data['relationship_user']: user_nh = _nh_safe_get(form.cleaned_data['relationship_user']) if user_nh: helpers.set_user(request.user, host, user_nh.handle_id) if form.cleaned_data['relationship_owner']: owner_nh = _nh_safe_get(form.cleaned_data['relationship_owner']) if owner_nh: helpers.set_owner(request.user, host, owner_nh.handle_id) # You can not set location and depends on at the same time if form.cleaned_data['relationship_depends_on']: depends_on_nh = _nh_safe_get(form.cleaned_data['relationship_depends_on']) if depends_on_nh: helpers.set_depends_on(request.user, host, depends_on_nh.handle_id) elif form.cleaned_data['relationship_location']: _handle_location(request.user, host, form.cleaned_data['relationship_location']) if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']: helpers.remove_rogue_service_marker(request.user, host.handle_id) _handle_ports(host, form.cleaned_data['relationship_ports'], request.user) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditHostForm(host.data) context = { 'node_handle': nh, 'node': host, 'form': form, 'location': location, 'relations': relations, 'depends_on': depends_on, 'ports': ports, 'host_services': host_services, 'dependency_categories': dependency_categories } return render(request, 'noclook/edit/edit_host.html', context)
def add_host_user(host): """ Tries to set a Uses or Owns relationship between the Host and a Host User if there are none. """ user = nlu.get_user() host_name = host.data['name'] domain = extract_domain(host_name) stakeholder = HostUserMap.objects.filter(domain=domain).first() relations = host.get_relations() if stakeholder and not (relations.get('Uses',None) or relations.get('Owns', None)): stakeholder_nh = nlu.get_unique_node_handle(stakeholder.host_user, 'Host User', 'Relation') if host.meta_type == 'Logical': helpers.set_user(user, host, stakeholder_nh.handle_id) elif host.meta_type == 'Physical': helpers.set_owner(user, host, stakeholder_nh.handle_id)
def set_host_user(host): """ Tries to set a Uses or Owns relationship between the Host and a Host User if there are none. """ user = utils.get_user() domain = '.'.join(host.data['name'].split('.')[-2:]) relations = host.get_relations() host_user_name = HOST_USERS_MAP.get(domain, None) if host_user_name and not (relations.get('Uses', None) or relations.get('Owns', None)): relation_node_handle = utils.get_unique_node_handle(host_user_name, 'Host User', 'Relation') if host.meta_type == 'Logical': helpers.set_user(user, host, relation_node_handle.handle_id) elif host.meta_type == 'Physical': helpers.set_owner(user, host, relation_node_handle.handle_id) logger.info('Host User {user_name} set for host {host_name}.'.format(user_name=host_user_name, host_name=host.data['name']))
def edit_service(request, handle_id): # Get needed data from node nh, service = helpers.get_nh_node(handle_id) relations = service.get_relations() depends_on = service.get_dependencies() dependency_categories = ['host', 'firewall', 'odf', 'optical-node', 'optical-path', 'optical-filter', 'router', 'service', 'switch', 'external-equipment'] user_categories = ['customer', 'end-user'] if request.POST: form = forms.EditServiceForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, service.handle_id, form) # Service node updates if form.cleaned_data['relationship_provider']: owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_provider']) helpers.set_provider(request.user, service, owner_nh.handle_id) if form.cleaned_data['relationship_user']: user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user']) helpers.set_user(request.user, service, user_nh.handle_id) if form.cleaned_data['relationship_depends_on']: depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_depends_on']) helpers.set_depends_on(request.user, service, depends_on_nh.handle_id) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditServiceForm(service.data) return render(request, 'noclook/edit/edit_service.html', {'node_handle': nh, 'form': form, 'node': service, 'relations': relations, 'depends_on': depends_on, 'dependency_categories': dependency_categories, 'user_categories': user_categories})
def insert_hosts(json_list): user = utils.get_user() node_type = 'Host' meta_type = 'Logical' sunet_user = utils.get_unique_node_handle('SUNET', 'Host User', 'Relation') for item in json_list: name = item['host']['name'] data = item['host'].get('nunoc_cosmos', {}) if not address_is_a(data.get('addresses', []), ALLOWED_NODE_TYPE_SET): logger.info( '%s had an address that belongs to something that is not a host', name) continue node_handle = utils.get_unique_node_handle_by_name( name, node_type, meta_type, ALLOWED_NODE_TYPE_SET) if not node_handle or node_handle.node_type.type not in ALLOWED_NODE_TYPE_SET: logger.warning("%s is not in %s", name, ALLOWED_NODE_TYPE_SET) continue node = node_handle.get_node() helpers.update_noclook_auto_manage(node) properties = { 'ip_addresses': data.get('addresses', []), 'sunet_iaas': data.get('sunet_iaas', False) } if data.get('managed_by'): properties['managed_by'] = data.get('managed_by') # Set operational state if it is missing if not node.data.get('operational_state', None) and properties['ip_addresses']: properties['operational_state'] = 'In service' # Update host node helpers.dict_update_node(user, node.handle_id, properties) if data.get('sunet_iaas', False): if node.meta_type == 'Logical': helpers.set_user(user, node, sunet_user.handle_id) elif node.meta_type == 'Physical': helpers.set_owner(user, node, sunet_user.handle_id) logger.info("%s has been imported", name)
def edit_firewall(request, handle_id): # Get needed data from node nh, firewall = helpers.get_nh_node(handle_id) location = firewall.get_location() relations = firewall.get_relations() host_services = firewall.get_host_services() ports = firewall.get_ports() if request.POST: form = forms.EditFirewallForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, firewall.handle_id, form) # Firewall specific updates if form.cleaned_data['relationship_user']: user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user']) helpers.set_user(request.user, firewall, user_nh.handle_id) if form.cleaned_data['relationship_owner']: owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_owner']) helpers.set_owner(request.user, firewall, owner_nh.handle_id) _handle_location(request.user, firewall, form.cleaned_data['relationship_location']) if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']: helpers.remove_rogue_service_marker(request.user, firewall.handle_id) _handle_ports(firewall, form.cleaned_data['relationship_ports'], request.user) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditFirewallForm(firewall.data) return render(request, 'noclook/edit/edit_firewall.html', {'node_handle': nh, 'node': firewall, 'form': form, 'location': location, 'relations': relations, 'ports': ports, 'host_services': host_services})
def add_user(self, user, main_logical_nh, usr_relation_nh): helpers.set_user(user, main_logical_nh.get_node(), usr_relation_nh.handle_id)
def link_uses(cls, user, relation_nh, logical_nh): helpers.set_user(user, logical_nh.get_node(), relation_nh.handle_id)