def _mock_subnet(self, subnet_pool, requested_pools, cidr, gateway): ip_version = 4 subnetpool_id = subnet_pool['id]'] if subnet_pool else None # if pools is passed, then allocation_pools should be a request format # which is IPRange type, but if not passed, then subnet format is # subnet dict. if requested_pools is None: requested_pools = ipam_utils.generate_pools(cidr, gateway) allocation_pools = [ {'start': str(netaddr.IPAddress(p.first, ip_version)), 'end': str(netaddr.IPAddress(p.last, ip_version))} for p in requested_pools] else: allocation_pools = requested_pools return { 'id': 'subnet-id', 'cidr': cidr, 'tenant_id': 'tenant-id', 'gateway_ip': gateway, 'name': 'subnet-name', 'network_id': 'network-id', 'subnetpool_id': subnetpool_id, 'allocation_pools': allocation_pools, 'ip_version': ip_version, 'enable_dhcp': True}
def create_from_subnet_request(cls, subnet_request, ctx): ipam_subnet_id = uuidutils.generate_uuid() subnet_manager = ipam_db_api.IpamSubnetManager( ipam_subnet_id, subnet_request.subnet_id) # Create subnet resource subnet_manager.create(ctx) # If allocation pools are not specified, define them around # the subnet's gateway IP if not subnet_request.allocation_pools: pools = ipam_utils.generate_pools(subnet_request.subnet_cidr, subnet_request.gateway_ip) else: pools = subnet_request.allocation_pools # Create IPAM allocation pools cls.create_allocation_pools(subnet_manager, ctx, pools, subnet_request.subnet_cidr) return cls(ipam_subnet_id, ctx, cidr=subnet_request.subnet_cidr, allocation_pools=pools, gateway_ip=subnet_request.gateway_ip, tenant_id=subnet_request.tenant_id, subnet_id=subnet_request.subnet_id)
def create_from_subnet_request(cls, subnet_request, ctx): ipam_subnet_id = uuidutils.generate_uuid() subnet_manager = ipam_db_api.IpamSubnetManager( ipam_subnet_id, None) # Create subnet resource session = ctx.session subnet_manager.create(session) # If allocation pools are not specified, define them around # the subnet's gateway IP if not subnet_request.allocation_pools: pools = ipam_utils.generate_pools(subnet_request.subnet_cidr, subnet_request.gateway_ip) else: pools = subnet_request.allocation_pools # Create IPAM allocation pools and availability ranges cls.create_allocation_pools(subnet_manager, session, pools) return cls(ipam_subnet_id, ctx, cidr=subnet_request.subnet_cidr, allocation_pools=pools, gateway_ip=subnet_request.gateway_ip, tenant_id=subnet_request.tenant_id, subnet_id=subnet_request.subnet_id, subnet_id_not_set=True)
def _prepare_allocation_pools(self, allocation_pools, cidr, gateway_ip): """Returns allocation pools represented as list of IPRanges""" if not attributes.is_attr_set(allocation_pools): return ipam_utils.generate_pools(cidr, gateway_ip) ip_range_pools = self.pools_to_ip_range(allocation_pools) self._validate_allocation_pools(ip_range_pools, cidr) if gateway_ip: self.validate_gw_out_of_pools(gateway_ip, ip_range_pools) return ip_range_pools
def _create_ib_ip_range(self, rollback_list): subnet = self.ib_cxt.subnet cidr = subnet.get('cidr') ip_version = subnet.get('ip_version') gateway_ip = subnet.get('gateway_ip') allocation_pools = subnet.get('allocation_pools') if not allocation_pools: allocation_pools = ipam_utils.generate_pools(cidr, gateway_ip) self._allocate_pools(rollback_list, allocation_pools, cidr, ip_version, True)
def _prepare_allocation_pools(self, allocation_pools, cidr, gateway_ip): """Returns allocation pools represented as list of IPRanges""" if not attributes.is_attr_set(allocation_pools): return ipam_utils.generate_pools(cidr, gateway_ip) self._validate_allocation_pools(allocation_pools, cidr) if gateway_ip: self.validate_gw_out_of_pools(gateway_ip, allocation_pools) return [netaddr.IPRange(p['start'], p['end']) for p in allocation_pools]
def _allocate_pools_for_subnet(self, context, subnet): """Create IP allocation pools for a given subnet Pools are defined by the 'allocation_pools' attribute, a list of dict objects with 'start' and 'end' keys for defining the pool range. """ pools = ipam_utils.generate_pools(subnet['cidr'], subnet['gateway_ip']) return [{'start': str(netaddr.IPAddress(pool.first)), 'end': str(netaddr.IPAddress(pool.last))} for pool in pools]
def _allocate_any_subnet(self, request): with self._context.session.begin(subtransactions=True): self._lock_subnetpool() self._check_subnetpool_tenant_quota(request.tenant_id, request.prefixlen) prefix_pool = self._get_available_prefix_list() for prefix in prefix_pool: if request.prefixlen >= prefix.prefixlen: subnet = next(prefix.subnet(request.prefixlen)) gateway_ip = request.gateway_ip if not gateway_ip: gateway_ip = subnet.network + 1 pools = ipam_utils.generate_pools(subnet.cidr, gateway_ip) return IpamSubnet( request.tenant_id, request.subnet_id, subnet.cidr, gateway_ip=gateway_ip, allocation_pools=pools ) msg = _("Insufficient prefix space to allocate subnet size /%s") raise n_exc.SubnetAllocationError(reason=msg % str(request.prefixlen))
def test_generate_pools_v4_31(self): cidr = '192.168.0.0/31' expected = [netaddr.IPRange('192.168.0.0', '192.168.0.1')] self.assertEqual(expected, utils.generate_pools(cidr, None))
def generate_pools(self, cidr, gateway_ip): return ipam_utils.generate_pools(cidr, gateway_ip)
def test_generate_pools_v6_empty(self): # We want to be sure the range will begin and end with an IPv6 # address, even if an ambiguous ::/64 cidr is given. cidr = '::/64' expected = [netaddr.IPRange('::1', '::FFFF:FFFF:FFFF:FFFF')] self.assertEqual(expected, utils.generate_pools(cidr, None))
def test_generate_pools_v6_nogateway(self): # other than the difference in the last address, the rest of the # logic is the same as v4 so we only need one test cidr = 'F111::0/64' expected = [netaddr.IPRange('F111::1', 'F111::FFFF:FFFF:FFFF:FFFF')] self.assertEqual(expected, utils.generate_pools(cidr, None))
def test_generate_pools_v4_31(self): cidr = '192.168.0.0/31' expected = [] self.assertEqual(expected, utils.generate_pools(cidr, None))
def test_generate_pools_v4_nogateway(self): cidr = '192.168.0.0/24' expected = [netaddr.IPRange('192.168.0.1', '192.168.0.254')] self.assertEqual(expected, utils.generate_pools(cidr, None))
def test_generate_pools_v4_32(self): # 32 is special because it should have 1 usable address cidr = '192.168.0.0/32' expected = [netaddr.IPRange('192.168.0.0', '192.168.0.0')] self.assertEqual(expected, utils.generate_pools(cidr, None))
def test_generate_pools_v4_gateway_last(self): cidr = '192.168.0.0/24' gateway = '192.168.0.254' expected = [netaddr.IPRange('192.168.0.1', '192.168.0.253')] self.assertEqual(expected, utils.generate_pools(cidr, gateway))
def test_generate_pools_v4_gateway_middle(self): cidr = '192.168.0.0/24' gateway = '192.168.0.128' expected = [netaddr.IPRange('192.168.0.1', '192.168.0.127'), netaddr.IPRange('192.168.0.129', '192.168.0.254')] self.assertEqual(expected, utils.generate_pools(cidr, gateway))