Skip to content

Commit 8b157db

Browse files
author
Daan Hoogland
committed
extra check if net already exists for vm
1 parent e6fb268 commit 8b157db

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,10 +4021,23 @@ public NicProfile addVmToNetwork(final VirtualMachine vm, final Network network,
40214021
}
40224022
}
40234023

4024+
/**
4025+
* duplicated in {@see UserVmManagerImpl} for a {@see UserVmVO}
4026+
*/
4027+
private void checkIfNetExistsForVM(VirtualMachine virtualMachine, Network network) {
4028+
List<NicVO> allNics = _nicsDao.listByVmId(virtualMachine.getId());
4029+
for (NicVO nic : allNics) {
4030+
if (nic.getNetworkId() == network.getId()) {
4031+
throw new CloudRuntimeException("A NIC already exists for VM:" + virtualMachine.getInstanceName() + " in network: " + network.getUuid());
4032+
}
4033+
}
4034+
}
4035+
40244036
private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Network network, final NicProfile requested) throws ConcurrentOperationException, ResourceUnavailableException,
40254037
InsufficientCapacityException {
40264038
final CallContext cctx = CallContext.current();
40274039

4040+
checkIfNetExistsForVM(vm, network);
40284041
s_logger.debug("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested);
40294042
final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
40304043
final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,12 +1384,7 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV
13841384
Account vmOwner = _accountMgr.getAccount(vmInstance.getAccountId());
13851385
_networkModel.checkNetworkPermissions(vmOwner, network);
13861386

1387-
List<NicVO> allNics = _nicDao.listByVmId(vmInstance.getId());
1388-
for (NicVO nic : allNics) {
1389-
if (nic.getNetworkId() == network.getId()) {
1390-
throw new CloudRuntimeException("A NIC already exists for VM:" + vmInstance.getInstanceName() + " in network: " + network.getUuid());
1391-
}
1392-
}
1387+
checkIfNetExistsForVM(vmInstance, network);
13931388

13941389
macAddress = validateOrReplaceMacAddress(macAddress, network.getId());
13951390

@@ -1460,6 +1455,18 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV
14601455
return _vmDao.findById(vmInstance.getId());
14611456
}
14621457

1458+
/**
1459+
* duplicated in {@see VirtualMachineManagerImpl} for a {@see VMInstanceVO}
1460+
*/
1461+
private void checkIfNetExistsForVM(VirtualMachine virtualMachine, Network network) {
1462+
List<NicVO> allNics = _nicDao.listByVmId(virtualMachine.getId());
1463+
for (NicVO nic : allNics) {
1464+
if (nic.getNetworkId() == network.getId()) {
1465+
throw new CloudRuntimeException("A NIC already exists for VM:" + virtualMachine.getInstanceName() + " in network: " + network.getUuid());
1466+
}
1467+
}
1468+
}
1469+
14631470
/**
14641471
* If the given MAC address is invalid it replaces the given MAC with the next available MAC address
14651472
*/

0 commit comments

Comments
 (0)