Skip to content

Commit 4a7ffb7

Browse files
committed
In progress add source NAT
1 parent c135fa1 commit 4a7ffb7

File tree

8 files changed

+36
-10
lines changed

8 files changed

+36
-10
lines changed

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/CreateNsxTier1GatewayCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
public class CreateNsxTier1GatewayCommand extends NsxCommand {
2222
private long vpcId;
2323
private String vpcName;
24+
private boolean sourceNatEnabled;
2425

25-
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName) {
26+
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName, boolean sourceNatEnabled) {
2627
super(domainId, accountId, zoneId);
2728
this.vpcId = vpcId;
2829
this.vpcName = vpcName;
30+
this.sourceNatEnabled = sourceNatEnabled;
2931
}
3032

3133
public long getVpcId() {
@@ -36,6 +38,10 @@ public String getVpcName() {
3638
return vpcName;
3739
}
3840

41+
public boolean isSourceNatEnabled() {
42+
return sourceNatEnabled;
43+
}
44+
3945
@Override
4046
public boolean equals(Object o) {
4147
if (this == o) return true;

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ private Answer executeRequest(ReadyCommand cmd) {
254254

255255
private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
256256
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
257+
boolean sourceNatEnabled = cmd.isSourceNatEnabled();
257258
try {
258-
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster);
259+
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster, sourceNatEnabled);
259260
return new NsxAnswer(cmd, true, "");
260261
} catch (CloudRuntimeException e) {
261262
LOGGER.error(String.format("Cannot create tier 1 gateway %s (VPC: %s): %s", name, cmd.getVpcName(), e.getMessage()));

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxApiClient.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.cloudstack.utils.NsxControllerUtils;
4949
import org.apache.log4j.Logger;
5050

51+
import java.util.ArrayList;
5152
import java.util.List;
5253
import java.util.function.Function;
5354

@@ -185,21 +186,33 @@ private void createTier1LocaleServices(String tier1Id, String edgeCluster, Strin
185186
}
186187
}
187188

188-
public void createTier1Gateway(String name, String tier0Gateway, String edgeCluster) {
189+
private List<String> getRouterAdvertisementTypeList(boolean sourceNatEnabled) {
190+
List<String> types = new ArrayList<>();
191+
types.add(RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT.name());
192+
types.add(RouteAdvertisementType.TIER1_NAT.name());
193+
if (!sourceNatEnabled) {
194+
types.add(RouteAdvertisementType.TIER1_CONNECTED.name());
195+
}
196+
return types;
197+
}
198+
199+
public void createTier1Gateway(String name, String tier0Gateway, String edgeCluster, boolean sourceNatEnabled) {
189200
String tier0GatewayPath = TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway;
190201
Tier1 tier1 = getTier1Gateway(name);
191202
if (tier1 != null) {
192203
throw new InvalidParameterValueException(String.format("VPC network with name %s exists in NSX zone", name));
193204
}
194205

206+
List<String> routeAdvertisementTypes = getRouterAdvertisementTypeList(sourceNatEnabled);
207+
195208
Tier1s tier1service = (Tier1s) nsxService.apply(Tier1s.class);
196209
tier1 = new Tier1.Builder()
197210
.setTier0Path(tier0GatewayPath)
198211
.setResourceType(TIER_1_RESOURCE_TYPE)
199212
.setPoolAllocation(PoolAllocation.ROUTING.name())
200213
.setHaMode(HAMode.ACTIVE_STANDBY.name())
201214
.setFailoverMode(FailoverMode.PREEMPTIVE.name())
202-
.setRouteAdvertisementTypes(List.of(RouteAdvertisementType.TIER1_CONNECTED.name(), RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT.name()))
215+
.setRouteAdvertisementTypes(routeAdvertisementTypes)
203216
.setId(name)
204217
.setDisplayName(name)
205218
.build();

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxElement.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.cloud.network.vpc.PrivateGateway;
5353
import com.cloud.network.vpc.StaticRouteProfile;
5454
import com.cloud.network.vpc.Vpc;
55+
import com.cloud.network.vpc.dao.VpcOfferingServiceMapDao;
5556
import com.cloud.offering.NetworkOffering;
5657
import com.cloud.resource.ResourceManager;
5758
import com.cloud.resource.ResourceStateAdapter;
@@ -101,6 +102,8 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
101102
NetworkModel networkModel;
102103
@Inject
103104
DomainDao domainDao;
105+
@Inject
106+
private VpcOfferingServiceMapDao vpcOfferingServiceMapDao;
104107

105108
private static final Logger LOGGER = Logger.getLogger(NsxElement.class);
106109

@@ -289,7 +292,9 @@ public boolean implementVpc(Vpc vpc, DeployDestination dest, ReservationContext
289292
}
290293
Account account = isNsxAndAccount.second();
291294
DomainVO domain = getDomainFromAccount(account);
292-
return nsxService.createVpcNetwork(vpc.getZoneId(), account.getId(), domain.getId(), vpc.getId(), vpc.getName());
295+
Network.Service[] services = { Network.Service.SourceNat };
296+
boolean sourceNatEnabled = vpcOfferingServiceMapDao.areServicesSupportedByVpcOffering(vpc.getVpcOfferingId(), services);
297+
return nsxService.createVpcNetwork(vpc.getZoneId(), account.getId(), domain.getId(), vpc.getId(), vpc.getName(), sourceNatEnabled);
293298
}
294299

295300
@Override

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class NsxServiceImpl implements NsxService {
3434
@Inject
3535
VpcDao vpcDao;
3636

37-
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, long vpcId, String vpcName) {
37+
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, long vpcId, String vpcName, boolean sourceNatEnabled) {
3838
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
39-
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName);
39+
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName, sourceNatEnabled);
4040
NsxAnswer result = nsxControllerUtils.sendNsxCommand(createNsxTier1GatewayCommand, zoneId);
4141
return result.getResult();
4242
}

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/resource/NsxResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testConfigure_MissingParameter() throws ConfigurationException {
106106
@Test
107107
public void testCreateNsxTier1Gateway() {
108108
NsxCommand command = new CreateNsxTier1GatewayCommand(1L, 2L,
109-
1L, 3L, "VPC01");
109+
1L, 3L, "VPC01", false);
110110

111111
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
112112
assertTrue(answer.getResult());

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxElementTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.List;
4343

4444
import static org.junit.Assert.assertTrue;
45+
import static org.mockito.ArgumentMatchers.anyBoolean;
4546
import static org.mockito.ArgumentMatchers.anyString;
4647
import static org.mockito.ArgumentMatchers.anyLong;
4748
import static org.mockito.Mockito.mock;
@@ -115,7 +116,7 @@ public void setup() {
115116

116117
@Test
117118
public void testImplementVpc() throws ResourceUnavailableException, InsufficientCapacityException {
118-
when(nsxService.createVpcNetwork(anyLong(), anyLong(), anyLong(), anyLong(), anyString())).thenReturn(true);
119+
when(nsxService.createVpcNetwork(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean())).thenReturn(true);
119120

120121
assertTrue(nsxElement.implementVpc(vpc, deployDestination, reservationContext));
121122
}

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxServiceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testCreateVpcNetwork() {
6767
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class), anyLong())).thenReturn(createNsxTier1GatewayAnswer);
6868
when(createNsxTier1GatewayAnswer.getResult()).thenReturn(true);
6969

70-
assertTrue(nsxService.createVpcNetwork(1L, 3L, 2L, 5L, "VPC01"));
70+
assertTrue(nsxService.createVpcNetwork(1L, 3L, 2L, 5L, "VPC01", false));
7171
}
7272

7373
@Test

0 commit comments

Comments
 (0)