Skip to content

Commit 1cc95ef

Browse files
committed
cloudvision/cvlib: support negative index for get_ip_from_subnet
- allows allocation from the end of the subnet - required by avd-campus studio. Change-Id: Id79b0ba459f3540cb39f4a8603636616bc84f9d4
1 parent 3c3b4fd commit 1cc95ef

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

cloudvision/cvlib/iputils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,18 @@ def get_ip_from_subnet(network, index: int, hostname: str = None,
101101
poolname: str = None):
102102
# Return the ip address at the specified index.
103103
# Input index is 0 based.
104+
# Negative input index allocates from the end of the subnet.
104105
num_addresses = number_of_usable_addresses(network)
106+
if index < 0:
107+
index = num_addresses + index
108+
if index < 0 or index >= num_addresses:
109+
raise IpHostIndexException(
110+
network, num_addresses, index, hostname, poolname)
105111
if (network.version == 4 and network.prefixlen < 31) or (
106112
network.version == 6 and network.prefixlen < 127):
107113
# When indexing directly into non p2p-link networks,
108114
# skip identification/anycast address
109115
index += 1
110-
if index > num_addresses:
111-
raise IpHostIndexException(
112-
network, num_addresses, index, hostname, poolname)
113116
return network[index]
114117

115118

test/cvlib/iputils/test_iputils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def test_host_utils(name, ip_network, exp_num_hosts, exp_first_host,
261261
None,
262262
None,
263263
None,
264-
IpHostIndexException('1.1.1.240/28', 14, 15, 'dev1'),
264+
IpHostIndexException('1.1.1.240/28', 14, 14, 'dev1'),
265265
],
266266
[
267267
'ipv6 indexing one beyond last host in last subnet',
@@ -274,7 +274,7 @@ def test_host_utils(name, ip_network, exp_num_hosts, exp_first_host,
274274
None,
275275
None,
276276
None,
277-
IpHostIndexException('1:1:1::ffff:fff0/124', 15, 16, 'dev1'),
277+
IpHostIndexException('1:1:1::ffff:fff0/124', 15, 15, 'dev1'),
278278
],
279279
]
280280

0 commit comments

Comments
 (0)