def create_network_pool(self, networks, prefix): """ :rtype : IpNetworksPool """ pool = IpNetworksPool(networks=networks, prefix=prefix) pool.set_allocated_networks(ExternalModel.get_allocated_networks()) return pool
def test_getting_subnetworks(self): pool = IpNetworksPool(networks=[IPNetwork('10.1.0.0/22')], prefix=24) pool.set_allocated_networks([IPv4Network('10.1.1.0/24')]) networks = list(pool) self.assertTrue(IPv4Network('10.1.0.0/24') in networks) self.assertFalse(IPv4Network('10.1.1.0/24') in networks) self.assertTrue(IPv4Network('10.1.2.0/24') in networks) self.assertTrue(IPv4Network('10.1.3.0/24') in networks)
def create_network_pool(cls, networks, prefix): """Create network pool :rtype : IpNetworksPool """ pool = IpNetworksPool(networks=networks, prefix=prefix) pool.set_allocated_networks(cls.get_driver().get_allocated_networks()) return pool
def test_network_model(self): environment = Environment.create('test_env') node = Node.objects.create( group=None, name='test_node', role='default', ) pool = IpNetworksPool(networks=[IPNetwork('10.1.0.0/24')], prefix=24) address_pool = AddressPool.address_pool_create(environment=environment, name='internal', pool=pool) l2_net_dev = L2NetworkDevice.objects.create(group=None, address_pool=address_pool, name='test_l2_dev') interface1 = Interface.interface_create(l2_network_device=l2_net_dev, node=node, label='eth0') assert interface1.model == 'virtio' interface2 = Interface.interface_create(l2_network_device=l2_net_dev, node=node, label='eth0', model='e1000') assert interface2.model == 'e1000'
def test_getting_subnetworks(self): pool = IpNetworksPool([IPNetwork('10.1.0.0/22')], 24) networks = list(pool) assert len(networks) == 4 assert (IPNetwork('10.1.0.0/24') in networks) is True assert (IPNetwork('10.1.1.0/24') in networks) is True assert (IPNetwork('10.1.2.0/24') in networks) is True assert (IPNetwork('10.1.3.0/24') in networks) is True
def test_getting_subnetworks_allocated(self): pool = IpNetworksPool( networks=[IPNetwork('10.1.0.0/22')], prefix=24, allocated_networks=[ IPNetwork('10.1.1.0/24'), IPNetwork('10.1.3.0/24'), ]) networks = list(pool) assert len(networks) == 2 assert (IPNetwork('10.1.0.0/24') in networks) is True assert (IPNetwork('10.1.1.0/24') not in networks) is True assert (IPNetwork('10.1.2.0/24') in networks) is True assert (IPNetwork('10.1.3.0/24') not in networks) is True
def add_address_pool(self, name, net, **params): networks, prefix = net.split(':') ip_networks = [IPNetwork(x) for x in networks.split(',')] pool = IpNetworksPool(networks=ip_networks, prefix=int(prefix), allocated_networks=self.get_allocated_networks()) return AddressPool.address_pool_create(environment=self, name=name, pool=pool, **params)
def address_pool_create(cls, name, environment, pool=None, **params): """Create network :rtype : Network """ if pool is None: pool = IpNetworksPool( networks=[IPNetwork('10.0.0.0/16')], prefix=24, allocated_networks=environment.get_allocated_networks()) address_pool = cls._safe_create_network(environment=environment, name=name, pool=pool, **params) # Translate indexes into IP addresses for ip_reserved and ip_ranges def _relative_to_ip(ip_network, ip_id): """Get an IP from IPNetwork ip's list by index :param ip_network: IPNetwork object :param ip_id: string, if contains '+' or '-' then it is used as index of an IP address in ip_network, else it is considered as IP address. :rtype : str(IP) """ if isinstance(ip_id, int): return str(ip_network[int(ip_id)]) else: return str(ip_id) if 'ip_reserved' in params: for ip_res in params['ip_reserved'].keys(): ip = _relative_to_ip(address_pool.ip_network, params['ip_reserved'][ip_res]) params['ip_reserved'][ip_res] = ip # Store to template address_pool.ip_reserved[ip_res] = ip # Store to the object if 'ip_ranges' in params: for ip_range in params['ip_ranges']: ipr_start = _relative_to_ip(address_pool.ip_network, params['ip_ranges'][ip_range][0]) ipr_end = _relative_to_ip(address_pool.ip_network, params['ip_ranges'][ip_range][1]) params['ip_ranges'][ip_range] = (ipr_start, ipr_end) address_pool.ip_ranges[ip_range] = (ipr_start, ipr_end) address_pool.save() return address_pool
def test_node_creation(self): environment = Environment.create('test_env3') pool = IpNetworksPool(networks=[IPNetwork('10.1.0.0/24')], prefix=24) address_pool = AddressPool.address_pool_create(environment=environment, name='internal', pool=pool) l2_net_dev = L2NetworkDevice.objects.create(group=None, address_pool=address_pool, name='test_l2_dev') node = Node.objects.create( group=None, name='test_node', role='default', ) Interface.interface_create(l2_network_device=l2_net_dev, node=node, label='eth0') environment.define()
def test_network_iterator(self): environment = Environment.create('test_env') node = Node.objects.create( group=None, name='test_node', role='default', ) pool = IpNetworksPool(networks=[IPNetwork('10.1.0.0/24')], prefix=24) address_pool = AddressPool.address_pool_create(environment=environment, name='internal', pool=pool) l2_net_dev = L2NetworkDevice.objects.create(group=None, address_pool=address_pool, name='test_l2_dev') interface = Interface.interface_create(l2_network_device=l2_net_dev, node=node, label='eth0') assert str(address_pool.next_ip()) == '10.1.0.3' Address.objects.create(ip_address=str('10.1.0.3'), interface=interface) assert str(address_pool.next_ip()) == '10.1.0.4' Address.objects.create(ip_address=str('10.1.0.4'), interface=interface) assert str(address_pool.next_ip()) == '10.1.0.5'