From 5f12ea73a87dd1fbbe425450867d488df1a2dc0a Mon Sep 17 00:00:00 2001 From: Abhinandan Prateek Date: Mon, 4 Jul 2011 13:15:40 +0530 Subject: [PATCH] bug 8962: Completed moving VM from one account to another. --- .../src/com/cloud/vm/UserVmManagerImpl.java | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 42181fe8abe..5f482aafaaf 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -3310,52 +3310,57 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } // OS 3: update the network - s_logger.warn("zone.getNetworkType() = " + zone.getNetworkType()); - if (zone.getNetworkType() == NetworkType.Basic) { - s_logger.warn("Basic network type"); - } - else { + if (zone.getNetworkType() == NetworkType.Advanced) { //cleanup the network for the oldOwner _networkMgr.cleanupNics(vmOldProfile); _networkMgr.expungeNics(vmOldProfile); - s_logger.warn("Nics expunged "); // add the new nics List networkList = new ArrayList(); NetworkVO defaultNetwork = null; - List oldNetwork = _networkMgr.listNetworksForAccount(oldAccount.getId(), zone.getId(), GuestIpType.Virtual, true); - long networkOffering = oldNetwork.get(0).getNetworkOfferingId(); - s_logger.warn("Old network offering " + networkOffering); - List virtualNetworks = _networkMgr.listNetworksForAccount(newAccount.getId(), zone.getId(), GuestIpType.Virtual, true); - if (virtualNetworks.isEmpty()) { - s_logger.warn("Creating default Virtual network for account " + newAccount + " as a part of deployVM process"); - Network newNetwork = _networkMgr.createNetwork(networkOffering, newAccount.getAccountName() + "-network", newAccount.getAccountName() + "-network", false, null, - vm.getDataCenterIdToDeployIn(), null, null, null, null, newAccount, false, null, null); - s_logger.warn("New network " + newNetwork); - defaultNetwork = _networkDao.findById(newNetwork.getId()); - } else if (virtualNetworks.size() > 1) { - throw new InvalidParameterValueException("More than 1 default Virtaul networks are found for account " + newAccount + "; please specify networkIds"); - } else { - defaultNetwork = virtualNetworks.get(0); - s_logger.warn("Virtaul networks " + virtualNetworks.get(0)); - } - - networkList.add(defaultNetwork); - List> networks = new ArrayList>(); - short defaultNetworkNumber = 0; - for (NetworkVO network : networkList) { - - if (network.isDefault()) { - defaultNetworkNumber++; - } - - networks.add(new Pair(network, null)); - } - VMInstanceVO vmi = _itMgr.findById(vm.getType(), vm.getId()); - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmi); - _networkMgr.allocate(vmProfile, networks); - s_logger.warn("Saved the network profile "); + List oldNetworks = new ArrayList(); + List zoneNetworks = _networkDao.listByZone(zone.getId()); + + for (NetworkVO network : zoneNetworks) { // get the default networks for the account + NetworkOfferingVO no = _networkOfferingDao.findById(network.getNetworkOfferingId()); + if (!no.isSystemOnly()) { + if (network.getIsShared() || !_networkDao.listBy(oldAccount.getId(), network.getId()).isEmpty()) { + if (network.isDefault()) { + oldNetworks.add(network); + } + } + } + } + for (NetworkVO oldNet: oldNetworks){ + long networkOffering = oldNet.getNetworkOfferingId(); + List virtualNetworks = _networkMgr.listNetworksForAccount(newAccount.getId(), zone.getId(), GuestIpType.Virtual, true); + if (virtualNetworks.isEmpty()) { + Network newNetwork = _networkMgr.createNetwork(networkOffering, newAccount.getAccountName() + "-network", newAccount.getAccountName() + "-network", false, null, + vm.getDataCenterIdToDeployIn(), null, null, null, null, newAccount, false, null, null); + defaultNetwork = _networkDao.findById(newNetwork.getId()); + } else if (virtualNetworks.size() > 1) { + throw new InvalidParameterValueException("More than 1 default Virtaul networks are found for account " + newAccount + "; please specify networkIds"); + } else { + defaultNetwork = virtualNetworks.get(0); + } + + networkList.add(defaultNetwork); + List> networks = new ArrayList>(); + short defaultNetworkNumber = 0; + for (NetworkVO network : networkList) { + + if (network.isDefault()) { + defaultNetworkNumber++; + } + + networks.add(new Pair(network, null)); + } + + VMInstanceVO vmi = _itMgr.findById(vm.getType(), vm.getId()); + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmi); + _networkMgr.allocate(vmProfile, networks); + } } @@ -3363,4 +3368,5 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } + }