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
This commit is contained in:
Wei Zhou 2021-01-27 14:58:52 +01:00 committed by GitHub
parent 4a779deab2
commit 182cea79b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 11 deletions

View File

@ -274,6 +274,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder(); SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ); nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
nicSearch.and("removedNic", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
DistinctHostNameSearch = createSearchBuilder(String.class); DistinctHostNameSearch = createSearchBuilder(String.class);
DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName()); DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName());

View File

@ -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()); throw new CloudRuntimeException(vmInstance + " is in zone:" + vmInstance.getDataCenterId() + " but " + network + " is in zone:" + network.getDataCenterId());
} }
// Get all vms hostNames in the network 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<String> hostNames = _vmInstanceDao.listDistinctHostNames(network.getId()); List<String> hostNames = _vmInstanceDao.listDistinctHostNames(network.getId());
// verify that there are no duplicates, listDistictHostNames could return hostNames even if the NIC //* verify that there are no duplicates
//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())) { if (hostNames.contains(vmInstance.getHostName())) {
for (String hostName : hostNames) { throw new CloudRuntimeException("Network " + network.getName() + " already has a vm with host name: " + vmInstance.getHostName());
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());
}
} }
} }