From 1f1d3bf2d68db6d921ca1599b681affdb75d40f3 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> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/config.py | 5 ++--- lib/network.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/config.py b/lib/config.py index d2b9c5633..c84c1f112 100644 --- a/lib/config.py +++ b/lib/config.py @@ -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) diff --git a/lib/network.py b/lib/network.py index 31037b991..a0197053d 100644 --- a/lib/network.py +++ b/lib/network.py @@ -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""" -- GitLab