def save_allocation_pools(self, context, subnet, allocation_pools):
     for pool in allocation_pools:
         first_ip = str(netaddr.IPAddress(pool.first, pool.version))
         last_ip = str(netaddr.IPAddress(pool.last, pool.version))
         obj_subnet.IPAllocationPool(
             context, subnet_id=subnet['id'], start=first_ip,
             end=last_ip).create()
 def test_update_db_subnet_new_pools(self, pool_mock):
     old_pools = [{'start': '192.1.1.2', 'end': '192.1.1.254'}]
     context = self.admin_context
     subnet = {
         'id': uuidutils.generate_uuid(),
         'ip_version': constants.IP_VERSION_4,
         'cidr': netaddr.IPNetwork('192.1.1.0/24'),
         'ipv6_address_mode': None,
         'ipv6_ra_mode': None
     }
     # make a copy of subnet for validation, since update_subnet changes
     # incoming subnet dict
     expected_subnet = subnet.copy()
     subnet_obj = obj_subnet.Subnet(context, **subnet)
     subnet_obj.create()
     subnet['allocation_pools'] = [
         netaddr.IPRange('192.1.1.10', '192.1.1.254')
     ]
     expected_subnet = subnet.copy()
     obj_subnet.IPAllocationPool(context,
                                 subnet_id=subnet['id'],
                                 start='192.1.1.10',
                                 end='192.1.1.254').create()
     # validate that subnet passed to request factory is the same as
     # incoming one, i.e. new pools in it are not overwritten by old pools
     self._test_update_db_subnet(pool_mock, subnet, expected_subnet,
                                 old_pools)
Exemplo n.º 3
0
    def _update_subnet_allocation_pools(self, context, subnet_id, s):
        subnet_obj.IPAllocationPool.delete_objects(context,
                                                   subnet_id=subnet_id)
        pools = [(netaddr.IPAddress(p.first, p.version).format(),
                  netaddr.IPAddress(p.last, p.version).format())
                 for p in s['allocation_pools']]
        for p in pools:
            subnet_obj.IPAllocationPool(context, start=p[0], end=p[1],
                                        subnet_id=subnet_id).create()

        # Gather new pools for result
        result_pools = [{'start': p[0], 'end': p[1]} for p in pools]
        del s['allocation_pools']
        return result_pools