fix assign vm issue

This commit is contained in:
Pearl Dsilva 2025-05-09 17:05:28 +05:30
parent 23482e4a28
commit cac5905ecf
1 changed files with 24 additions and 16 deletions

View File

@ -3984,7 +3984,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
return defaultNetwork;
}
private NetworkVO createDefaultNetworkForAccount(DataCenter zone, Account owner, List<NetworkOfferingVO> requiredOfferings)
protected NetworkVO createDefaultNetworkForAccount(DataCenter zone, Account owner, List<NetworkOfferingVO> requiredOfferings)
throws InsufficientCapacityException, ResourceAllocationException {
NetworkVO defaultNetwork = null;
long physicalNetworkId = _networkModel.findPhysicalNetworkId(zone.getId(), requiredOfferings.get(0).getTags(), requiredOfferings.get(0).getTrafficType());
@ -7591,15 +7591,14 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
updateVmOwner(newAccount, vm, domainId, newAccountId);
updateVolumesOwner(volumes, oldAccount, newAccount, newAccountId);
MutableBoolean isNetworkCreated = new MutableBoolean(false);
try {
updateVmNetwork(cmd, caller, vm, newAccount, template);
} catch (InsufficientCapacityException | ResourceAllocationException e) {
updateVmNetwork(cmd, caller, vm, newAccount, template, isNetworkCreated);
} catch (Exception e) {
List<NetworkVO> networkVOS = _networkDao.listByAccountIdNetworkName(newAccountId, newAccount.getAccountName() + "-network");
if (networkVOS.size() == 1) {
if (networkVOS.size() == 1 && isNetworkCreated.get()) {
_networkDao.remove(networkVOS.get(0).getId());
}
_accountMgr.getActiveAccountByName(newAccount.getAccountName() + "-network", newAccount.getDomainId());
throw new CloudRuntimeException(String.format("Unable to update networks when assigning VM [%s] due to [%s].", vm, e.getMessage()), e);
}
@ -7663,8 +7662,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
* @throws InsufficientCapacityException
* @throws ResourceAllocationException
*/
protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template)
throws InsufficientCapacityException, ResourceAllocationException {
protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template, MutableBoolean isNetworkCreated)
throws Exception {
logger.trace("Updating network for VM [{}].", vm);
@ -7681,7 +7680,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
return;
}
updateAdvancedTypeNetworkForVm(cmd, caller, vm, newAccount, template, vmOldProfile, zone, networkIdList, securityGroupIdList);
updateAdvancedTypeNetworkForVm(cmd, caller, vm, newAccount, template, vmOldProfile, zone, networkIdList, securityGroupIdList, isNetworkCreated);
}
/**
@ -7792,8 +7791,8 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
* @throws InvalidParameterValueException
*/
protected void updateAdvancedTypeNetworkForVm(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template,
VirtualMachineProfileImpl vmOldProfile, DataCenterVO zone, List<Long> networkIdList, List<Long> securityGroupIdList)
throws InsufficientCapacityException, ResourceAllocationException, InvalidParameterValueException {
VirtualMachineProfileImpl vmOldProfile, DataCenterVO zone, List<Long> networkIdList, List<Long> securityGroupIdList, MutableBoolean isNetworkCreated)
throws Exception {
LinkedHashSet<NetworkVO> applicableNetworks = new LinkedHashSet<>();
Map<Long, String> requestedIPv4ForNics = new HashMap<>();
@ -7825,7 +7824,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
addNetworksToNetworkIdList(vm, newAccount, vmOldProfile, networkIdList, applicableNetworks, requestedIPv4ForNics, requestedIPv6ForNics);
if (applicableNetworks.isEmpty()) {
selectApplicableNetworkToCreateVm(caller, newAccount, zone, applicableNetworks);
selectApplicableNetworkToCreateVm(caller, newAccount, zone, applicableNetworks, isNetworkCreated);
}
addNicsToApplicableNetworksAndReturnDefaultNetwork(applicableNetworks, requestedIPv4ForNics, requestedIPv6ForNics, networks);
@ -7944,11 +7943,11 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
* @param newAccount The new account associated to the selected network.
* @param zone The zone where the network is selected.
* @param applicableNetworks The applicable networks to which the selected network has to be added to.
* @throws InsufficientCapacityException
* @throws ResourceAllocationException
* @throws Exception
*/
protected void selectApplicableNetworkToCreateVm(Account caller, Account newAccount, DataCenterVO zone, Set<NetworkVO> applicableNetworks)
throws InsufficientCapacityException, ResourceAllocationException {
protected void selectApplicableNetworkToCreateVm(Account caller, Account newAccount, DataCenterVO zone,
Set<NetworkVO> applicableNetworks, MutableBoolean isNetworkCreated)
throws Exception {
logger.trace("Selecting the applicable network to create the VM.");
@ -7969,6 +7968,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
if (virtualNetworks.isEmpty()) {
try (TransactionLegacy txn = TransactionLegacy.open("CreateNetworkTxn")) {
defaultNetwork = createApplicableNetworkToCreateVm(caller, newAccount, zone, firstRequiredOffering);
isNetworkCreated.set(true);
txn.commit();
}
} else if (virtualNetworks.size() > 1) {
@ -9166,4 +9166,12 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
vm.setVncPassword(customParameters.get(VmDetailConstants.KVM_VNC_PASSWORD));
}
}
public static class MutableBoolean {
private boolean value;
public MutableBoolean(boolean val) { this.value = val; }
public void set(boolean val) { this.value = val; }
public boolean get() { return this.value; }
}
}