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: ...@@ -376,12 +376,11 @@ class ConfigWriter:
net_uuid = self._UnlockedLookupNetwork(net) net_uuid = self._UnlockedLookupNetwork(net)
nobj = self._UnlockedGetNetwork(net_uuid) nobj = self._UnlockedGetNetwork(net_uuid)
pool = network.AddressPool(nobj) pool = network.AddressPool(nobj)
gen_free = pool.GenerateFree()
def gen_one(): def gen_one():
try: try:
ip = gen_free() ip = pool.GenerateFree()
except StopIteration: except errors.AddressPoolError:
raise errors.ReservationError("Cannot generate IP. Network is full") raise errors.ReservationError("Cannot generate IP. Network is full")
return (constants.RESERVE_ACTION, ip, net_uuid) return (constants.RESERVE_ACTION, ip, net_uuid)
......
...@@ -173,12 +173,14 @@ class AddressPool(object): ...@@ -173,12 +173,14 @@ class AddressPool(object):
return address return address
def GenerateFree(self): def GenerateFree(self):
"""A generator for free addresses.""" """Returns the first free address of the network if any or
def _iter_free(): raises an error if it is full.
for idx in self.all_reservations.search("0", 64):
yield str(self.network[idx]) """
# pylint: disable=E1101 if self.IsFull():
return _iter_free().next raise errors.AddressPoolError("%s is full" % self.network)
idx = self.all_reservations.search("0", 1)
return str(self.network[idx])
def GetExternalReservations(self): def GetExternalReservations(self):
"""Returns a list of all externally reserved addresses""" """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