This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch nsx-isolated-source-nat
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit b89085ef11eb1abdd8c51a8156a0c6d8a58d3d31
Author: Pearl Dsilva <[email protected]>
AuthorDate: Fri Nov 3 14:10:47 2023 -0400

    NSX: Support Source NAT on NSX Isolated networks
---
 .../cloudstack/service/NsxGuestNetworkGuru.java    | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git 
a/plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java
 
b/plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java
index d507d9199a7..e7efe9190c2 100644
--- 
a/plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java
+++ 
b/plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java
@@ -28,10 +28,12 @@ import 
com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.network.NetworkMigrationResponder;
+import com.cloud.network.NetworkModel;
 import com.cloud.network.NetworkProfile;
 import com.cloud.network.Network;
 import com.cloud.network.Networks;
 import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.PublicIpAddress;
 import com.cloud.network.dao.NetworkVO;
 import com.cloud.network.dao.PhysicalNetworkVO;
 import com.cloud.network.guru.GuestNetworkGuru;
@@ -50,6 +52,7 @@ import org.apache.cloudstack.NsxAnswer;
 import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
 import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
 import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
+import org.apache.cloudstack.agent.api.CreateNsxTier1NatRuleCommand;
 import org.apache.cloudstack.utils.NsxControllerUtils;
 
 import org.apache.cloudstack.utils.NsxHelper;
@@ -70,6 +73,8 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru 
implements NetworkMigr
     AccountDao accountDao;
     @Inject
     DomainDao domainDao;
+    @Inject
+    NetworkModel networkModel;
 
     public NsxGuestNetworkGuru() {
         super();
@@ -218,6 +223,25 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru 
implements NetworkMigr
                 throw new CloudRuntimeException(msg);
             }
 
+            if (isNull(network.getVpcId())) {
+                long domainId = domain.getId();
+                long accountId = account.getId();
+                long dataCenterId = zone.getId();
+                long resourceId = network.getId();
+                PublicIpAddress ipAddress = 
networkModel.getSourceNatIpAddressForGuestNetwork(account, network);
+                String translatedIp = ipAddress.getAddress().addr();
+                String tier1GatewayName = 
NsxControllerUtils.getTier1GatewayName(domainId, accountId, dataCenterId, 
resourceId, false);
+                LOGGER.debug(String.format("Creating NSX NAT Rule for Tier1 GW 
%s for translated IP %s for Isolated network %s", tier1GatewayName, 
translatedIp, network.getName()));
+                String natRuleId = 
NsxControllerUtils.getNsxNatRuleId(domainId, accountId, dataCenterId, 
resourceId, false);
+                CreateNsxTier1NatRuleCommand cmd = 
NsxHelper.createNsxNatRuleCommand(domainId, accountId, dataCenterId, 
tier1GatewayName, "SNAT", translatedIp, natRuleId);
+                NsxAnswer nsxAnswer = nsxControllerUtils.sendNsxCommand(cmd, 
dataCenterId);
+                if (!nsxAnswer.getResult()) {
+                    String msg = String.format("Could not create NSX NAT Rule 
on Tier1 Gateway %s for IP %s  for Isolated network %s", tier1GatewayName, 
translatedIp, network.getName());
+                    LOGGER.error(msg);
+                    throw new CloudRuntimeException(msg);
+                }
+            }
+
             // Create the DHCP relay config for the segment
             String iPv4Address = nicProfile.getIPv4Address();
             List<String> addresses = List.of(iPv4Address);

Reply via email to