@@ -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 );
7596+ updateVmNetwork (cmd , caller , vm , newAccount , template , isNetworkCreated );
75977597 } catch (InsufficientCapacityException | ResourceAllocationException 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,7 +7662,7 @@ 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 )
7665+ protected void updateVmNetwork (AssignVMCmd cmd , Account caller , UserVmVO vm , Account newAccount , VirtualMachineTemplate template , MutableBoolean isNetworkCreated )
76677666 throws InsufficientCapacityException , ResourceAllocationException {
76687667
76697668 logger .trace ("Updating network for VM [{}]." , vm );
@@ -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,7 +7791,7 @@ 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 )
7794+ VirtualMachineProfileImpl vmOldProfile , DataCenterVO zone , List <Long > networkIdList , List <Long > securityGroupIdList , MutableBoolean isNetworkCreated )
77967795 throws InsufficientCapacityException , ResourceAllocationException , InvalidParameterValueException {
77977796
77987797 LinkedHashSet <NetworkVO > applicableNetworks = new LinkedHashSet <>();
@@ -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 );
@@ -7947,7 +7946,8 @@ protected NetworkVO addNicsToApplicableNetworksAndReturnDefaultNetwork(LinkedHas
79477946 * @throws InsufficientCapacityException
79487947 * @throws ResourceAllocationException
79497948 */
7950- protected void selectApplicableNetworkToCreateVm (Account caller , Account newAccount , DataCenterVO zone , Set <NetworkVO > applicableNetworks )
7949+ protected void selectApplicableNetworkToCreateVm (Account caller , Account newAccount , DataCenterVO zone ,
7950+ Set <NetworkVO > applicableNetworks , MutableBoolean isNetworkCreated )
79517951 throws InsufficientCapacityException , ResourceAllocationException {
79527952
79537953 logger .trace ("Selecting the applicable network to create the VM." );
@@ -7969,6 +7969,7 @@ protected void selectApplicableNetworkToCreateVm(Account caller, Account newAcco
79697969 if (virtualNetworks .isEmpty ()) {
79707970 try (TransactionLegacy txn = TransactionLegacy .open ("CreateNetworkTxn" )) {
79717971 defaultNetwork = createApplicableNetworkToCreateVm (caller , newAccount , zone , firstRequiredOffering );
7972+ isNetworkCreated .set (true );
79727973 txn .commit ();
79737974 }
79747975 } else if (virtualNetworks .size () > 1 ) {
@@ -9166,4 +9167,12 @@ private void setVncPasswordForKvmIfAvailable(Map<String, String> customParameter
91669167 vm .setVncPassword (customParameters .get (VmDetailConstants .KVM_VNC_PASSWORD ));
91679168 }
91689169 }
9170+
9171+ public static class MutableBoolean {
9172+ private boolean value ;
9173+ public MutableBoolean (boolean val ) { this .value = val ; }
9174+ public void set (boolean val ) { this .value = val ; }
9175+ public boolean get () { return this .value ; }
9176+ }
9177+
91699178}
0 commit comments