Skip to content

Commit 43a5b71

Browse files
committed
Enhancement: Allow creating network with duplicate name
Add a global setting to disable creating networks with same name in an account Add a global setting to disable creating network without mentioning the start and end IPv4 or IPv6 address
1 parent cc093c6 commit 43a5b71

5 files changed

Lines changed: 349 additions & 1 deletion

File tree

engine/schema/src/main/java/com/cloud/network/dao/NetworkDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,6 @@ public interface NetworkDao extends GenericDao<NetworkVO, Long>, StateDao<State,
120120
int getNonSystemNetworkCountByVpcId(long vpcId);
121121

122122
List<NetworkVO> listNetworkVO(List<Long> idset);
123+
124+
List<NetworkVO> listByAccountIdNetworkName(long accountId, String name);
123125
}

engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public NetworkDaoImpl() {
104104
@PostConstruct
105105
protected void init() {
106106
AllFieldsSearch = createSearchBuilder();
107+
AllFieldsSearch.and("name", AllFieldsSearch.entity().getName(), Op.EQ);
107108
AllFieldsSearch.and("trafficType", AllFieldsSearch.entity().getTrafficType(), Op.EQ);
108109
AllFieldsSearch.and("cidr", AllFieldsSearch.entity().getCidr(), Op.EQ);
109110
AllFieldsSearch.and("broadcastType", AllFieldsSearch.entity().getBroadcastDomainType(), Op.EQ);
@@ -697,4 +698,13 @@ public List<NetworkVO> listNetworkVO(List<Long> idset) {
697698
sc_2.addAnd("removed", SearchCriteria.Op.EQ, null);
698699
return this.search(sc_2, searchFilter_2);
699700
}
701+
702+
@Override
703+
public List<NetworkVO> listByAccountIdNetworkName(final long accountId, final String name) {
704+
final SearchCriteria<NetworkVO> sc = AllFieldsSearch.create();
705+
sc.setParameters("account", accountId);
706+
sc.setParameters("name", name);
707+
708+
return listBy(sc, null);
709+
}
700710
}

server/src/main/java/com/cloud/network/NetworkServiceImpl.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import org.apache.cloudstack.api.response.AcquirePodIpCmdResponse;
5656
import org.apache.cloudstack.context.CallContext;
5757
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
58+
import org.apache.cloudstack.framework.config.ConfigKey;
59+
import org.apache.cloudstack.framework.config.Configurable;
5860
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
5961
import org.apache.cloudstack.framework.messagebus.MessageBus;
6062
import org.apache.cloudstack.framework.messagebus.PublishScope;
@@ -107,6 +109,7 @@
107109
import com.cloud.network.dao.IPAddressDao;
108110
import com.cloud.network.dao.IPAddressVO;
109111
import com.cloud.network.dao.LoadBalancerDao;
112+
import com.cloud.network.dao.NetworkAccountDao;
110113
import com.cloud.network.dao.NetworkDao;
111114
import com.cloud.network.dao.NetworkDetailVO;
112115
import com.cloud.network.dao.NetworkDetailsDao;
@@ -200,9 +203,14 @@
200203
/**
201204
* NetworkServiceImpl implements NetworkService.
202205
*/
203-
public class NetworkServiceImpl extends ManagerBase implements NetworkService {
206+
public class NetworkServiceImpl extends ManagerBase implements NetworkService, Configurable {
204207
private static final Logger s_logger = Logger.getLogger(NetworkServiceImpl.class);
205208

209+
private static final ConfigKey<Boolean> AllowDuplicateNetworkName = new ConfigKey<Boolean>("Advanced", Boolean.class,
210+
"allow.duplicate.networkname", "true", "Allow creating networks with same name in account", true, ConfigKey.Scope.Account);
211+
private static final ConfigKey<Boolean> AllowEmptyStartEndIpAddress = new ConfigKey<Boolean>("Advanced", Boolean.class,
212+
"allow.empty.start.end.ipaddress", "true", "Allow creating network without mentioning start and end IP address",
213+
true, ConfigKey.Scope.Account);
206214
private static final long MIN_VLAN_ID = 0L;
207215
private static final long MAX_VLAN_ID = 4095L; // 2^12 - 1
208216
private static final long MIN_GRE_KEY = 0L;
@@ -313,6 +321,8 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
313321
VpcOfferingDao _vpcOfferingDao;
314322
@Inject
315323
AccountService _accountService;
324+
@Inject
325+
NetworkAccountDao _networkAccountDao;
316326

317327
int _cidrLimit;
318328
boolean _allowSubdomainNetworkAccess;
@@ -1164,6 +1174,18 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
11641174
throw new InvalidParameterValueException("Parameter subDomainAccess can be specified only with aclType=Domain");
11651175
}
11661176

1177+
if (aclType == ACLType.Domain) {
1178+
owner = _accountDao.findById(Account.ACCOUNT_ID_SYSTEM);
1179+
}
1180+
1181+
// The network name is unique under the account
1182+
if (!AllowDuplicateNetworkName.valueIn(owner.getAccountId())) {
1183+
List<NetworkVO> existingNetwork = _networksDao.listByAccountIdNetworkName(owner.getId(), name);
1184+
if (!existingNetwork.isEmpty()) {
1185+
throw new InvalidParameterValueException("Another network with same name already exists within account: " + owner.getAccountName());
1186+
}
1187+
}
1188+
11671189
boolean ipv4 = true, ipv6 = false;
11681190
if (startIP != null) {
11691191
ipv4 = true;
@@ -1188,6 +1210,15 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
11881210
}
11891211
}
11901212

1213+
// Start and end IP address are mandatory for shared networks.
1214+
if (ntwkOff.getGuestType() == GuestType.Shared && vpcId == null) {
1215+
if (!AllowEmptyStartEndIpAddress.valueIn(owner.getAccountId()) &&
1216+
(startIP == null && endIP == null) &&
1217+
(startIPv6 == null && endIPv6 == null)) {
1218+
throw new InvalidParameterValueException("Either IPv4 or IPv6 start and end address are mandatory");
1219+
}
1220+
}
1221+
11911222
String cidr = null;
11921223
if (ipv4) {
11931224
// if end ip is not specified, default it to startIp
@@ -4534,4 +4565,14 @@ public boolean releasePodIp(ReleasePodIpCmdByAdmin ip) throws CloudRuntimeExcept
45344565
return true;
45354566
}
45364567

4568+
@Override
4569+
public String getConfigComponentName() {
4570+
return NetworkService.class.getSimpleName();
4571+
}
4572+
4573+
@Override
4574+
public ConfigKey<?>[] getConfigKeys() {
4575+
return new ConfigKey<?>[] {AllowDuplicateNetworkName, AllowEmptyStartEndIpAddress};
4576+
}
4577+
45374578
}

server/src/test/java/com/cloud/vpc/dao/MockNetworkDaoImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,9 @@ public int getNonSystemNetworkCountByVpcId(final long vpcId) {
235235
public List<NetworkVO> listNetworkVO(List<Long> idset) {
236236
return null;
237237
}
238+
239+
@Override
240+
public List<NetworkVO> listByAccountIdNetworkName(final long accountId, final String name) {
241+
return null;
242+
}
238243
}

0 commit comments

Comments
 (0)