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 bd378ce5f2
commit d816a6adb8

View File

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