In progress add source NAT

This commit is contained in:
nvazquez 2023-10-23 14:00:34 -03:00
parent c135fa13ae
commit 4a7ffb70fa
No known key found for this signature in database
GPG Key ID: 656E1BCC8CB54F84
8 changed files with 36 additions and 10 deletions

View File

@ -21,11 +21,13 @@ import java.util.Objects;
public class CreateNsxTier1GatewayCommand extends NsxCommand {
private long vpcId;
private String vpcName;
private boolean sourceNatEnabled;
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName) {
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName, boolean sourceNatEnabled) {
super(domainId, accountId, zoneId);
this.vpcId = vpcId;
this.vpcName = vpcName;
this.sourceNatEnabled = sourceNatEnabled;
}
public long getVpcId() {
@ -36,6 +38,10 @@ public class CreateNsxTier1GatewayCommand extends NsxCommand {
return vpcName;
}
public boolean isSourceNatEnabled() {
return sourceNatEnabled;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -254,8 +254,9 @@ public class NsxResource implements ServerResource {
private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
boolean sourceNatEnabled = cmd.isSourceNatEnabled();
try {
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster);
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster, sourceNatEnabled);
return new NsxAnswer(cmd, true, "");
} catch (CloudRuntimeException e) {
LOGGER.error(String.format("Cannot create tier 1 gateway %s (VPC: %s): %s", name, cmd.getVpcName(), e.getMessage()));

View File

@ -48,6 +48,7 @@ import com.vmware.vapi.std.errors.Error;
import org.apache.cloudstack.utils.NsxControllerUtils;
import org.apache.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
@ -185,13 +186,25 @@ public class NsxApiClient {
}
}
public void createTier1Gateway(String name, String tier0Gateway, String edgeCluster) {
private List<String> getRouterAdvertisementTypeList(boolean sourceNatEnabled) {
List<String> types = new ArrayList<>();
types.add(RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT.name());
types.add(RouteAdvertisementType.TIER1_NAT.name());
if (!sourceNatEnabled) {
types.add(RouteAdvertisementType.TIER1_CONNECTED.name());
}
return types;
}
public void createTier1Gateway(String name, String tier0Gateway, String edgeCluster, boolean sourceNatEnabled) {
String tier0GatewayPath = TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway;
Tier1 tier1 = getTier1Gateway(name);
if (tier1 != null) {
throw new InvalidParameterValueException(String.format("VPC network with name %s exists in NSX zone", name));
}
List<String> routeAdvertisementTypes = getRouterAdvertisementTypeList(sourceNatEnabled);
Tier1s tier1service = (Tier1s) nsxService.apply(Tier1s.class);
tier1 = new Tier1.Builder()
.setTier0Path(tier0GatewayPath)
@ -199,7 +212,7 @@ public class NsxApiClient {
.setPoolAllocation(PoolAllocation.ROUTING.name())
.setHaMode(HAMode.ACTIVE_STANDBY.name())
.setFailoverMode(FailoverMode.PREEMPTIVE.name())
.setRouteAdvertisementTypes(List.of(RouteAdvertisementType.TIER1_CONNECTED.name(), RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT.name()))
.setRouteAdvertisementTypes(routeAdvertisementTypes)
.setId(name)
.setDisplayName(name)
.build();

View File

@ -52,6 +52,7 @@ import com.cloud.network.vpc.NetworkACLItem;
import com.cloud.network.vpc.PrivateGateway;
import com.cloud.network.vpc.StaticRouteProfile;
import com.cloud.network.vpc.Vpc;
import com.cloud.network.vpc.dao.VpcOfferingServiceMapDao;
import com.cloud.offering.NetworkOffering;
import com.cloud.resource.ResourceManager;
import com.cloud.resource.ResourceStateAdapter;
@ -101,6 +102,8 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
NetworkModel networkModel;
@Inject
DomainDao domainDao;
@Inject
private VpcOfferingServiceMapDao vpcOfferingServiceMapDao;
private static final Logger LOGGER = Logger.getLogger(NsxElement.class);
@ -289,7 +292,9 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
}
Account account = isNsxAndAccount.second();
DomainVO domain = getDomainFromAccount(account);
return nsxService.createVpcNetwork(vpc.getZoneId(), account.getId(), domain.getId(), vpc.getId(), vpc.getName());
Network.Service[] services = { Network.Service.SourceNat };
boolean sourceNatEnabled = vpcOfferingServiceMapDao.areServicesSupportedByVpcOffering(vpc.getVpcOfferingId(), services);
return nsxService.createVpcNetwork(vpc.getZoneId(), account.getId(), domain.getId(), vpc.getId(), vpc.getName(), sourceNatEnabled);
}
@Override

View File

@ -34,9 +34,9 @@ public class NsxServiceImpl implements NsxService {
@Inject
VpcDao vpcDao;
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, long vpcId, String vpcName) {
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, long vpcId, String vpcName, boolean sourceNatEnabled) {
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName);
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName, sourceNatEnabled);
NsxAnswer result = nsxControllerUtils.sendNsxCommand(createNsxTier1GatewayCommand, zoneId);
return result.getResult();
}

View File

@ -106,7 +106,7 @@ public class NsxResourceTest {
@Test
public void testCreateNsxTier1Gateway() {
NsxCommand command = new CreateNsxTier1GatewayCommand(1L, 2L,
1L, 3L, "VPC01");
1L, 3L, "VPC01", false);
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
assertTrue(answer.getResult());

View File

@ -42,6 +42,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.List;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
@ -115,7 +116,7 @@ public class NsxElementTest {
@Test
public void testImplementVpc() throws ResourceUnavailableException, InsufficientCapacityException {
when(nsxService.createVpcNetwork(anyLong(), anyLong(), anyLong(), anyLong(), anyString())).thenReturn(true);
when(nsxService.createVpcNetwork(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean())).thenReturn(true);
assertTrue(nsxElement.implementVpc(vpc, deployDestination, reservationContext));
}

View File

@ -67,7 +67,7 @@ public class NsxServiceImplTest {
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class), anyLong())).thenReturn(createNsxTier1GatewayAnswer);
when(createNsxTier1GatewayAnswer.getResult()).thenReturn(true);
assertTrue(nsxService.createVpcNetwork(1L, 3L, 2L, 5L, "VPC01"));
assertTrue(nsxService.createVpcNetwork(1L, 3L, 2L, 5L, "VPC01", false));
}
@Test