From: Dimitris Aragiorgis Date: Mon, 29 Oct 2012 19:00:06 +0000 (+0200) Subject: Simplify GenerateFree in network module X-Git-Tag: v2.6.1+ippool11+hotplug4+extstorage3~5^2~2 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/326d8273799b5546a7ae36570f8835a4679ecb67 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 --- diff --git a/lib/config.py b/lib/config.py index a18789d..0539f52 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 96ed654..27a25a7 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"""