Skip to content

Commit df6ce24

Browse files
author
Anthony Xu
committed
if networkID is not specified, get one network with free ips.
1 parent 96f092a commit df6ce24

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

api/src/com/cloud/network/NetworkModel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public interface NetworkModel {
9292

9393
boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Service... services);
9494

95+
Network getNetworkWithSGWithFreeIPs(Long zoneId);
96+
9597
Network getNetworkWithSecurityGroupEnabled(Long zoneId);
9698

9799
String getIpOfNetworkElementInVirtualNetwork(long accountId, long dataCenterId);
@@ -277,4 +279,4 @@ public interface NetworkModel {
277279
boolean getNetworkEgressDefaultPolicy(Long networkId);
278280

279281
void checkNetworkPermissions(Account owner, Network network, AccessType accessType);
280-
}
282+
}

server/src/com/cloud/network/NetworkModelImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,31 @@ public NetworkVO getSystemNetworkByZoneAndTrafficType(long zoneId, TrafficType t
740740
return networks.get(0);
741741
}
742742

743+
@Override
744+
public NetworkVO getNetworkWithSGWithFreeIPs(Long zoneId) {
745+
List<NetworkVO> networks = _networksDao.listByZoneSecurityGroup(zoneId);
746+
if (networks == null || networks.isEmpty()) {
747+
return null;
748+
}
749+
NetworkVO ret_network = null;
750+
for (NetworkVO nw : networks) {
751+
List<VlanVO> vlans = _vlanDao.listVlansByNetworkId(nw.getId());
752+
for (VlanVO vlan : vlans) {
753+
if (_ipAddressDao.countFreeIpsInVlan(vlan.getId()) > 0) {
754+
ret_network = nw;
755+
break;
756+
}
757+
}
758+
if (ret_network != null) {
759+
break;
760+
}
761+
}
762+
if (ret_network == null) {
763+
s_logger.debug("Can not find network with security group enabled with free IPs");
764+
}
765+
return ret_network;
766+
}
767+
743768
@Override
744769
public NetworkVO getNetworkWithSecurityGroupEnabled(Long zoneId) {
745770
List<NetworkVO> networks = _networksDao.listByZoneSecurityGroup(zoneId);

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ public UserVm createAdvancedSecurityGroupVirtualMachine(DataCenter zone, Service
23502350

23512351
// If no network is specified, find system security group enabled network
23522352
if (networkIdList == null || networkIdList.isEmpty()) {
2353-
Network networkWithSecurityGroup = _networkModel.getNetworkWithSecurityGroupEnabled(zone.getId());
2353+
Network networkWithSecurityGroup = _networkModel.getNetworkWithSGWithFreeIPs(zone.getId());
23542354
if (networkWithSecurityGroup == null) {
23552355
throw new InvalidParameterValueException("No network with security enabled is found in zone id=" + zone.getId());
23562356
}

server/test/com/cloud/network/MockNetworkModelImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Ser
229229
return false;
230230
}
231231

232+
/* (non-Javadoc)
233+
* @see com.cloud.network.NetworkModel#getNetworkWithSGWithFreeIPs(java.lang.Long)
234+
*/
235+
@Override
236+
public NetworkVO getNetworkWithSGWithFreeIPs(Long zoneId) {
237+
// TODO Auto-generated method stub
238+
return null;
239+
}
240+
232241
/* (non-Javadoc)
233242
* @see com.cloud.network.NetworkModel#getNetworkWithSecurityGroupEnabled(java.lang.Long)
234243
*/

server/test/com/cloud/vpc/MockNetworkModelImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Ser
240240
return (_ntwkOfferingSrvcDao.areServicesSupportedByNetworkOffering(networkOfferingId, services));
241241
}
242242

243+
/* (non-Javadoc)
244+
* @see com.cloud.network.NetworkModel#getNetworkWithSGWithFreeIPs(java.lang.Long)
245+
*/
246+
@Override
247+
public NetworkVO getNetworkWithSGWithFreeIPs(Long zoneId) {
248+
// TODO Auto-generated method stub
249+
return null;
250+
}
251+
243252
/* (non-Javadoc)
244253
* @see com.cloud.network.NetworkModel#getNetworkWithSecurityGroupEnabled(java.lang.Long)
245254
*/

0 commit comments

Comments
 (0)