From 68219eba31e749e05bccbb97907a2d844d48fde3 Mon Sep 17 00:00:00 2001 From: alena Date: Tue, 15 Mar 2011 10:05:07 -0700 Subject: [PATCH] Fixed create vlan range for Account - pick source nat ip address from account specific vlan --- .../com/cloud/network/NetworkManagerImpl.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 7bf74dc7916..4dcd008c656 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -1589,7 +1589,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // allow isDefault/isShared to be set only for Direct network if (networkOffering.getGuestType() == GuestIpType.Virtual) { - if (isDefault != null) { + if (isDefault != null && !isDefault) { throw new InvalidParameterValueException("Can specify isDefault parameter only for Direct network."); } else { isDefault = true; @@ -2428,15 +2428,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override @DB public boolean associateIpAddressListToAccount(long userId, long accountId, long zoneId, Long vlanId, Network network) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException { Account owner = _accountMgr.getActiveAccount(accountId); - boolean createNetwork = true; + boolean createNetwork = false; Transaction txn= Transaction.currentTxn(); txn.start(); - if (network != null) { - createNetwork = false; - } else { + if (network == null) { List networks = getVirtualNetworksOwnedByAccountInZone(owner.getAccountName(), owner.getDomainId(), zoneId); if (networks.size() == 0) { createNetwork = true; @@ -2448,7 +2446,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // create new Virtual network for the user if it doesn't exist if (createNetwork) { List offerings = _configMgr.listNetworkOfferings(TrafficType.Guest, false); - network = createNetwork(offerings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", false, true, zoneId, null, null, null, null, owner, false); + network = createNetwork(offerings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", false, null, zoneId, null, null, null, null, owner, false); if (network == null) { s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId); @@ -2456,13 +2454,25 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } + //Check if there is a source nat ip address for this account; if not - we have to allocate one + boolean allocateSourceNat = false; + List sourceNat = _ipAddressDao.listByAssociatedNetwork(network.getId(), true); + if (sourceNat.isEmpty()) { + allocateSourceNat = true; + } //update all ips with a network id, mark them as allocated and update resourceCount/usage List ips = _ipAddressDao.listByVlanId(vlanId); + boolean isSourceNatAllocated = false; for (IPAddressVO addr : ips) { - if (!addr.isSourceNat() && addr.getState() != State.Allocated) { + if (addr.getState() != State.Allocated) { + if (!isSourceNatAllocated && allocateSourceNat) { + addr.setSourceNat(true); + isSourceNatAllocated = true; + } else { + addr.setSourceNat(false); + } addr.setAssociatedWithNetworkId(network.getId()); - addr.setSourceNat(false); addr.setAllocatedTime(new Date()); addr.setAllocatedInDomainId(owner.getDomainId()); addr.setAllocatedToAccountId(owner.getId());