CLOUDSTACK-7218: Remvoing all static nat associated with vm in case of secondary ips

In vm secondary ips case static nat configured to vm primary/secondary ips
IP1-->vm1Ip1, IP2-->vm1Ip2
While destroying vm deleting all static nats associated with the vm
This commit is contained in:
Jayapal 2014-08-26 10:13:32 +05:30
parent 30ecf935e8
commit 384025f258
3 changed files with 25 additions and 8 deletions

View File

@ -52,6 +52,10 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
IPAddressVO findByAssociatedVmId(long vmId); IPAddressVO findByAssociatedVmId(long vmId);
// for vm secondary ips case mapping is IP1--> vmIp1, IP2-->vmIp2, etc
// This method is used when one vm is mapped to muliple to public ips
List<IPAddressVO> findAllByAssociatedVmId(long vmId);
IPAddressVO findByIpAndSourceNetworkId(long networkId, String ipAddress); IPAddressVO findByIpAndSourceNetworkId(long networkId, String ipAddress);
public IPAddressVO findByIpAndDcId(long dcId, String ipAddress); public IPAddressVO findByIpAndDcId(long dcId, String ipAddress);
@ -79,4 +83,5 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
boolean deletePublicIPRange(long vlanDbId); boolean deletePublicIPRange(long vlanDbId);
void lockRange(long vlandbId); void lockRange(long vlandbId);
} }

View File

@ -263,6 +263,17 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
return findOneBy(sc); return findOneBy(sc);
} }
// for vm secondary ips case mapping is IP1--> vmIp1, IP2-->vmIp2, etc
// Used when vm is mapped to muliple to public ips
@Override
public List<IPAddressVO> findAllByAssociatedVmId(long vmId) {
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("associatedWithVmId", vmId);
return listBy(sc);
}
@Override @Override
public IPAddressVO findByVmIp(String vmIp) { public IPAddressVO findByVmIp(String vmIp) {
SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create(); SearchCriteria<IPAddressVO> sc = AllFieldsSearch.create();

View File

@ -1776,20 +1776,21 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
// If vm is assigned to static nat, disable static nat for the ip // If vm is assigned to static nat, disable static nat for the ip
// address and disassociate ip if elasticIP is enabled // address and disassociate ip if elasticIP is enabled
IPAddressVO ip = _ipAddressDao.findByAssociatedVmId(vmId); List<IPAddressVO> ips = _ipAddressDao.findAllByAssociatedVmId(vmId);
for (IPAddressVO ip : ips) {
try { try {
if (ip != null) {
if (_rulesMgr.disableStaticNat(ip.getId(), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM, true)) { if (_rulesMgr.disableStaticNat(ip.getId(), _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM, true)) {
s_logger.debug("Disabled 1-1 nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge"); s_logger.debug("Disabled 1-1 nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge");
} else { } else {
s_logger.warn("Failed to disable static nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge"); s_logger.warn("Failed to disable static nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge");
success = false; success = false;
} }
}
} catch (ResourceUnavailableException e) { } catch (ResourceUnavailableException e) {
success = false; success = false;
s_logger.warn("Failed to disable static nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge because resource is unavailable", e); s_logger.warn("Failed to disable static nat for ip address " + ip + " as a part of vm id=" + vmId + " expunge because resource is unavailable", e);
} }
}
return success; return success;
} }
@ -4340,8 +4341,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
throw new InvalidParameterValueException("Remove the load balancing rules for this VM before assigning to another user."); throw new InvalidParameterValueException("Remove the load balancing rules for this VM before assigning to another user.");
} }
// check for one on one nat // check for one on one nat
IPAddressVO ip = _ipAddressDao.findByAssociatedVmId(cmd.getVmId()); List<IPAddressVO> ips = _ipAddressDao.findAllByAssociatedVmId(cmd.getVmId());
if (ip != null) { for (IPAddressVO ip : ips) {
if (ip.isOneToOneNat()) { if (ip.isOneToOneNat()) {
throw new InvalidParameterValueException("Remove the one to one nat rule for this VM for ip " + ip.toString()); throw new InvalidParameterValueException("Remove the one to one nat rule for this VM for ip " + ip.toString());
} }