Skip to content

Commit a2a4968

Browse files
authored
server: Allow creating network with duplicate name (#3807)
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 By default we can create networks with the same name in the account. Sometimes we should not create the networks with same name. This change adds a global setting which prevents creating the network with same name. The default value is true and set it to false to prevent creating network with same names. Also its possible to create a shared network without mentioning the start and the end IPv4 or IPv6 address. This change adds a global setting which prevents creating a shared network without specifying the start and the end IPv4 or IPv6 address
1 parent 1a5b7c3 commit a2a4968

6 files changed

Lines changed: 351 additions & 1 deletion

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ env:
4444
smoke/test_affinity_groups_projects
4545
smoke/test_async_job
4646
smoke/test_create_list_domain_account_project
47+
smoke/test_create_network
4748
smoke/test_deploy_vgpu_enabled_vm
4849
smoke/test_deploy_vm_iso
4950
smoke/test_deploy_vm_root_resize

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
@@ -4537,4 +4568,14 @@ public boolean releasePodIp(ReleasePodIpCmdByAdmin ip) throws CloudRuntimeExcept
45374568
return true;
45384569
}
45394570

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

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)