diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index f466b190c21..d97f73b5ccf 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -438,7 +438,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public boolean applyIpAssociations(Network network, boolean continueOnError) throws ResourceUnavailableException { - List userIps = _ipAddressDao.listByAssociatedNetwork(network.getId()); + List userIps = _ipAddressDao.listByAssociatedNetwork(network.getId(), null); List publicIps = new ArrayList(); if (userIps != null && !userIps.isEmpty()) { for (IPAddressVO userIp : userIps) { @@ -1101,7 +1101,20 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag network.setGateway(result.getGateway()); network.setMode(result.getMode()); _networksDao.update(networkId, network); - + + //If network if guest virtual and there is no source nat ip, associate a new one + if (network.getGuestType() == GuestIpType.Virtual) { + List ips = _ipAddressDao.listByAssociatedNetwork(networkId, true); + + if (ips.isEmpty()) { + s_logger.debug("Creating a source natp ip for " + network); + Account owner = _accountMgr.getAccount(network.getAccountId()); + PublicIp sourceNatIp = assignSourceNatIpAddress(owner, network, context.getCaller().getId()); + if (sourceNatIp == null) { + throw new InsufficientAddressCapacityException("Unable to assign source nat ip address to the network " + network, DataCenter.class, network.getDataCenterId()); + } + } + } for (NetworkElement element : _networkElements) { if (s_logger.isDebugEnabled()) { @@ -1659,14 +1672,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else { network = networks.get(0); } - - if (network.getGuestType() == GuestIpType.Virtual) { - s_logger.debug("Creating a source natp ip for " + network); - PublicIp ip = assignSourceNatIpAddress(owner, network, userId); - if (ip == null) { - throw new InsufficientAddressCapacityException("Unable to assign source nat ip address to owner for this network", DataCenter.class, zoneId); - } - } } txn.commit(); @@ -2023,7 +2028,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } //release all ip addresses - List ipsToRelease = _ipAddressDao.listByAssociatedNetwork(networkId); + List ipsToRelease = _ipAddressDao.listByAssociatedNetwork(networkId, null); for (IPAddressVO ipToRelease : ipsToRelease) { IPAddressVO ip = _ipAddressDao.markAsUnavailable(ipToRelease.getId()); assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable."; diff --git a/server/src/com/cloud/network/dao/IPAddressDao.java b/server/src/com/cloud/network/dao/IPAddressDao.java index f1d78faba9b..73e67499e4c 100644 --- a/server/src/com/cloud/network/dao/IPAddressDao.java +++ b/server/src/com/cloud/network/dao/IPAddressDao.java @@ -36,7 +36,7 @@ public interface IPAddressDao extends GenericDao { List listByDcIdIpAddress(long dcId, String ipAddress); - List listByAssociatedNetwork(long networkId); + List listByAssociatedNetwork(long networkId, Boolean isSourceNat); int countIPs(long dcId, long vlanDbId, boolean onlyCountAllocated); @@ -49,4 +49,5 @@ public interface IPAddressDao extends GenericDao { IPAddressVO findByAssociatedVmId(long vmId); IPAddressVO findByAccountAndIp(long accountId, String ipAddress); + } diff --git a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java index cd784a1b151..d848bfdb3c4 100644 --- a/server/src/com/cloud/network/dao/IPAddressDaoImpl.java +++ b/server/src/com/cloud/network/dao/IPAddressDaoImpl.java @@ -192,10 +192,14 @@ public class IPAddressDaoImpl extends GenericDaoBase implemen } @Override - public List listByAssociatedNetwork(long networkId) { + public List listByAssociatedNetwork(long networkId, Boolean isSourceNat) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("network", networkId); + if (isSourceNat != null) { + sc.setParameters("sourceNat", isSourceNat); + } + return listBy(sc); }