Skip to content

Commit ef6c0c4

Browse files
abh1sarDaanHooglandlucas-a-martinsLucas Martins
authored
Prepend VPC name to VPC network tier name (#9780)
* Fix `updateTemplatePermission` when the UI is set to a language other than English (#9766) * Fix updateTemplatePermission UI in non-english language * Improve fix --------- Co-authored-by: Lucas Martins <[email protected]> * Prepend vpc name to vpc tier network name based on global setting * Added UT for createVpcGuestNetwork * rename connector to delimiter and add configKey.Category.Network * Move setting the name to a new method --------- Co-authored-by: Daan Hoogland <[email protected]> Co-authored-by: Lucas Martins <[email protected]> Co-authored-by: Lucas Martins <[email protected]>
1 parent 546ef31 commit ef6c0c4

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

engine/components-api/src/main/java/com/cloud/network/vpc/VpcManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,26 @@
3737
import com.cloud.network.addr.PublicIp;
3838
import com.cloud.offering.NetworkOffering;
3939
import com.cloud.user.Account;
40+
import org.apache.cloudstack.framework.config.ConfigKey;
4041

4142
public interface VpcManager {
43+
ConfigKey<Boolean> VpcTierNamePrepend = new ConfigKey<>(Boolean.class,
44+
"vpc.tier.name.prepend",
45+
ConfigKey.CATEGORY_NETWORK,
46+
"false",
47+
"Whether to prepend the VPC name to the VPC tier network name",
48+
true,
49+
ConfigKey.Scope.Global,
50+
null);
51+
ConfigKey<String> VpcTierNamePrependDelimiter = new ConfigKey<>(String.class,
52+
"vpc.tier.name.prepend.delimiter",
53+
ConfigKey.CATEGORY_NETWORK,
54+
" ",
55+
"Delimiter string to use between the VPC and the VPC tier name",
56+
true,
57+
ConfigKey.Scope.Global,
58+
null);
59+
4260
/**
4361
* Returns all the Guest networks that are part of VPC
4462
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,11 @@ private void isIPv6AddressValid(String routerIPv6) {
14761476
}
14771477
}
14781478

1479+
private String getVpcPrependedNetworkName(String networkName, Vpc vpc) {
1480+
final String delimiter = VpcManager.VpcTierNamePrependDelimiter.value();
1481+
return vpc.getName() + delimiter + networkName;
1482+
}
1483+
14791484
@Override
14801485
@DB
14811486
@ActionEvent(eventType = EventTypes.EVENT_NETWORK_CREATE, eventDescription = "creating network")
@@ -1788,6 +1793,13 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
17881793

17891794
checkNetworkDns(ipv6, ntwkOff, vpcId, ip4Dns1, ip4Dns2, ip6Dns1, ip6Dns2);
17901795

1796+
if (vpcId != null && VpcManager.VpcTierNamePrepend.value()) {
1797+
Vpc vpc = _vpcDao.findById(vpcId);
1798+
if (vpc != null) {
1799+
name = getVpcPrependedNetworkName(name, vpc);
1800+
}
1801+
}
1802+
17911803
Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId, bypassVlanOverlapCheck, name, displayText, caller, physicalNetworkId, zone.getId(),
17921804
domainId, isDomainSpecific, subdomainAccess, vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, secondaryVlanId, privateVlanType, ntwkOff, pNtwk, aclType, owner, cidr, createVlan,
17931805
externalId, routerIPv4, routerIPv6, associatedNetwork, ip4Dns1, ip4Dns2, ip6Dns1, ip6Dns2, interfaceMTUs, networkCidrSize);

server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
import org.apache.cloudstack.api.command.user.vpc.UpdateVPCCmd;
7272
import org.apache.cloudstack.context.CallContext;
7373
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
74+
import org.apache.cloudstack.framework.config.ConfigKey;
75+
import org.apache.cloudstack.framework.config.Configurable;
7476
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
7577
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
7678
import org.apache.cloudstack.network.Ipv4GuestSubnetNetworkMap;
@@ -195,7 +197,7 @@
195197

196198
import static com.cloud.offering.NetworkOffering.RoutingMode.Dynamic;
197199

198-
public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvisioningService, VpcService {
200+
public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvisioningService, VpcService, Configurable {
199201

200202
public static final String SERVICE = "service";
201203
public static final String CAPABILITYTYPE = "capabilitytype";
@@ -3115,6 +3117,19 @@ protected void markStaticRouteForRevoke(final StaticRouteVO route, final Account
31153117
}
31163118
}
31173119

3120+
@Override
3121+
public String getConfigComponentName() {
3122+
return VpcManager.class.getSimpleName();
3123+
}
3124+
3125+
@Override
3126+
public ConfigKey<?>[] getConfigKeys() {
3127+
return new ConfigKey<?>[]{
3128+
VpcTierNamePrepend,
3129+
VpcTierNamePrependDelimiter
3130+
};
3131+
}
3132+
31183133
protected class VpcCleanupTask extends ManagedContextRunnable {
31193134
@Override
31203135
protected void runInContext() {

server/src/test/java/com/cloud/network/NetworkServiceImplTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,41 @@ public void testCreateIpv4RoutedNetwork() throws InsufficientCapacityException,
777777
Mockito.verify(routedIpv4Manager).assignIpv4SubnetToNetwork(nullable(Network.class));
778778
}
779779

780+
@Test
781+
public void testCreateVpcTier() throws InsufficientCapacityException, ResourceAllocationException, NoSuchFieldException, IllegalAccessException {
782+
Integer privateMtu = 1200;
783+
Long networkOfferingId = 1L;
784+
Long vpcId = 2L;
785+
786+
ReflectionTestUtils.setField(createNetworkCmd, "name", "testNetwork");
787+
ReflectionTestUtils.setField(createNetworkCmd, "displayText", "Test Network");
788+
ReflectionTestUtils.setField(createNetworkCmd, "networkOfferingId", networkOfferingId);
789+
ReflectionTestUtils.setField(createNetworkCmd, "zoneId", zoneId);
790+
ReflectionTestUtils.setField(createNetworkCmd, "privateMtu", privateMtu);
791+
ReflectionTestUtils.setField(createNetworkCmd, "vpcId", vpcId);
792+
793+
dc = Mockito.mock(DataCenterVO.class);
794+
Mockito.when(dcDao.findById(zoneId)).thenReturn(dc);
795+
Mockito.when(dc.getId()).thenReturn(zoneId);
796+
vpc = Mockito.mock(VpcVO.class);
797+
Mockito.when(vpc.getName()).thenReturn("Vpc 1");
798+
Mockito.when(vpcDao.findById(vpcId)).thenReturn(vpc);
799+
networkOfferingVO = Mockito.mock(NetworkOfferingVO.class);
800+
Mockito.when(networkOfferingDao.findById(networkOfferingId)).thenReturn(networkOfferingVO);
801+
Mockito.when(configMgr.isOfferingForVpc(networkOfferingVO)).thenReturn(true);
802+
803+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrepend, "_defaultValue", "true");
804+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrependDelimiter, "_defaultValue", " -- ");
805+
806+
service.createGuestNetwork(createNetworkCmd);
807+
808+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrepend, "_defaultValue", "false");
809+
810+
Mockito.verify(vpcMgr, times(1)).createVpcGuestNetwork(networkOfferingId, "Vpc 1 -- testNetwork", "Test Network", null, null,
811+
null, null, accountMock, null, phyNet, zoneId, null, null, vpcId, null, accountMock, true,
812+
null, null, null, null, null, null, null, new Pair<>(0, privateMtu), null);
813+
}
814+
780815
@Test
781816
public void testCheckAndUpdateNetworkResetSuccess() {
782817
NetworkVO networkVO = new NetworkVO();

0 commit comments

Comments
 (0)