def test_correct_subnet_and_ip(self, session, subnet, interface): ip_address, _ = get_free_ip((subnet, )) with session.begin_nested(): session.add(IP(interface=interface, address=ip_address, subnet=subnet)) ip_address, _ = get_free_ip((subnet, )) with session.begin_nested(): session.add(IP(address=ip_address, subnet=subnet, interface=interface)) with session.begin_nested(): IP.q.filter(IP.interface == interface).delete()
def interface_create(host, name, mac, ips, processor): interface = Interface(host=host, mac=mac, name=name) session.add(interface) subnets = get_subnets_for_room(interface.host.room) if ips is None: ip, _ = get_free_ip(subnets) ips = {ip} # IP added for ip in ips: subnet = next( iter([subnet for subnet in subnets if (ip in subnet.address)]), None) if subnet is not None: session.add(IP(interface=interface, address=ip, subnet=subnet)) message = deferred_gettext( u"Created interface ({}, {}) with name '{}' for host '{}'.".format( interface.mac, ', '.join(str(ip.address) for ip in interface.ips), interface.name, interface.host.name)) log_user_event(author=processor, user=host.owner, message=message.to_json()) return interface
def migrate_user_host(host, new_room, processor): """ Migrate a UserHost to a new room and if necessary to a new subnet. If the host changes subnet, it will get a new IP address. :param Host host: Host to be migrated :param Room new_room: new room of the host :param User processor: User processing the migration :return: """ old_room = host.room host.room = new_room subnets = [ subnet for p in new_room.connected_patch_ports for vlan in p.switch_port.default_vlans for subnet in vlan.subnets ] if old_room.building_id == new_room.building_id: return for interface in host.interfaces: old_ips = tuple(ip for ip in interface.ips) for old_ip in old_ips: ip_address, subnet = get_free_ip(subnets) new_ip = IP(interface=interface, address=ip_address, subnet=subnet) session.session.add(new_ip) old_address = old_ip.address session.session.delete(old_ip) message = deferred_gettext( u"Changed IP from {old_ip} to {new_ip}.").format( old_ip=str(old_address), new_ip=str(new_ip)) log_user_event(author=processor, user=host.owner, message=message.to_json())
def test_missing_subnet(self): ip_address, _ = get_free_ip((self.subnet, )) ip = host.IP(interface=self.interface, address=ip_address) with self.assertRaises(IntegrityError): self.session.add(ip) self.session.commit()
def pick_ip(self): ip, _ = get_free_ip((self.subnet, )) addr = host.IP(interface=self.interface, address=ip, subnet=self.subnet) self.session.add(addr) self.session.commit() return addr
def setup_ipv4_networking(host): """Add suitable ips for every interface of a host""" subnets = get_subnets_for_room(host.room) for interface in host.interfaces: ip_address, subnet = get_free_ip(subnets) new_ip = IP(interface=interface, address=ip_address, subnet=subnet) session.session.add(new_ip)
def test_correct_subnet_and_ip(self): ip_address, _ = get_free_ip((self.subnet, )) ip = host.IP(interface=self.interface, address=ip_address, subnet=self.subnet) self.session.add(ip) self.session.commit() ip_address, _ = get_free_ip((self.subnet, )) ip = host.IP(address=ip_address, subnet=self.subnet, interface=self.interface) self.session.add(ip) self.session.commit() host.IP.q.filter(host.IP.interface == self.interface).delete() self.session.commit()
def test_get_free_ip_next_to_full(self): first_net = self.subnets[0] second_net = self.subnets[1] subnets = (first_net, second_net) host = self.host interface = host.interfaces[0] self.fill_net(first_net, interface) self.session.refresh(first_net) self.assertRaises(SubnetFullException, get_free_ip, (first_net,)) try: get_free_ip(subnets) except SubnetFullException: self.fail("Subnets should have free IPs.") self.fill_net(subnets[1], interface) self.assertRaises(SubnetFullException, get_free_ip, subnets) self.session.delete(host) self.session.commit()
def test_0010_correct_subnet_and_ip(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(interface=interface) ip.address = ip_address ip.subnet = subnet session.session.add(ip) session.session.commit() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(address=ip_address, subnet=subnet, interface=interface) session.session.add(ip) session.session.commit() host.IP.q.filter(host.IP.interface == interface).delete() session.session.commit()
def test_0010_correct_subnet_and_ip(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(interface=interface) ip.address = ip_address ip.subnet = subnet session.session.add(ip) session.session.commit() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(address=ip_address, subnet=subnet, interface=interface) session.session.add(ip) session.session.commit() host.IP.q.filter(host.IP.interface == interface).delete() session.session.commit()
def setup_ipv4_networking(host): """Add suitable ips for every interface of a host""" subnets = [ s for p in host.room.connected_patch_ports for v in p.switch_port.default_vlans for s in v.subnets if s.address.version == 4 ] for interface in host.interfaces: ip_address, subnet = get_free_ip(subnets) new_ip = IP(interface=interface, address=ip_address, subnet=subnet) session.session.add(new_ip)
def test_0020_missing_subnet(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(interface=interface) ip.address = ip_address def commit(): session.session.add(ip) session.session.commit() self.assertRaises(IntegrityError, commit)
def test_0020_missing_subnet(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnet, )) ip = host.IP(interface=interface) ip.address = ip_address def commit(): session.session.add(ip) session.session.commit() self.assertRaises(IntegrityError, commit)
def test_0040_delete_subnet(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip, _ = get_free_ip((subnet, )) ip_addr = host.IP(interface=interface, address=ip, subnet=subnet) session.session.add(ip_addr) session.session.commit() with self.assertRaises(IntegrityError): ip_addr.subnet = None self.assertIsNone(ip_addr.subnet) session.session.commit()
def test_0030_get_free_ip_next_to_full(self): first_net = Subnet.q.filter_by( address=SubnetData.user_ipv4.address).one() second_net = Subnet.q.filter_by( address=SubnetData.dummy_subnet2.address).one() subnets = (first_net, second_net) owner = User.q.filter_by(login=HostData.dummy.owner.login).one() host = Host.q.filter_by(owner=owner).one() interface = host.interfaces[0] self.fill_net(first_net, interface) session.session.refresh(first_net) self.assertRaises(SubnetFullException, get_free_ip, (first_net,)) try: get_free_ip(subnets) except SubnetFullException: self.fail("Subnets should have free IPs.") self.fill_net(subnets[1], interface) self.assertRaises(SubnetFullException, get_free_ip, subnets) session.session.delete(host) session.session.commit()
def test_0040_delete_subnet(self): subnet = Subnet.q.first() interface = host.Interface.q.first() ip, _ = get_free_ip((subnet, )) ip_addr = host.IP(interface=interface, address=ip, subnet=subnet) session.session.add(ip_addr) session.session.commit() with self.assertRaises(IntegrityError): ip_addr.subnet = None self.assertIsNone(ip_addr.subnet) session.session.commit()
def test_0030_get_free_ip_next_to_full(self): first_net = Subnet.q.filter_by( address=SubnetData.user_ipv4.address).one() second_net = Subnet.q.filter_by( address=SubnetData.dummy_subnet2.address).one() subnets = (first_net, second_net) owner = User.q.filter_by(login=HostData.dummy.owner.login).one() host = Host.q.filter_by(owner=owner).one() interface = host.interfaces[0] self.fill_net(first_net, interface) session.session.refresh(first_net) self.assertRaises(SubnetFullException, get_free_ip, (first_net, )) try: get_free_ip(subnets) except SubnetFullException: self.fail("Subnets should have free IPs.") self.fill_net(subnets[1], interface) self.assertRaises(SubnetFullException, get_free_ip, subnets) session.session.delete(host) session.session.commit()
def test_wrong_subnet(self, interface, subnet, subnet2): ip_address, _ = get_free_ip((subnet, )) ip = IP(interface=interface, address=ip_address) with pytest.raises(ValueError): ip.subnet = subnet2 ip = IP(interface=interface, subnet=subnet2) with pytest.raises(ValueError): ip.address = ip_address with pytest.raises(ValueError): IP(interface=interface, subnet=subnet2, address=ip_address)
def test_0040_wrong_subnet(self): subnets = Subnet.q.all() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnets[0], )) ip = host.IP(interface=interface, address=ip_address) with self.assertRaises(ValueError): ip.subnet = subnets[1] ip = host.IP(interface=interface, subnet=subnets[1]) with self.assertRaises(ValueError): ip.address = ip_address with self.assertRaises(ValueError): host.IP(interface=interface, subnet=subnets[1], address=ip_address)
def test_0040_wrong_subnet(self): subnets = Subnet.q.all() interface = host.Interface.q.first() ip_address, _ = get_free_ip((subnets[0], )) ip = host.IP(interface=interface, address=ip_address) with self.assertRaises(ValueError): ip.subnet = subnets[1] ip = host.IP(interface=interface, subnet=subnets[1]) with self.assertRaises(ValueError): ip.address = ip_address with self.assertRaises(ValueError): host.IP(interface=interface, subnet=subnets[1], address=ip_address)
def migrate_user_host(host, new_room, processor): """ Migrate a UserHost to a new room and if necessary to a new subnet. If the host changes subnet, it will get a new IP address. :param Host host: Host to be migrated :param Room new_room: new room of the host :param User processor: User processing the migration :return: """ old_room = host.room host.room = new_room subnets_old = get_subnets_for_room(old_room) subnets = get_subnets_for_room(new_room) if subnets_old != subnets: for interface in host.interfaces: old_ips = tuple(ip for ip in interface.ips) for old_ip in old_ips: ip_address, subnet = get_free_ip(subnets) new_ip = IP(interface=interface, address=ip_address, subnet=subnet) session.session.add(new_ip) old_address = old_ip.address session.session.delete(old_ip) message = deferred_gettext( u"Changed IP of {mac} from {old_ip} to {new_ip}.").format( old_ip=str(old_address), new_ip=str(new_ip.address), mac=interface.mac) log_user_event(author=processor, user=host.owner, message=message.to_json()) message = deferred_gettext( u"Moved host '{name}' from {room_old} to {room_new}.".format( name=host.name, room_old=old_room.short_name, room_new=new_room.short_name)) log_user_event(author=processor, user=host.owner, message=message.to_json())
def test_wrong_subnet(self): ip_address, _ = get_free_ip((self.subnets[0], )) ip = host.IP(interface=self.interface, address=ip_address) with self.assertRaises(ValueError): ip.subnet = self.subnets[1] ip = host.IP(interface=self.interface, subnet=self.subnets[1]) with self.assertRaises(ValueError): ip.address = ip_address with self.assertRaises(ValueError): host.IP(interface=self.interface, subnet=self.subnets[1], address=ip_address)
def test_0010_get_free_ip_simple(self): subnets = Subnet.q.all() for subnet in subnets: ip, subnet = get_free_ip((subnet,)) self.assertIn(ip, subnet.address)
def test_get_free_ip_simple(self): for subnet in self.subnets: ip, subnet = get_free_ip((subnet,)) self.assertIn(ip, subnet.address)
def test_missing_subnet(self, session, interface, subnet): ip_address, _ = get_free_ip((subnet, )) ip = IP(interface=interface, address=ip_address) with pytest.raises(IntegrityError), session.begin_nested(): session.add(ip)
def ip_addr(self, class_session, subnet, interface): ip, _ = get_free_ip((subnet,)) addr = IP(interface=interface, address=ip, subnet=subnet) class_session.add(addr) return addr
def fill_net(self, net, interface): for num in range(0, self.calculate_usable_ips(net)): ip, _ = get_free_ip((net,)) session.session.add(IP(address=ip, subnet=net, interface=interface)) session.session.commit()
def fill_net(self, net, interface): for num in range(0, self.calculate_usable_ips(net)): ip, _ = get_free_ip((net, )) session.session.add(IP(address=ip, subnet=net, interface=interface)) session.session.commit()
def test_0010_get_free_ip_simple(self): subnets = Subnet.q.all() for subnet in subnets: ip, subnet = get_free_ip((subnet, )) self.assertIn(ip, subnet.address)