@@ -7445,6 +7445,8 @@ public UserVm moveVmToUser(final AssignVMCmd cmd) throws ResourceAllocationExcep
74457445 logger .trace ("Verifying if the new account [{}] has access to the specified domain [{}]." , newAccount , domain );
74467446 _accountMgr .checkAccess (newAccount , domain );
74477447
7448+ ensureDestinationNetwork (cmd , vm , newAccount );
7449+
74487450 Transaction .execute (new TransactionCallbackNoReturn () {
74497451 @ Override
74507452 public void doInTransactionWithoutResult (TransactionStatus status ) {
@@ -7556,6 +7558,54 @@ protected void validateIfNewOwnerHasAccessToTemplate(UserVmVO vm, Account newAcc
75567558 }
75577559 }
75587560
7561+ /**
7562+ * <javadoc about in what situation the network will be created>
7563+ */
7564+ protected void ensureDestinationNetwork (AssignVMCmd cmd , UserVmVO vm , Account newAccount ) throws InsufficientCapacityException , ResourceAllocationException {
7565+ DataCenterVO zone = _dcDao .findById (vm .getDataCenterId ());
7566+ if (zone .getNetworkType () == NetworkType .Basic ) {
7567+ return ;
7568+ }
7569+
7570+ List <Long > networkIdList = cmd .getNetworkIds ();
7571+ List <Long > securityGroupIdList = cmd .getSecurityGroupIdList ();
7572+ if (_networkModel .checkSecurityGroupSupportForNetwork (newAccount , zone , networkIdList , securityGroupIdList )) {
7573+ return ;
7574+ }
7575+ if (securityGroupIdList != null && !securityGroupIdList .isEmpty ()) {
7576+ return ;
7577+ }
7578+
7579+ VirtualMachine vmoi = _itMgr .findById (vm .getId ());
7580+ VirtualMachineProfileImpl vmOldProfile = new VirtualMachineProfileImpl (vmoi );
7581+ LinkedHashSet <NetworkVO > applicableNetworks = new LinkedHashSet <>();
7582+
7583+ addNetworksToNetworkIdList (vm , newAccount , vmOldProfile , networkIdList , applicableNetworks , new HashMap <>(), new HashMap <>());
7584+ if (!applicableNetworks .isEmpty ()) {
7585+ return ;
7586+ }
7587+
7588+ List <? extends Network > virtualNetworks = _networkModel .listNetworksForAccount (newAccount .getId (), zone .getId (), Network .GuestType .Isolated );
7589+ if (!virtualNetworks .isEmpty ()) {
7590+ return ;
7591+ }
7592+
7593+ List <NetworkOfferingVO > requiredOfferings = _networkOfferingDao .listByAvailability (Availability .Required , false );
7594+ if (CollectionUtils .isEmpty (requiredOfferings )) {
7595+ throw new InvalidParameterValueException (String .format ("Unable to find network offering with availability [%s] to automatically create the network as a part of VM "
7596+ + "creation." , Availability .Required ));
7597+ }
7598+
7599+ NetworkOfferingVO firstRequiredOffering = requiredOfferings .get (0 );
7600+ if (firstRequiredOffering .getState () != NetworkOffering .State .Enabled ) {
7601+ throw new InvalidParameterValueException (String .format ("Required network offering ID [%s] is not in [%s] state." , firstRequiredOffering .getId (),
7602+ NetworkOffering .State .Enabled ));
7603+ }
7604+
7605+ Account caller = CallContext .current ().getCallingAccount ();
7606+ createApplicableNetworkToCreateVm (caller , newAccount , zone , firstRequiredOffering );
7607+ }
7608+
75597609 /**
75607610 * Executes all ownership steps necessary to assign a VM to another user:
75617611 * generating a destroy VM event ({@link EventTypes}),
@@ -7802,6 +7852,7 @@ protected void updateAdvancedTypeNetworkForVm(AssignVMCmd cmd, Account caller, U
78027852 _securityGroupMgr .removeInstanceFromGroups (vm );
78037853
78047854 addNetworksToNetworkIdList (vm , newAccount , vmOldProfile , networkIdList , applicableNetworks , requestedIPv4ForNics , requestedIPv6ForNics );
7855+ cleanupOfOldOwnerNicsForNetwork (vmOldProfile );
78057856
78067857 NetworkVO defaultNetwork = addNicsToApplicableNetworksAndReturnDefaultNetwork (applicableNetworks , requestedIPv4ForNics , requestedIPv6ForNics , networks );
78077858
@@ -7820,6 +7871,7 @@ protected void updateAdvancedTypeNetworkForVm(AssignVMCmd cmd, Account caller, U
78207871 }
78217872
78227873 addNetworksToNetworkIdList (vm , newAccount , vmOldProfile , networkIdList , applicableNetworks , requestedIPv4ForNics , requestedIPv6ForNics );
7874+ cleanupOfOldOwnerNicsForNetwork (vmOldProfile );
78237875
78247876 if (applicableNetworks .isEmpty ()) {
78257877 selectApplicableNetworkToCreateVm (caller , newAccount , zone , applicableNetworks );
@@ -7878,7 +7930,6 @@ protected void addSecurityGroupsToVm(Account newAccount, UserVmVO vm, VirtualMac
78787930 * Adds all networks to the list of network IDs by:
78797931 * attempting to keep the shared network for the VM ({@link #keepOldSharedNetworkForVm(UserVmVO, Account, List, Set, Map, Map)}),
78807932 * adding any additional applicable networks to the VM ({@link #addAdditionalNetworksToVm(UserVmVO, Account, List, Set, Map, Map)}),
7881- * and cleaning up the network associated to the old owner ({@link #cleanupOfOldOwnerNicsForNetwork(VirtualMachineProfileImpl)}).
78827933 * @param vm The VM to add the networks to.
78837934 * @param newAccount The account to access the networks.
78847935 * @param vmOldProfile The old profile of the VM.
@@ -7895,8 +7946,6 @@ protected void addNetworksToNetworkIdList(UserVmVO vm, Account newAccount, Virtu
78957946 keepOldSharedNetworkForVm (vm , newAccount , networkIdList , applicableNetworks , requestedIPv4ForNics , requestedIPv6ForNics );
78967947
78977948 addAdditionalNetworksToVm (vm , newAccount , networkIdList , applicableNetworks , requestedIPv4ForNics , requestedIPv6ForNics );
7898-
7899- cleanupOfOldOwnerNicsForNetwork (vmOldProfile );
79007949 }
79017950
79027951 /**
@@ -7949,22 +7998,11 @@ protected void selectApplicableNetworkToCreateVm(Account caller, Account newAcco
79497998
79507999 logger .trace ("Selecting the applicable network to create the VM." );
79518000
7952- List <NetworkOfferingVO > requiredOfferings = _networkOfferingDao .listByAvailability (Availability .Required , false );
7953- if (CollectionUtils .isEmpty (requiredOfferings )) {
7954- throw new InvalidParameterValueException (String .format ("Unable to find network offering with availability [%s] to automatically create the network as a part of VM "
7955- + "creation." , Availability .Required ));
7956- }
7957-
7958- NetworkOfferingVO firstRequiredOffering = requiredOfferings .get (0 );
7959- if (firstRequiredOffering .getState () != NetworkOffering .State .Enabled ) {
7960- throw new InvalidParameterValueException (String .format ("Required network offering ID [%s] is not in [%s] state." , firstRequiredOffering .getId (),
7961- NetworkOffering .State .Enabled ));
7962- }
7963-
79648001 NetworkVO defaultNetwork ;
79658002 List <? extends Network > virtualNetworks = _networkModel .listNetworksForAccount (newAccount .getId (), zone .getId (), Network .GuestType .Isolated );
79668003 if (virtualNetworks .isEmpty ()) {
7967- defaultNetwork = createApplicableNetworkToCreateVm (caller , newAccount , zone , firstRequiredOffering );
8004+ throw new CloudRuntimeException ("" );
8005+ // defaultNetwork = createApplicableNetworkToCreateVm(caller, newAccount, zone, firstRequiredOffering);
79688006 } else if (virtualNetworks .size () > 1 ) {
79698007 throw new InvalidParameterValueException (String .format ("More than one default isolated network has been found for account [%s]; please specify networkIDs." ,
79708008 newAccount ));
0 commit comments