diff --git a/lib/config.py b/lib/config.py index a18789d37c274d8e8e560b83eb5a1e86b9bccbbb..0539f5277f622010be352eefc69845a768243c34 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 96ed654ad7ea942b95b510ac94f0a480d2bcaf82..27a25a78ca6f6cc14b6b6a47ec50827c42b3e94b 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"""