Fix naming convention for NAT subnets to follow other resources (#47)

* Fix naming convention for NAT subnets to follow other resources

* Use vpc ID for nat subnets

* Use new nat subnet name for deletion of static nat rule

* fix naming convevntion for nat subnet
This commit is contained in:
Pearl Dsilva 2025-02-05 09:29:30 -05:00 committed by GitHub
parent ada3200a05
commit 427d7328c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -19,6 +19,8 @@ package org.apache.cloudstack.resource;
import org.apache.cloudstack.agent.api.NetrisCommand;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Objects;
public class NetrisResourceObjectUtils {
public enum NetrisObjectType {
@ -57,7 +59,12 @@ public class NetrisResourceObjectUtils {
break;
case IPAM_SUBNET:
if (!isZoneLevel) {
stringBuilder.append(String.format("-N%s", objectId));
if (Objects.nonNull(objectId) && Objects.nonNull(objectName)) {
stringBuilder.append(String.format("-N%s", objectId));
} else {
stringBuilder.append(String.format("-V%s-%s", suffixes[0], suffixes[1]));
return stringBuilder.toString();
}
}
break;
case SNAT:

View File

@ -299,7 +299,9 @@ public class NetrisApiClientImpl implements NetrisApiClient {
if (ruleExists) {
deleteNatRule(natRuleName, existingNatRule.getId(), vpcResource.getName());
if (cmd.getNatRuleType().equals("STATICNAT")) {
deleteNatSubnet(vpcResource.getId(), cmd.getNatIp());
String natIp = cmd.getNatIp();
String netrisSubnetName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_SUBNET, String.valueOf(cmd.getVpcId()), natIp);
deleteNatSubnet(netrisSubnetName, vpcResource.getId(), natIp);
}
}
} catch (Exception e) {
@ -893,7 +895,7 @@ public class NetrisApiClientImpl implements NetrisApiClient {
if (StringUtils.isNotBlank(targetIpSubnet) && existsDestinationSubnet(targetIpSubnet)) {
logger.debug(String.format("Creating subnet with NAT purpose for %s", targetIpSubnet));
createNatSubnet(targetIpSubnet, vpcResource.getId());
createNatSubnet(cmd, targetIpSubnet, vpcResource.getId());
}
NatGetBody existingNatRule = netrisNatRuleExists(ruleName);
@ -959,7 +961,7 @@ public class NetrisApiClientImpl implements NetrisApiClient {
return false;
}
// Create a /32 subnet for the DNAT IP
createNatSubnet(natIP, vpcResource.getId());
createNatSubnet(cmd, natIP, vpcResource.getId());
NatApi natApi = apiClient.getApiStubForMethod(NatApi.class);
NatPostBody natBody = new NatPostBody();
natBody.setAction(NatPostBody.ActionEnum.DNAT);
@ -993,15 +995,15 @@ public class NetrisApiClientImpl implements NetrisApiClient {
return true;
}
private void createNatSubnet(String natIp, Integer netrisVpcId) {
private void createNatSubnet(NetrisCommand cmd, String natIp, Integer netrisVpcId) {
try {
FilterByVpc vpcFilter = new FilterByVpc();
vpcFilter.add(netrisVpcId);
String netrisSubnetName = natIp;
String netrisSubnetName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_SUBNET, String.valueOf(((CreateOrUpdateNetrisNatCommand)cmd).getVpcId()), natIp);
List<IpTreeSubnet> matchedSubnets = getSubnet(vpcFilter, netrisSubnetName);
if (matchedSubnets.isEmpty()) {
VPCListing systemVpc = getSystemVpc();
createIpamSubnetInternal(natIp, natIp, SubnetBody.PurposeEnum.NAT, systemVpc, null);
createIpamSubnetInternal(netrisSubnetName, natIp, SubnetBody.PurposeEnum.NAT, systemVpc, null);
return;
}
logger.debug("NAT subnet: {} already exists", natIp);
@ -1344,10 +1346,9 @@ public class NetrisApiClientImpl implements NetrisApiClient {
return NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.VPC, suffix);
}
private void deleteNatSubnet(Integer netrisVpcId, String natIp) {
private void deleteNatSubnet(String netrisSubnetName, Integer netrisVpcId, String natIp) {
FilterByVpc vpcFilter = new FilterByVpc();
vpcFilter.add(netrisVpcId);
String netrisSubnetName = natIp + "/32";
deleteSubnetInternal(vpcFilter, null, netrisSubnetName);
}
}