Commit 1f1d3bf2 authored by Dimitris Aragiorgis's avatar Dimitris Aragiorgis Committed by Iustin Pop
Browse files

Simplify GenerateFree in network module



GenerateFree now returns the first available IP in the network
or raises AddressPoolError if it is full.
Signed-off-by: default avatarDimitris Aragiorgis <dimara@grnet.gr>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent e81eef56
......@@ -376,12 +376,11 @@ class ConfigWriter:
net_uuid = self._UnlockedLookupNetwork(net)
nobj = self._UnlockedGetNetwork(net_uuid)
pool = network.AddressPool(nobj)
gen_free = pool.GenerateFree()
def gen_one():
try:
ip = gen_free()
except StopIteration:
ip = pool.GenerateFree()
except errors.AddressPoolError:
raise errors.ReservationError("Cannot generate IP. Network is full")
return (constants.RESERVE_ACTION, ip, net_uuid)
......
......@@ -173,12 +173,14 @@ class AddressPool(object):
return address
def GenerateFree(self):
"""A generator for free addresses."""
def _iter_free():
for idx in self.all_reservations.search("0", 64):
yield str(self.network[idx])
# pylint: disable=E1101
return _iter_free().next
"""Returns the first free address of the network if any or
raises an error if it is full.
"""
if self.IsFull():
raise errors.AddressPoolError("%s is full" % self.network)
idx = self.all_reservations.search("0", 1)
return str(self.network[idx])
def GetExternalReservations(self):
"""Returns a list of all externally reserved addresses"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment