Skip to content

Commit 23482e4

Browse files
committed
Fix issue with Assign VM operation
1 parent f6d0590 commit 23482e4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import javax.xml.parsers.DocumentBuilder;
5555
import javax.xml.parsers.ParserConfigurationException;
5656

57+
import com.cloud.utils.db.TransactionLegacy;
5758
import org.apache.cloudstack.acl.ControlledEntity;
5859
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
5960
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -7594,6 +7595,11 @@ protected void executeStepsToChangeOwnershipOfVm(AssignVMCmd cmd, Account caller
75947595
try {
75957596
updateVmNetwork(cmd, caller, vm, newAccount, template);
75967597
} catch (InsufficientCapacityException | ResourceAllocationException e) {
7598+
List<NetworkVO> networkVOS = _networkDao.listByAccountIdNetworkName(newAccountId, newAccount.getAccountName() + "-network");
7599+
if (networkVOS.size() == 1) {
7600+
_networkDao.remove(networkVOS.get(0).getId());
7601+
}
7602+
_accountMgr.getActiveAccountByName(newAccount.getAccountName() + "-network", newAccount.getDomainId());
75977603
throw new CloudRuntimeException(String.format("Unable to update networks when assigning VM [%s] due to [%s].", vm, e.getMessage()), e);
75987604
}
75997605

@@ -7961,7 +7967,10 @@ protected void selectApplicableNetworkToCreateVm(Account caller, Account newAcco
79617967
NetworkVO defaultNetwork;
79627968
List<? extends Network> virtualNetworks = _networkModel.listNetworksForAccount(newAccount.getId(), zone.getId(), Network.GuestType.Isolated);
79637969
if (virtualNetworks.isEmpty()) {
7964-
defaultNetwork = createApplicableNetworkToCreateVm(caller, newAccount, zone, firstRequiredOffering);
7970+
try (TransactionLegacy txn = TransactionLegacy.open("CreateNetworkTxn")) {
7971+
defaultNetwork = createApplicableNetworkToCreateVm(caller, newAccount, zone, firstRequiredOffering);
7972+
txn.commit();
7973+
}
79657974
} else if (virtualNetworks.size() > 1) {
79667975
throw new InvalidParameterValueException(String.format("More than one default isolated network has been found for account [%s]; please specify networkIDs.",
79677976
newAccount));

0 commit comments

Comments
 (0)