simplify cidr block logic a little bit
This commit is contained in:
parent
fcbea1e29b
commit
ad9c3476c7
@ -35,16 +35,15 @@ def index():
|
|||||||
display_host = dict(name=host_id, networks=value['networks'])
|
display_host = dict(name=host_id, networks=value['networks'])
|
||||||
|
|
||||||
for network in display_host['networks']:
|
for network in display_host['networks']:
|
||||||
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
|
public_ipv4_cidr_block_split = network["public_ipv4_cidr_block"].split("/")
|
||||||
network_start_int = -1
|
if len(public_ipv4_cidr_block_split) != 2:
|
||||||
network_end_int = -1
|
raise ValueError(f"network {network['network_name']} has invalid cidr block {network['public_ipv4_cidr_block']}")
|
||||||
i = 0
|
|
||||||
for ipv4_address in ipv4_network:
|
|
||||||
i += 1
|
|
||||||
if i > 2:
|
|
||||||
if network_start_int == -1:
|
|
||||||
network_start_int = int(ipv4_address)
|
|
||||||
|
|
||||||
|
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_end_int = int(ipv4_address)
|
||||||
|
|
||||||
network['allocations'] = []
|
network['allocations'] = []
|
||||||
|
@ -142,11 +142,14 @@ def can_claim_create(payload, host_id) -> (str, str):
|
|||||||
for vm in vms:
|
for vm in vms:
|
||||||
claimed_ipv4s[vm['public_ipv4']] = True
|
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)
|
ipv4_network = ipaddress.ip_network(network["public_ipv4_cidr_block"], False)
|
||||||
i = 0
|
|
||||||
for ipv4_address in ipv4_network:
|
for ipv4_address in ipv4_network:
|
||||||
i += 1
|
if int(ipv4_address) > ipv4_network_first_address_int and str(ipv4_address) not in claimed_ipv4s:
|
||||||
if i > 2 and str(ipv4_address) not in claimed_ipv4s:
|
|
||||||
allocated_ipv4_address = str(ipv4_address)
|
allocated_ipv4_address = str(ipv4_address)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user