This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch fix-assign-vm
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit cac5905ecf89eecbac1c939ffec16c84fe0d04de
Author: Pearl Dsilva <pearl1...@gmail.com>
AuthorDate: Fri May 9 17:05:28 2025 +0530

    fix assign vm issue
---
 .../main/java/com/cloud/vm/UserVmManagerImpl.java  | 40 +++++++++++++---------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java 
b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
index 40ba5ba1ed3..61d127e55aa 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 @@ 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; }
+    }
+
 }

Reply via email to