diff --git a/server/src/com/cloud/event/ActionEventUtils.java b/server/src/com/cloud/event/ActionEventUtils.java index 7b727cd6f9d..4ee6e9e0a2b 100755 --- a/server/src/com/cloud/event/ActionEventUtils.java +++ b/server/src/com/cloud/event/ActionEventUtils.java @@ -200,6 +200,10 @@ public class ActionEventUtils { private static long getDomainId(long accountId){ AccountVO account = _accountDao.findByIdIncludingRemoved(accountId); + if (account == null) { + s_logger.error("Failed to find account(including removed ones) by id '" + accountId + "'"); + return 0; + } return account.getDomainId(); } } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 5fc4f418ca2..b4890970ec1 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -2805,13 +2805,6 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L return false; } - // Don't allow to delete network via api call when it has vms assigned to it - int nicCount = getActiveNicsInNetwork(networkId); - if (nicCount > 0) { - s_logger.debug("Unable to remove the network id=" + networkId + " as it has active Nics."); - return false; - } - // Make sure that there are no user vms in the network that are not Expunged/Error List userVms = _userVmDao.listByNetworkIdAndStates(networkId); @@ -2822,6 +2815,15 @@ public class NetworkManagerImpl extends ManagerBase implements NetworkManager, L } } + // Don't allow to delete network via api call when it has vms assigned to it + int nicCount = getActiveNicsInNetwork(networkId); + if (nicCount > 0) { + s_logger.debug("The network id=" + networkId + " has active Nics, but shouldn't."); + // at this point we have already determined that there are no active user vms in network + // if the op_networks table shows active nics, it's a bug in releasing nics updating op_networks + _networksDao.changeActiveNicsBy(networkId, (-1 * nicCount)); + } + //In Basic zone, make sure that there are no non-removed console proxies and SSVMs using the network DataCenter zone = _configMgr.getZone(network.getDataCenterId()); if (zone.getNetworkType() == NetworkType.Basic) {