Bug 14299 - Fix adding public ip range per account

Changes:
Fixed as described in the bug.

* CreateVlanIpRangeCmd still accept account/domainId info
* if account owns:
- one Isolated network with source nat service enabled, use this network
- more than one Isolated network with source nat service enabled - error out
- none Isolated networks with source nat service enabled, create it only in
case when there is an Isolated network offering with Availability=Required and
source nat service enabled.
This commit is contained in:
prachi 2012-03-21 16:34:47 -07:00
parent 1a8e5287fd
commit 45c7ad63bf
4 changed files with 52 additions and 10 deletions

View File

@ -133,4 +133,6 @@ public interface NetworkService {
List<Pair<TrafficType, String>> listTrafficTypeImplementor(ListTrafficTypeImplementorsCmd cmd);
List<? extends Network> getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long zoneId, Account owner);
}

View File

@ -954,6 +954,12 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
return _networksDao.listBy(owner.getId(), zoneId, Network.GuestType.Isolated);
}
@Override
public List<? extends Network> getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long zoneId, Account owner) {
return _networksDao.listSourceNATEnabledNetworks(owner.getId(), zoneId, Network.GuestType.Isolated);
}
@Override
@DB
@ -3654,24 +3660,34 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
txn.start();
if (network == null) {
List<? extends Network> networks = getIsolatedNetworksOwnedByAccountInZone(zoneId, owner);
List<? extends Network> networks = getIsolatedNetworksWithSourceNATOwnedByAccountInZone(zoneId, owner);
if (networks.size() == 0) {
createNetwork = true;
} else {
} else if (networks.size() == 1) {
network = networks.get(0);
}else{
throw new InvalidParameterValueException("Error, more than 1 Guest Isolated Networks with SourceNAT service enabled found for this account, cannot assosiate the IP range, please provide the network ID");
}
}
// create new Virtual network for the user if it doesn't exist
// create new Virtual network (Isolated with SourceNAT) for the user if it doesn't exist
if (createNetwork) {
List<? extends NetworkOffering> offerings = _configMgr.listNetworkOfferings(TrafficType.Guest, false);
List<NetworkOfferingVO> requiredOfferings = _networkOfferingDao.listByAvailability(Availability.Required, false);
if (requiredOfferings.size() < 1) {
throw new CloudRuntimeException("Unable to find network offering with availability=" + Availability.Required + " to automatically create the network as part of createVlanIpRange");
}
PhysicalNetwork physicalNetwork = translateZoneIdToPhysicalNetwork(zoneId);
network = createGuestNetwork(offerings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null, null, null, owner, false, null, physicalNetwork, zoneId,
ACLType.Account, null);
if (network == null) {
s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId);
return false;
if (requiredOfferings.get(0).getState() == NetworkOffering.State.Enabled) {
s_logger.debug("Creating network for account " + owner + " from the network offering id=" + requiredOfferings.get(0).getId() + " as a part of createVlanIpRange process");
network = createGuestNetwork(requiredOfferings.get(0).getId(), owner.getAccountName() + "-network", owner.getAccountName() + "-network", null, null, null, null, owner, false, null, physicalNetwork, zoneId,
ACLType.Account, null);
if (network == null) {
s_logger.warn("Failed to create default Virtual network for the account " + accountId + "in zone " + zoneId);
throw new CloudRuntimeException("Failed to create a Guest Isolated Networks with SourceNAT service enabled as a part of createVlanIpRange, for the account " + accountId + "in zone " + zoneId);
}
} else {
throw new CloudRuntimeException("Required network offering id=" + requiredOfferings.get(0).getId() + " is not in " + NetworkOffering.State.Enabled);
}
}

View File

@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import com.cloud.network.Network;
import com.cloud.network.Network.GuestType;
import com.cloud.network.NetworkAccountVO;
import com.cloud.network.NetworkVO;
import com.cloud.network.Networks.TrafficType;
@ -98,4 +99,6 @@ public interface NetworkDao extends GenericDao<NetworkVO, Long> {
long countNetworksUserCanCreate(long ownerId);
List<NetworkVO> listSourceNATEnabledNetworks(long accountId, long dataCenterId, GuestType type);
}

View File

@ -66,6 +66,7 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
final SearchBuilder<NetworkVO> SecurityGroupSearch;
final GenericSearchBuilder<NetworkVO, Long> NetworksRegularUserCanCreateSearch;
private final GenericSearchBuilder<NetworkVO, Integer> NetworksCount;
final SearchBuilder<NetworkVO> SourceNATSearch;
NetworkAccountDaoImpl _accountsDao = ComponentLocator.inject(NetworkAccountDaoImpl.class);
@ -164,6 +165,15 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
NetworksRegularUserCanCreateSearch.done();
_tgMacAddress = _tgs.get("macAddress");
SourceNATSearch = createSearchBuilder();
SourceNATSearch.and("account", SourceNATSearch.entity().getAccountId(), Op.EQ);
SourceNATSearch.and("datacenter", SourceNATSearch.entity().getDataCenterId(), Op.EQ);
SourceNATSearch.and("guestType", SourceNATSearch.entity().getGuestType(), Op.EQ);
SearchBuilder<NetworkServiceMapVO> join6 = _ntwkSvcMap.createSearchBuilder();
join6.and("service", join6.entity().getService(), Op.EQ);
SourceNATSearch.join("services", join6, SourceNATSearch.entity().getId(), join6.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
SourceNATSearch.done();
}
@ -442,5 +452,16 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
sc.setJoinParameters("ntwkOff", "specifyVlan", false);
return customSearch(sc, null).get(0);
}
@Override
public List<NetworkVO> listSourceNATEnabledNetworks(long accountId, long dataCenterId, Network.GuestType type) {
SearchCriteria<NetworkVO> sc = SourceNATSearch.create();
sc.setParameters("datacenter", dataCenterId);
sc.setParameters("account", accountId);
sc.setParameters("guestType", type);
sc.setJoinParameters("services", "service", Service.SourceNat.getName());
return listBy(sc);
}
}