Rename range to avoid shadowing the builtin

This refactor supports a patch I was reviewing [1].  I thought it
better to apply this consistently in this file as a separate refactor
so that that patch could be done cleaner.

[1] https://review.openstack.org/#/c/100963

Change-Id: Ida58758504e868dc731e97d7bce53d4849998723
This commit is contained in:
Carl Baldwin 2014-08-19 03:58:22 +00:00 committed by rossella
parent 3d3fbc3837
commit 702967e2a8
1 changed files with 23 additions and 20 deletions

View File

@ -202,26 +202,26 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
models_v2.IPAvailabilityRange).join(
models_v2.IPAllocationPool).with_lockmode('update')
for subnet in subnets:
range = range_qry.filter_by(subnet_id=subnet['id']).first()
if not range:
ip_range = range_qry.filter_by(subnet_id=subnet['id']).first()
if not ip_range:
LOG.debug(_("All IPs from subnet %(subnet_id)s (%(cidr)s) "
"allocated"),
{'subnet_id': subnet['id'], 'cidr': subnet['cidr']})
continue
ip_address = range['first_ip']
ip_address = ip_range['first_ip']
LOG.debug(_("Allocated IP - %(ip_address)s from %(first_ip)s "
"to %(last_ip)s"),
{'ip_address': ip_address,
'first_ip': range['first_ip'],
'last_ip': range['last_ip']})
if range['first_ip'] == range['last_ip']:
'first_ip': ip_range['first_ip'],
'last_ip': ip_range['last_ip']})
if ip_range['first_ip'] == ip_range['last_ip']:
# No more free indices on subnet => delete
LOG.debug(_("No more free IP's in slice. Deleting allocation "
"pool."))
context.session.delete(range)
context.session.delete(ip_range)
else:
# increment the first free
range['first_ip'] = str(netaddr.IPAddress(ip_address) + 1)
ip_range['first_ip'] = str(netaddr.IPAddress(ip_address) + 1)
return {'ip_address': ip_address, 'subnet_id': subnet['id']}
raise n_exc.IpAddressGenerationFailure(net_id=subnets[0]['network_id'])
@ -263,11 +263,11 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
yield netaddr.IPRange(first, last)
# Write the ranges to the db
for range in ipset_to_ranges(available):
for ip_range in ipset_to_ranges(available):
available_range = models_v2.IPAvailabilityRange(
allocation_pool_id=pool['id'],
first_ip=str(netaddr.IPAddress(range.first)),
last_ip=str(netaddr.IPAddress(range.last)))
first_ip=str(netaddr.IPAddress(ip_range.first)),
last_ip=str(netaddr.IPAddress(ip_range.last)))
context.session.add(available_range)
@staticmethod
@ -278,26 +278,29 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
models_v2.IPAvailabilityRange).join(
models_v2.IPAllocationPool).with_lockmode('update')
results = range_qry.filter_by(subnet_id=subnet_id)
for range in results:
first = int(netaddr.IPAddress(range['first_ip']))
last = int(netaddr.IPAddress(range['last_ip']))
for ip_range in results:
first = int(netaddr.IPAddress(ip_range['first_ip']))
last = int(netaddr.IPAddress(ip_range['last_ip']))
if first <= ip <= last:
if first == last:
context.session.delete(range)
context.session.delete(ip_range)
return
elif first == ip:
range['first_ip'] = str(netaddr.IPAddress(ip_address) + 1)
new_first_ip = str(netaddr.IPAddress(ip_address) + 1)
ip_range['first_ip'] = new_first_ip
return
elif last == ip:
range['last_ip'] = str(netaddr.IPAddress(ip_address) - 1)
new_last_ip = str(netaddr.IPAddress(ip_address) - 1)
ip_range['last_ip'] = new_last_ip
return
else:
# Split into two ranges
new_first = str(netaddr.IPAddress(ip_address) + 1)
new_last = range['last_ip']
range['last_ip'] = str(netaddr.IPAddress(ip_address) - 1)
new_last = ip_range['last_ip']
new_last_ip = str(netaddr.IPAddress(ip_address) - 1)
ip_range['last_ip'] = new_last_ip
ip_range = models_v2.IPAvailabilityRange(
allocation_pool_id=range['allocation_pool_id'],
allocation_pool_id=ip_range['allocation_pool_id'],
first_ip=new_first,
last_ip=new_last)
context.session.add(ip_range)