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 = AddressPool(nobj) if action == RESERVE_ACTION: pool.Reserve(address) elif action == RELEASE_ACTION: pool.Release(address)
def _UnlockedReserveIp(self, net_uuid, address, ec_id, check=True): """Reserve a given IPv4 address for use by an instance. """ nobj = self._UnlockedGetNetwork(net_uuid) pool = AddressPool(nobj) try: isreserved = pool.IsReserved(address) isextreserved = pool.IsReserved(address, external=True) except errors.AddressPoolError: raise errors.ReservationError("IP address not in network") if isreserved: raise errors.ReservationError("IP address already in use") if check and isextreserved: raise errors.ReservationError("IP is externally reserved") return self._temporary_ips.Reserve(ec_id, (RESERVE_ACTION, address, net_uuid))
def CreateNic(self, uuid=None, name=None, mac=None, ip=None, network=None, nicparams=None, netinfo=None): """Create a new L{objecs.NIC} object @rtype: L{objects.NIC} @return: the newly create NIC object """ nic_id = self._cur_nic_id self._cur_nic_id += 1 if uuid is None: uuid = self._GetUuid() if name is None: name = "mock_nic_%d" % nic_id if mac is None: mac = "aa:00:00:aa:%02x:%02x" % (nic_id / 0xff, nic_id % 0xff) if isinstance(network, objects.Network): if ip: pool = AddressPool(network) pool.Reserve(ip) network = network.uuid if nicparams is None: nicparams = {} return objects.NIC(uuid=uuid, name=name, mac=mac, ip=ip, network=network, nicparams=nicparams, netinfo=netinfo)
def GenerateIp(self, net_uuid, ec_id): """Find a free IPv4 address for an instance. """ nobj = self._UnlockedGetNetwork(net_uuid) pool = AddressPool(nobj) def gen_one(): try: ip = pool.GenerateFree() except errors.AddressPoolError: raise errors.ReservationError( "Cannot generate IP. Network is full") return (RESERVE_ACTION, ip, net_uuid) _, address, _ = self._temporary_ips.Generate([], gen_one, ec_id) return address