Skip to content

Commit 182cea7

Browse files
authored
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
1 parent 4a779de commit 182cea7

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ protected void init() {
274274

275275
SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
276276
nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
277+
nicSearch.and("removedNic", nicSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
277278

278279
DistinctHostNameSearch = createSearchBuilder(String.class);
279280
DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName());

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,17 +1252,14 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV
12521252
throw new CloudRuntimeException(vmInstance + " is in zone:" + vmInstance.getDataCenterId() + " but " + network + " is in zone:" + network.getDataCenterId());
12531253
}
12541254

1255-
// Get all vms hostNames in the network
1256-
List<String> hostNames = _vmInstanceDao.listDistinctHostNames(network.getId());
1257-
// verify that there are no duplicates, listDistictHostNames could return hostNames even if the NIC
1258-
//in the network is removed, so also check if the NIC is present and then throw an exception.
1259-
//This will also check if there are multiple nics of same vm in the network
1260-
if (hostNames.contains(vmInstance.getHostName())) {
1261-
for (String hostName : hostNames) {
1262-
VMInstanceVO vm = _vmInstanceDao.findVMByHostName(hostName);
1263-
if (_networkModel.getNicInNetwork(vm.getId(), network.getId()) != null && vm.getHostName().equals(vmInstance.getHostName())) {
1264-
throw new CloudRuntimeException(network + " already has a vm with host name: " + vmInstance.getHostName());
1265-
}
1255+
if(_networkModel.getNicInNetwork(vmInstance.getId(),network.getId()) != null){
1256+
s_logger.debug("VM " + vmInstance.getHostName() + " already in network " + network.getName() + " going to add another NIC");
1257+
} else {
1258+
//* get all vms hostNames in the network
1259+
List<String> hostNames = _vmInstanceDao.listDistinctHostNames(network.getId());
1260+
//* verify that there are no duplicates
1261+
if (hostNames.contains(vmInstance.getHostName())) {
1262+
throw new CloudRuntimeException("Network " + network.getName() + " already has a vm with host name: " + vmInstance.getHostName());
12661263
}
12671264
}
12681265

0 commit comments

Comments
 (0)