Skip to content

Commit cac5905

Browse files
committed
fix assign vm issue
1 parent 23482e4 commit cac5905

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,7 +3984,7 @@ private NetworkVO getDefaultNetwork(DataCenter zone, Account owner, boolean sele
39843984
return defaultNetwork;
39853985
}
39863986

3987-
private NetworkVO createDefaultNetworkForAccount(DataCenter zone, Account owner, List<NetworkOfferingVO> requiredOfferings)
3987+
protected NetworkVO createDefaultNetworkForAccount(DataCenter zone, Account owner, List<NetworkOfferingVO> requiredOfferings)
39883988
throws InsufficientCapacityException, ResourceAllocationException {
39893989
NetworkVO defaultNetwork = null;
39903990
long physicalNetworkId = _networkModel.findPhysicalNetworkId(zone.getId(), requiredOfferings.get(0).getTags(), requiredOfferings.get(0).getTrafficType());
@@ -7591,15 +7591,14 @@ protected void executeStepsToChangeOwnershipOfVm(AssignVMCmd cmd, Account caller
75917591
updateVmOwner(newAccount, vm, domainId, newAccountId);
75927592

75937593
updateVolumesOwner(volumes, oldAccount, newAccount, newAccountId);
7594-
7594+
MutableBoolean isNetworkCreated = new MutableBoolean(false);
75957595
try {
7596-
updateVmNetwork(cmd, caller, vm, newAccount, template);
7597-
} catch (InsufficientCapacityException | ResourceAllocationException e) {
7596+
updateVmNetwork(cmd, caller, vm, newAccount, template, isNetworkCreated);
7597+
} catch (Exception e) {
75987598
List<NetworkVO> networkVOS = _networkDao.listByAccountIdNetworkName(newAccountId, newAccount.getAccountName() + "-network");
7599-
if (networkVOS.size() == 1) {
7599+
if (networkVOS.size() == 1 && isNetworkCreated.get()) {
76007600
_networkDao.remove(networkVOS.get(0).getId());
76017601
}
7602-
_accountMgr.getActiveAccountByName(newAccount.getAccountName() + "-network", newAccount.getDomainId());
76037602
throw new CloudRuntimeException(String.format("Unable to update networks when assigning VM [%s] due to [%s].", vm, e.getMessage()), e);
76047603
}
76057604

@@ -7663,8 +7662,8 @@ protected void updateVolumesOwner(final List<VolumeVO> volumes, Account oldAccou
76637662
* @throws InsufficientCapacityException
76647663
* @throws ResourceAllocationException
76657664
*/
7666-
protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template)
7667-
throws InsufficientCapacityException, ResourceAllocationException {
7665+
protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template, MutableBoolean isNetworkCreated)
7666+
throws Exception {
76687667

76697668
logger.trace("Updating network for VM [{}].", vm);
76707669

@@ -7681,7 +7680,7 @@ protected void updateVmNetwork(AssignVMCmd cmd, Account caller, UserVmVO vm, Acc
76817680
return;
76827681
}
76837682

7684-
updateAdvancedTypeNetworkForVm(cmd, caller, vm, newAccount, template, vmOldProfile, zone, networkIdList, securityGroupIdList);
7683+
updateAdvancedTypeNetworkForVm(cmd, caller, vm, newAccount, template, vmOldProfile, zone, networkIdList, securityGroupIdList, isNetworkCreated);
76857684
}
76867685

76877686
/**
@@ -7792,8 +7791,8 @@ protected void updateBasicTypeNetworkForVm(AssignVMCmd cmd, UserVmVO vm, Account
77927791
* @throws InvalidParameterValueException
77937792
*/
77947793
protected void updateAdvancedTypeNetworkForVm(AssignVMCmd cmd, Account caller, UserVmVO vm, Account newAccount, VirtualMachineTemplate template,
7795-
VirtualMachineProfileImpl vmOldProfile, DataCenterVO zone, List<Long> networkIdList, List<Long> securityGroupIdList)
7796-
throws InsufficientCapacityException, ResourceAllocationException, InvalidParameterValueException {
7794+
VirtualMachineProfileImpl vmOldProfile, DataCenterVO zone, List<Long> networkIdList, List<Long> securityGroupIdList, MutableBoolean isNetworkCreated)
7795+
throws Exception {
77977796

77987797
LinkedHashSet<NetworkVO> applicableNetworks = new LinkedHashSet<>();
77997798
Map<Long, String> requestedIPv4ForNics = new HashMap<>();
@@ -7825,7 +7824,7 @@ protected void updateAdvancedTypeNetworkForVm(AssignVMCmd cmd, Account caller, U
78257824
addNetworksToNetworkIdList(vm, newAccount, vmOldProfile, networkIdList, applicableNetworks, requestedIPv4ForNics, requestedIPv6ForNics);
78267825

78277826
if (applicableNetworks.isEmpty()) {
7828-
selectApplicableNetworkToCreateVm(caller, newAccount, zone, applicableNetworks);
7827+
selectApplicableNetworkToCreateVm(caller, newAccount, zone, applicableNetworks, isNetworkCreated);
78297828
}
78307829

78317830
addNicsToApplicableNetworksAndReturnDefaultNetwork(applicableNetworks, requestedIPv4ForNics, requestedIPv6ForNics, networks);
@@ -7944,11 +7943,11 @@ protected NetworkVO addNicsToApplicableNetworksAndReturnDefaultNetwork(LinkedHas
79447943
* @param newAccount The new account associated to the selected network.
79457944
* @param zone The zone where the network is selected.
79467945
* @param applicableNetworks The applicable networks to which the selected network has to be added to.
7947-
* @throws InsufficientCapacityException
7948-
* @throws ResourceAllocationException
7946+
* @throws Exception
79497947
*/
7950-
protected void selectApplicableNetworkToCreateVm(Account caller, Account newAccount, DataCenterVO zone, Set<NetworkVO> applicableNetworks)
7951-
throws InsufficientCapacityException, ResourceAllocationException {
7948+
protected void selectApplicableNetworkToCreateVm(Account caller, Account newAccount, DataCenterVO zone,
7949+
Set<NetworkVO> applicableNetworks, MutableBoolean isNetworkCreated)
7950+
throws Exception {
79527951

79537952
logger.trace("Selecting the applicable network to create the VM.");
79547953

@@ -7969,6 +7968,7 @@ protected void selectApplicableNetworkToCreateVm(Account caller, Account newAcco
79697968
if (virtualNetworks.isEmpty()) {
79707969
try (TransactionLegacy txn = TransactionLegacy.open("CreateNetworkTxn")) {
79717970
defaultNetwork = createApplicableNetworkToCreateVm(caller, newAccount, zone, firstRequiredOffering);
7971+
isNetworkCreated.set(true);
79727972
txn.commit();
79737973
}
79747974
} else if (virtualNetworks.size() > 1) {
@@ -9166,4 +9166,12 @@ private void setVncPasswordForKvmIfAvailable(Map<String, String> customParameter
91669166
vm.setVncPassword(customParameters.get(VmDetailConstants.KVM_VNC_PASSWORD));
91679167
}
91689168
}
9169+
9170+
public static class MutableBoolean {
9171+
private boolean value;
9172+
public MutableBoolean(boolean val) { this.value = val; }
9173+
public void set(boolean val) { this.value = val; }
9174+
public boolean get() { return this.value; }
9175+
}
9176+
91699177
}

0 commit comments

Comments
 (0)