From 182cea79b51d41e7fe3a4b651764de2df8c27230 Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Wed, 27 Jan 2021 14:58:52 +0100 Subject: [PATCH] server: fix cannot create vm if another vm with same name has been added and removed on the network (#4600) * server: fix cannot create vm if another vm with same name has been added and removed on the network steps to reproduce the issue (1) create vm-1 on network-1 (2) add vm-1 to network-2 (3) remove vm-1 from network-2 (4) create another vm with same name vm-1 on network-2 expected result: operation succeed actual result: operation failed. * #4600: add back a removed line --- .../com/cloud/vm/dao/VMInstanceDaoImpl.java | 1 + .../java/com/cloud/vm/UserVmManagerImpl.java | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java index 1d7d4440566..97a505d7c8f 100755 --- a/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java @@ -274,6 +274,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase implem SearchBuilder nicSearch = _nicDao.createSearchBuilder(); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); + nicSearch.and("removedNic", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL); DistinctHostNameSearch = createSearchBuilder(String.class); DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName()); diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java index 20c7930d379..a084d63eb39 100644 --- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java @@ -1252,17 +1252,14 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir throw new CloudRuntimeException(vmInstance + " is in zone:" + vmInstance.getDataCenterId() + " but " + network + " is in zone:" + network.getDataCenterId()); } - // Get all vms hostNames in the network - List hostNames = _vmInstanceDao.listDistinctHostNames(network.getId()); - // verify that there are no duplicates, listDistictHostNames could return hostNames even if the NIC - //in the network is removed, so also check if the NIC is present and then throw an exception. - //This will also check if there are multiple nics of same vm in the network - if (hostNames.contains(vmInstance.getHostName())) { - for (String hostName : hostNames) { - VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName); - if (_networkModel.getNicInNetwork(vm.getId(), network.getId()) != null && vm.getHostName().equals(vmInstance.getHostName())) { - throw new CloudRuntimeException(network + " already has a vm with host name: " + vmInstance.getHostName()); - } + if(_networkModel.getNicInNetwork(vmInstance.getId(),network.getId()) != null){ + s_logger.debug("VM " + vmInstance.getHostName() + " already in network " + network.getName() + " going to add another NIC"); + } else { + //* get all vms hostNames in the network + List hostNames = _vmInstanceDao.listDistinctHostNames(network.getId()); + //* verify that there are no duplicates + if (hostNames.contains(vmInstance.getHostName())) { + throw new CloudRuntimeException("Network " + network.getName() + " already has a vm with host name: " + vmInstance.getHostName()); } }