Skip to content

Commit c584a13

Browse files
abh1sarDaanHooglandlucas-a-martinsLucas Martins
authored andcommitted
Prepend VPC name to VPC network tier name (apache#9780)
* Fix `updateTemplatePermission` when the UI is set to a language other than English (apache#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 dc483bc commit c584a13

File tree

4 files changed

+72
-78
lines changed

4 files changed

+72
-78
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")
@@ -1793,6 +1798,13 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
17931798

17941799
checkNetworkDns(ipv6, ntwkOff, vpcId, ip4Dns1, ip4Dns2, ip6Dns1, ip6Dns2);
17951800

1801+
if (vpcId != null && VpcManager.VpcTierNamePrepend.value()) {
1802+
Vpc vpc = _vpcDao.findById(vpcId);
1803+
if (vpc != null) {
1804+
name = getVpcPrependedNetworkName(name, vpc);
1805+
}
1806+
}
1807+
17961808
Network network = commitNetwork(networkOfferingId, gateway, startIP, endIP, netmask, networkDomain, vlanId, bypassVlanOverlapCheck, name, displayText, caller, physicalNetworkId, zone.getId(),
17971809
domainId, isDomainSpecific, subdomainAccess, vpcId, startIPv6, endIPv6, ip6Gateway, ip6Cidr, displayNetwork, aclId, secondaryVlanId, privateVlanType, ntwkOff, pNtwk, aclType, owner, cidr, createVlan,
17981810
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";
@@ -3133,6 +3135,19 @@ protected void markStaticRouteForRevoke(final StaticRouteVO route, final Account
31333135
}
31343136
}
31353137

3138+
@Override
3139+
public String getConfigComponentName() {
3140+
return VpcManager.class.getSimpleName();
3141+
}
3142+
3143+
@Override
3144+
public ConfigKey<?>[] getConfigKeys() {
3145+
return new ConfigKey<?>[]{
3146+
VpcTierNamePrepend,
3147+
VpcTierNamePrependDelimiter
3148+
};
3149+
}
3150+
31363151
protected class VpcCleanupTask extends ManagedContextRunnable {
31373152
@Override
31383153
protected void runInContext() {

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

Lines changed: 26 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -782,89 +782,38 @@ public void testCreateIpv4RoutedNetwork() {
782782
}
783783

784784
@Test
785-
public void testCreateIpv4RoutedNetworkWithBgpPeersFailure1() {
786-
registerCallContext();
787-
CreateNetworkCmdByAdmin cmd = Mockito.mock(CreateNetworkCmdByAdmin.class);
788-
Mockito.when(cmd.getCidrSize()).thenReturn(24);
789-
List<Long> bgpPeerIds = Arrays.asList(11L, 12L);
790-
Mockito.when(cmd.getBgpPeerIds()).thenReturn(bgpPeerIds);
791-
792-
prepareCreateNetworkDnsMocks(cmd, Network.GuestType.Isolated, false, true, true);
793-
when(networkOfferingVO.getNetworkMode()).thenReturn(NetworkOffering.NetworkMode.ROUTED);
794-
when(networkOfferingVO.getRoutingMode()).thenReturn(NetworkOffering.RoutingMode.Static);
795-
when(routedIpv4Manager.isRoutedNetworkVpcEnabled(nullable(Long.class))).thenReturn(true);
796-
when(routedIpv4Manager.isVirtualRouterGateway(networkOfferingVO)).thenReturn(true);
797-
798-
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
799-
when(cmd.getZoneId()).thenReturn(zoneId);
800-
when(dcDao.findById(zoneId)).thenReturn(zone);
801-
when(zone.getId()).thenReturn(zoneId);
802-
803-
try {
804-
service.createGuestNetwork(cmd);
805-
} catch (InsufficientCapacityException | ResourceAllocationException e) {
806-
Assert.fail(String.format("failure with exception: %s", e.getMessage()));
807-
} catch (InvalidParameterValueException ex) {
808-
Assert.assertEquals("The BGP peers of VPC tiers will inherit from the VPC, do not add separately.", ex.getMessage());
809-
}
810-
}
811-
812-
@Test
813-
public void testCreateIpv4RoutedNetworkWithBgpPeersFailure2() {
814-
registerCallContext();
815-
CreateNetworkCmdByAdmin cmd = Mockito.mock(CreateNetworkCmdByAdmin.class);
816-
Mockito.when(cmd.getCidrSize()).thenReturn(24);
817-
List<Long> bgpPeerIds = Arrays.asList(11L, 12L);
818-
Mockito.when(cmd.getBgpPeerIds()).thenReturn(bgpPeerIds);
819-
820-
prepareCreateNetworkDnsMocks(cmd, Network.GuestType.Isolated, false, false, true);
821-
when(networkOfferingVO.getNetworkMode()).thenReturn(NetworkOffering.NetworkMode.ROUTED);
822-
when(networkOfferingVO.getRoutingMode()).thenReturn(NetworkOffering.RoutingMode.Static);
823-
when(routedIpv4Manager.isRoutedNetworkVpcEnabled(nullable(Long.class))).thenReturn(true);
824-
when(routedIpv4Manager.isVirtualRouterGateway(networkOfferingVO)).thenReturn(true);
785+
public void testCreateVpcTier() throws InsufficientCapacityException, ResourceAllocationException, NoSuchFieldException, IllegalAccessException {
786+
Integer privateMtu = 1200;
787+
Long networkOfferingId = 1L;
788+
Long vpcId = 2L;
825789

826-
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
827-
when(cmd.getZoneId()).thenReturn(zoneId);
828-
when(dcDao.findById(zoneId)).thenReturn(zone);
829-
when(zone.getId()).thenReturn(zoneId);
790+
ReflectionTestUtils.setField(createNetworkCmd, "name", "testNetwork");
791+
ReflectionTestUtils.setField(createNetworkCmd, "displayText", "Test Network");
792+
ReflectionTestUtils.setField(createNetworkCmd, "networkOfferingId", networkOfferingId);
793+
ReflectionTestUtils.setField(createNetworkCmd, "zoneId", zoneId);
794+
ReflectionTestUtils.setField(createNetworkCmd, "privateMtu", privateMtu);
795+
ReflectionTestUtils.setField(createNetworkCmd, "vpcId", vpcId);
830796

831-
try {
832-
service.createGuestNetwork(cmd);
833-
} catch (InsufficientCapacityException | ResourceAllocationException e) {
834-
Assert.fail(String.format("failure with exception: %s", e.getMessage()));
835-
} catch (InvalidParameterValueException ex) {
836-
Assert.assertEquals("The network offering does not support Dynamic routing", ex.getMessage());
837-
}
838-
}
797+
dc = Mockito.mock(DataCenterVO.class);
798+
Mockito.when(dcDao.findById(zoneId)).thenReturn(dc);
799+
Mockito.when(dc.getId()).thenReturn(zoneId);
800+
vpc = Mockito.mock(VpcVO.class);
801+
Mockito.when(vpc.getName()).thenReturn("Vpc 1");
802+
Mockito.when(vpcDao.findById(vpcId)).thenReturn(vpc);
803+
networkOfferingVO = Mockito.mock(NetworkOfferingVO.class);
804+
Mockito.when(networkOfferingDao.findById(networkOfferingId)).thenReturn(networkOfferingVO);
805+
Mockito.when(configMgr.isOfferingForVpc(networkOfferingVO)).thenReturn(true);
839806

840-
@Test
841-
public void testCreateIpv4RoutedNetworkWithBgpPeersFailure3() {
842-
registerCallContext();
843-
CreateNetworkCmdByAdmin cmd = Mockito.mock(CreateNetworkCmdByAdmin.class);
844-
Mockito.when(cmd.getCidrSize()).thenReturn(24);
845-
List<Long> bgpPeerIds = Arrays.asList(11L, 12L);
846-
Mockito.when(cmd.getBgpPeerIds()).thenReturn(bgpPeerIds);
807+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrepend, "_defaultValue", "true");
808+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrependDelimiter, "_defaultValue", " -- ");
847809

848-
prepareCreateNetworkDnsMocks(cmd, Network.GuestType.Isolated, false, false, true);
849-
when(networkOfferingVO.getNetworkMode()).thenReturn(NetworkOffering.NetworkMode.ROUTED);
850-
when(networkOfferingVO.getRoutingMode()).thenReturn(NetworkOffering.RoutingMode.Static);
851-
when(routedIpv4Manager.isRoutedNetworkVpcEnabled(nullable(Long.class))).thenReturn(true);
852-
when(routedIpv4Manager.isVirtualRouterGateway(networkOfferingVO)).thenReturn(true);
853-
when(routedIpv4Manager.isDynamicRoutedNetwork(networkOfferingVO)).thenReturn(true);
854-
doThrow(new InvalidParameterValueException("validation error")).when(routedIpv4Manager).validateBgpPeers(nullable(Account.class), nullable(Long.class), any(List.class));
810+
service.createGuestNetwork(createNetworkCmd);
855811

856-
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
857-
when(cmd.getZoneId()).thenReturn(zoneId);
858-
when(dcDao.findById(zoneId)).thenReturn(zone);
859-
when(zone.getId()).thenReturn(zoneId);
812+
overrideDefaultConfigValue(VpcManager.VpcTierNamePrepend, "_defaultValue", "false");
860813

861-
try {
862-
service.createGuestNetwork(cmd);
863-
} catch (InsufficientCapacityException | ResourceAllocationException e) {
864-
Assert.fail(String.format("failure with exception: %s", e.getMessage()));
865-
} catch (InvalidParameterValueException ex) {
866-
Assert.assertEquals("validation error", ex.getMessage());
867-
}
814+
Mockito.verify(vpcMgr, times(1)).createVpcGuestNetwork(networkOfferingId, "Vpc 1 -- testNetwork", "Test Network", null, null,
815+
null, null, accountMock, null, phyNet, zoneId, null, null, vpcId, null, accountMock, true,
816+
null, null, null, null, null, null, null, new Pair<>(0, privateMtu), null);
868817
}
869818

870819
@Test

0 commit comments

Comments
 (0)