Skip to content

Commit 1124b07

Browse files
authored
Delete IPv6 allocation after tier removal on VPC with dual stack offering (apache#86)
1 parent 1ef3789 commit 1124b07

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

‎plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,32 +1018,30 @@ public void deleteNatRule(String natRuleName, Integer snatRuleId, String netrisV
10181018
}
10191019
}
10201020

1021-
private void deleteVpcIpamAllocationInternal(VPCListing vpcResource, String vpcCidr) {
1022-
logger.debug("Deleting Netris VPC IPAM Allocation {} for VPC {}", vpcCidr, vpcResource.getName());
1021+
private void deleteVpcIpamAllocationInternal(VPCListing vpcResource, String allocationName) {
1022+
logger.debug("Deleting Netris VPC IPAM Allocation {} for VPC {}", allocationName, vpcResource.getName());
10231023
try {
10241024
VpcApi vpcApi = apiClient.getApiStubForMethod(VpcApi.class);
10251025
VPCResponseResourceOK vpcResourcesResponse = vpcApi.apiV2VpcVpcIdResourcesGet(vpcResource.getId());
1026-
VPCResourceIpam vpcAllocationResource = getVpcAllocationResource(vpcResourcesResponse);
1026+
VPCResourceIpam vpcAllocationResource = getVpcAllocationResource(vpcResourcesResponse, allocationName);
10271027
if (Objects.isNull(vpcAllocationResource)) {
1028-
logger.info("No VPC IPAM Allocation found for VPC {}", vpcCidr);
1028+
logger.info("No VPC IPAM Allocation found for VPC {}", allocationName);
10291029
return;
10301030
}
10311031
IpamApi ipamApi = apiClient.getApiStubForMethod(IpamApi.class);
10321032
logger.debug("Removing the IPAM allocation {} with ID {}", vpcAllocationResource.getName(), vpcAllocationResource.getId());
10331033
ipamApi.apiV2IpamTypeIdDelete("allocation", vpcAllocationResource.getId());
10341034
} catch (ApiException e) {
1035-
logAndThrowException(String.format("Error removing IPAM Allocation %s for VPC %s", vpcCidr, vpcResource.getName()), e);
1035+
logAndThrowException(String.format("Error removing IPAM Allocation %s for VPC %s", allocationName, vpcResource.getName()), e);
10361036
}
10371037
}
10381038

1039-
private VPCResourceIpam getVpcAllocationResource(VPCResponseResourceOK vpcResourcesResponse) {
1039+
private VPCResourceIpam getVpcAllocationResource(VPCResponseResourceOK vpcResourcesResponse, String allocationName) {
10401040
VPCResource resource = vpcResourcesResponse.getData().get(0);
10411041
List<VPCResourceIpam> vpcAllocations = resource.getAllocation();
10421042
if (CollectionUtils.isNotEmpty(vpcAllocations)) {
1043-
if (vpcAllocations.size() > 1) {
1044-
logger.warn("Unexpected VPC allocations size {}, one expected", vpcAllocations.size());
1045-
}
1046-
return vpcAllocations.get(0);
1043+
vpcAllocations = vpcAllocations.stream().filter(x -> x.getName().equalsIgnoreCase(allocationName)).collect(Collectors.toList());
1044+
return CollectionUtils.isNotEmpty(vpcAllocations) ? vpcAllocations.get(0) : null;
10471045
}
10481046
return null;
10491047
}
@@ -1087,8 +1085,8 @@ public boolean deleteVpc(DeleteNetrisVpcCommand cmd) {
10871085
deleteNatRule(snatRuleName, existingNatRule.getId(), vpcResource.getName());
10881086
}
10891087

1090-
String vpcCidr = cmd.getCidr();
1091-
deleteVpcIpamAllocationInternal(vpcResource, vpcCidr);
1088+
String vpcAllocationName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_ALLOCATION, cmd.getCidr());
1089+
deleteVpcIpamAllocationInternal(vpcResource, vpcAllocationName);
10921090
VPCResponseObjectOK response = deleteVpcInternal(vpcResource);
10931091
return response != null && response.isIsSuccess();
10941092
}
@@ -1308,8 +1306,10 @@ public boolean deleteVnet(DeleteNetrisVnetCommand cmd) {
13081306
logger.debug("Successfully deleted vNet {}", vNetName);
13091307
deleteSubnetInternal(vpcFilter, netrisVnetName, netrisSubnetName);
13101308
if (Objects.nonNull(vnetV6Cidr)) {
1309+
String netrisV6IpamAllocationName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_ALLOCATION, vnetV6Cidr);
13111310
String netrisV6SubnetName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_SUBNET, String.valueOf(cmd.getVpcId()), vnetV6Cidr);
13121311
deleteSubnetInternal(vpcFilter, netrisVnetName, netrisV6SubnetName);
1312+
deleteVpcIpamAllocationInternal(associatedVpc, netrisV6IpamAllocationName);
13131313
}
13141314

13151315
} catch (Exception e) {

0 commit comments

Comments
 (0)