Validate if given CIDR belongs to a bigger allocation in Netris before creating the zone-level allocation (#48)

* Validate if given CIDR belongs to a bigger allocation in Netris before creating

* rename method
This commit is contained in:
Pearl Dsilva 2025-02-05 10:37:46 -05:00 committed by GitHub
parent 5289fe4b62
commit 7f31803232
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 1 deletions

View File

@ -18,6 +18,8 @@ package org.apache.cloudstack.service;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import io.netris.ApiClient;
import io.netris.ApiException;
import io.netris.ApiResponse;
@ -815,11 +817,19 @@ public class NetrisApiClientImpl implements NetrisApiClient {
filterByVpc.add(vpc.getId());
IpTree ipamTree = ipamApi.apiV2IpamGet(filterBySites, filterByVpc);
List<IpTreeAllocation> superCidrList = ipamTree.getData().stream()
.filter(x -> x.getPrefix().equals(superCidrPrefix))
.filter(x -> x.getPrefix().equals(superCidrPrefix) || isAllocationPartOfBiggerAllocation(x.getPrefix(), superCidrPrefix))
.collect(Collectors.toList());
return CollectionUtils.isEmpty(superCidrList) ? null : superCidrList.get(0).getId();
}
private boolean isAllocationPartOfBiggerAllocation(String netrisAllocation, String providedAllocation) {
IPAddress biggerAllocation = new IPAddressString(netrisAllocation).getAddress();
IPAddress smallerAllocation = new IPAddressString(providedAllocation).getAddress();
return biggerAllocation.contains(smallerAllocation);
}
private IpTreeSubnet getIpamSubnetByAllocationAndPrefixAndPurposeAndVpc(BigDecimal ipamAllocationId, String exactCidr, IpTreeSubnet.PurposeEnum purpose, VPCListing vpc) throws ApiException {
IpamApi ipamApi = apiClient.getApiStubForMethod(IpamApi.class);
FilterByVpc filterByVpc = new FilterByVpc();