simplify cidr block logic a little bit

This commit is contained in:
forest 2021-07-12 11:29:33 -05:00
parent fcbea1e29b
commit ad9c3476c7
2 changed files with 13 additions and 11 deletions

View File

@ -35,16 +35,15 @@ def index():
display_host = dict(name=host_id, networks=value['networks'])
for network in display_host['networks']:
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
network_start_int = -1
network_end_int = -1
i = 0
for ipv4_address in ipv4_network:
i += 1
if i > 2:
if network_start_int == -1:
network_start_int = int(ipv4_address)
public_ipv4_cidr_block_split = network["public_ipv4_cidr_block"].split("/")
if len(public_ipv4_cidr_block_split) != 2:
raise ValueError(f"network {network['network_name']} has invalid cidr block {network['public_ipv4_cidr_block']}")
network_start_int = int(ipaddress.ip_address(public_ipv4_cidr_block_split[0]))+1
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
network_end_int = -1
for ipv4_address in ipv4_network:
network_end_int = int(ipv4_address)
network['allocations'] = []

View File

@ -142,11 +142,14 @@ def can_claim_create(payload, host_id) -> (str, str):
for vm in vms:
claimed_ipv4s[vm['public_ipv4']] = True
public_ipv4_cidr_block_split = network["public_ipv4_cidr_block"].split("/")
if len(public_ipv4_cidr_block_split) != 2:
raise ValueError(f"network {network['network_name']} has invalid cidr block {network['public_ipv4_cidr_block']}")
ipv4_network_first_address_int = int(ipaddress.ip_address(public_ipv4_cidr_block_split[0]))
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
i = 0
for ipv4_address in ipv4_network:
i += 1
if i > 2 and str(ipv4_address) not in claimed_ipv4s:
if int(ipv4_address) > ipv4_network_first_address_int and str(ipv4_address) not in claimed_ipv4s:
allocated_ipv4_address = str(ipv4_address)
break