+ def _UnlockedCommitTemporaryIps(self, ec_id):
+ """Commit all reserved IP address to their respective pools
+
+ """
+ for action, address, net_uuid in self._temporary_ips.GetECReserved(ec_id):
+ self._UnlockedCommitIp(action, net_uuid, address)
+
+ def _UnlockedCommitIp(self, action, net_uuid, address):
+ """Commit a reserved IP address to an IP pool.
+
+ The IP address is taken from the network's IP pool and marked as reserved.
+
+ """
+ nobj = self._UnlockedGetNetwork(net_uuid)
+ pool = network.AddressPool(nobj)
+ if action == constants.RESERVE_ACTION:
+ pool.Reserve(address)
+ elif action == constants.RELEASE_ACTION:
+ pool.Release(address)
+
+ def _UnlockedReleaseIp(self, net_uuid, address, ec_id):
+ """Give a specific IP address back to an IP pool.
+
+ The IP address is returned to the IP pool designated by pool_id and marked
+ as reserved.
+
+ """
+ self._temporary_ips.Reserve(ec_id,
+ (constants.RELEASE_ACTION, address, net_uuid))
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def ReleaseIp(self, net_uuid, address, ec_id):
+ """Give a specified IP address back to an IP pool.
+
+ This is just a wrapper around _UnlockedReleaseIp.
+
+ """
+ if net_uuid:
+ self._UnlockedReleaseIp(net_uuid, address, ec_id)
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def GenerateIp(self, net_uuid, ec_id):
+ """Find a free IPv4 address for an instance.
+
+ """
+ nobj = self._UnlockedGetNetwork(net_uuid)
+ pool = network.AddressPool(nobj)
+
+ def gen_one():
+ try:
+ ip = pool.GenerateFree()
+ except errors.AddressPoolError:
+ raise errors.ReservationError("Cannot generate IP. Network is full")
+ return (constants.RESERVE_ACTION, ip, net_uuid)
+
+ _, address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
+ return address
+
+ def _UnlockedReserveIp(self, net_uuid, address, ec_id):
+ """Reserve a given IPv4 address for use by an instance.
+
+ """
+ nobj = self._UnlockedGetNetwork(net_uuid)
+ pool = network.AddressPool(nobj)
+ try:
+ isreserved = pool.IsReserved(address)
+ except errors.AddressPoolError:
+ raise errors.ReservationError("IP address not in network")
+ if isreserved:
+ raise errors.ReservationError("IP address already in use")
+
+ return self._temporary_ips.Reserve(ec_id,
+ (constants.RESERVE_ACTION,
+ address, net_uuid))
+
+ @locking.ssynchronized(_config_lock, shared=1)
+ def ReserveIp(self, net_uuid, address, ec_id):
+ """Reserve a given IPv4 address for use by an instance.
+
+ """
+ if net_uuid:
+ return self._UnlockedReserveIp(net_uuid, address, ec_id)
+