Fix issues with allocation pool generation for ::/64 cidr

Passing a ::/64 cidr to certain netaddr functions without specifying
the ip_version causes errors. Fix this by specifying ip_version.

Change-Id: I31aaf9f5dabe4dd0845507f245387cd4186c410c
Closes-Bug: 1472304
This commit is contained in:
John Davidge 2015-07-07 17:00:01 +01:00
parent 1e3b4f119a
commit a0a022373b
2 changed files with 15 additions and 5 deletions

View File

@ -43,16 +43,19 @@ def generate_pools(cidr, gateway_ip):
"""
# Auto allocate the pool around gateway_ip
net = netaddr.IPNetwork(cidr)
if net.first == net.last:
ip_version = net.version
first = netaddr.IPAddress(net.first, ip_version)
last = netaddr.IPAddress(net.last, ip_version)
if first == last:
# handle single address subnet case
return [netaddr.IPRange(net.first, net.last)]
first_ip = net.first + 1
return [netaddr.IPRange(first, last)]
first_ip = first + 1
# last address is broadcast in v4
last_ip = net.last - (net.version == 4)
last_ip = last - (ip_version == 4)
if first_ip >= last_ip:
# /31 lands here
return []
ipset = netaddr.IPSet(netaddr.IPRange(first_ip, last_ip))
if gateway_ip:
ipset.remove(netaddr.IPAddress(gateway_ip))
ipset.remove(netaddr.IPAddress(gateway_ip, ip_version))
return list(ipset.iter_ipranges())

View File

@ -80,3 +80,10 @@ class TestIpamUtils(base.BaseTestCase):
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_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))