fix assign vm issue
diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
index 40ba5ba..61d127e 100644
--- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
@@ -3984,7 +3984,7 @@
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 @@
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 @@
* @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 @@
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 @@
* @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 @@
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 @@
* @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 @@
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 @@
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; }
+ }
+
}