From 326d8273799b5546a7ae36570f8835a4679ecb67 Mon Sep 17 00:00:00 2001 From: Dimitris Aragiorgis <dimara@grnet.gr> Date: Mon, 29 Oct 2012 21:00:06 +0200 Subject: [PATCH] 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: Dimitris Aragiorgis <dimara@grnet.gr> --- lib/config.py | 5 ++--- lib/network.py | 12 +++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/config.py b/lib/config.py index a18789d37..0539f5277 100644 --- a/lib/config.py +++ b/lib/config.py @@ -369,12 +369,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) diff --git a/lib/network.py b/lib/network.py index 96ed654ad..27a25a78c 100644 --- a/lib/network.py +++ b/lib/network.py @@ -169,12 +169,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]) + """Returns the first free address of the network if any or + raises an error if it is full. - return _iter_free().next + """ + 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""" -- GitLab