mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): paginate instance type offerings (#7861)
This commit is contained in:
1 parent
184f99490b
commit
fb4d82548c
3 files changed
+276
-10
No files matched your search
@@ -5165,20 +5165,25 @@ public class Ec2QueryHandler {
|
||||
private Response handleDescribeInstanceTypeOfferings(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> typeNames = getList(p, "InstanceType");
|
||||
List<Map.Entry<String, List<String>>> filters = getFiltersList(p);
|
||||
List<Map<String, String>> offerings = service.describeInstanceTypeOfferings(
|
||||
region, typeNames, p.getFirst("LocationType"), filters);
|
||||
Integer maxResults = p.getFirst("MaxResults") == null ? null : parseIntParam(p, "MaxResults", 0);
|
||||
PaginatedResult<Map<String, String>> result = service.describeInstanceTypeOfferings(
|
||||
region, typeNames, p.getFirst("LocationType"), filters, maxResults, p.getFirst("NextToken"));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeInstanceTypeOfferingsResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.start("instanceTypeOfferingSet");
|
||||
for (Map<String, String> offering : offerings) {
|
||||
for (Map<String, String> offering : result.items()) {
|
||||
xml.start("item")
|
||||
.elem("instanceType", offering.get("instanceType"))
|
||||
.elem("locationType", offering.get("locationType"))
|
||||
.elem("location", offering.get("location"))
|
||||
.end("item");
|
||||
}
|
||||
xml.end("instanceTypeOfferingSet").end("DescribeInstanceTypeOfferingsResponse");
|
||||
xml.end("instanceTypeOfferingSet");
|
||||
if (result.nextToken() != null) {
|
||||
xml.elem("nextToken", result.nextToken());
|
||||
}
|
||||
xml.end("DescribeInstanceTypeOfferingsResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package io.floci.aws.services.ec2;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AvailabilityZoneIds;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
@@ -11,6 +13,7 @@ import io.floci.aws.core.common.AwsPartitions;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.CidrCanonicalizer;
|
||||
import io.floci.aws.core.common.ContainerTeardown;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
@@ -13852,15 +13855,21 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<Map<String, String>> describeInstanceTypeOfferings(String region, List<String> instanceTypeNames,
|
||||
public PaginatedResult<Map<String, String>> describeInstanceTypeOfferings(String region, List<String> instanceTypeNames,
|
||||
String locationType,
|
||||
Map<String, List<String>> filters) {
|
||||
return describeInstanceTypeOfferings(region, instanceTypeNames, locationType, filterList(filters));
|
||||
Map<String, List<String>> filters,
|
||||
Integer maxResults, String nextToken) {
|
||||
return describeInstanceTypeOfferings(region, instanceTypeNames, locationType, filterList(filters),
|
||||
maxResults, nextToken);
|
||||
}
|
||||
|
||||
List<Map<String, String>> describeInstanceTypeOfferings(String region, List<String> instanceTypeNames,
|
||||
PaginatedResult<Map<String, String>> describeInstanceTypeOfferings(String region, List<String> instanceTypeNames,
|
||||
String locationType,
|
||||
List<Map.Entry<String, List<String>>> filters) {
|
||||
List<Map.Entry<String, List<String>>> filters,
|
||||
Integer maxResults, String nextToken) {
|
||||
if (maxResults != null && (maxResults < 5 || maxResults > 1000)) {
|
||||
throw new AwsException("InvalidMaxResults", "MaxResults must be between 5 and 1000", 400);
|
||||
}
|
||||
// The first instance-type filter names the types to look up beside InstanceType.N, as a
|
||||
// lone one always has, or the whole catalog when one of its values holds a * or ?
|
||||
// wildcard. Every filter must then match a looked-up type, so the answer is InstanceType.N
|
||||
@@ -13905,7 +13914,31 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
offerings.add(offering);
|
||||
}
|
||||
}
|
||||
return offerings;
|
||||
ArrayNode scope = JsonNodeFactory.instance.arrayNode().add(region).add(effectiveLocationType);
|
||||
ArrayNode names = scope.addArray();
|
||||
for (String name : instanceTypeNames) {
|
||||
names.add(name);
|
||||
}
|
||||
ArrayNode filterScope = scope.addArray();
|
||||
if (filters != null) {
|
||||
for (Map.Entry<String, List<String>> filter : filters) {
|
||||
ArrayNode entry = filterScope.addArray().add(filter.getKey());
|
||||
ArrayNode values = entry.addArray();
|
||||
for (String value : filter.getValue()) {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
String namespace = "ec2-instance-type-offerings:" + Digests.sha256Hex(scope.toString());
|
||||
return Pagination.paginate(offerings,
|
||||
offering -> offering.get("instanceType") + ":" + offering.get("locationType")
|
||||
+ ":" + offering.get("location"),
|
||||
maxResults == null ? Integer.MAX_VALUE : maxResults, nextToken, namespace,
|
||||
token -> Pagination.tokenStartsWith(token, "ec2-instance-type-offerings:")
|
||||
? new AwsException("InvalidPaginationToken.DataMismatch",
|
||||
"The supplied request parameters do not match the the original request parameters "
|
||||
+ "that are being paginated over.", 400)
|
||||
: new AwsException("InvalidParameterValue", "Invalid NextToken", 400));
|
||||
}
|
||||
|
||||
// ─── Filter matching ───────────────────────────────────────────────────────
|
||||
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
package io.floci.aws.services.ec2;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.DescribeInstanceTypeOfferingsRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.DescribeInstanceTypeOfferingsResponse;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.ec2.model.InstanceTypeOffering;
|
||||
import software.amazon.awssdk.services.ec2.model.LocationType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.floci.aws.testing.Ec2Requests.ec2;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class Ec2InstanceTypeOfferingsPaginationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void maxResultsLimitsPagesAndContinuationReturnsEveryOfferingOnce() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
DescribeInstanceTypeOfferingsResponse page = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(5));
|
||||
assertEquals(5, page.instanceTypeOfferings().size(), "MaxResults must limit the first page");
|
||||
assertTrue(page.nextToken() != null && !page.nextToken().isBlank(),
|
||||
"A partial result must include NextToken");
|
||||
}
|
||||
assertCompleteTraversal(DescribeInstanceTypeOfferingsRequest.builder()
|
||||
.locationType(LocationType.REGION).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleTypeFilterReturnsCompleteResultWithoutContinuation() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
DescribeInstanceTypeOfferingsResponse response = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(5)
|
||||
.filters(f -> f.name("instance-type").values("t3.micro")));
|
||||
|
||||
assertEquals(1, response.instanceTypeOfferings().size());
|
||||
assertEquals("t3.micro", response.instanceTypeOfferings().getFirst().instanceTypeAsString());
|
||||
assertTrue(response.nextToken() == null || response.nextToken().isBlank(),
|
||||
"A complete filtered result must not include NextToken");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void availabilityZonePagesKeepDistinctLocationsForTheSameInstanceType() {
|
||||
assertCompleteTraversal(DescribeInstanceTypeOfferingsRequest.builder()
|
||||
.locationType(LocationType.AVAILABILITY_ZONE).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filtersApplyBeforePaginationOnEveryPage() {
|
||||
assertCompleteTraversal(DescribeInstanceTypeOfferingsRequest.builder()
|
||||
.locationType(LocationType.AVAILABILITY_ZONE)
|
||||
.filters(f -> f.name("instance-type").values("t3.*"),
|
||||
f -> f.name("location").values("us-east-1a", "us-east-1b")).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyFilteredResultHasNoContinuation() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
DescribeInstanceTypeOfferingsResponse response = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(5)
|
||||
.filters(f -> f.name("location").values("eu-west-1")));
|
||||
assertTrue(response.instanceTypeOfferings().isEmpty());
|
||||
assertTrue(response.nextToken() == null || response.nextToken().isBlank());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumPageSizeReturnsCompleteCatalogue() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
List<InstanceTypeOffering> expected = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION)).instanceTypeOfferings();
|
||||
DescribeInstanceTypeOfferingsResponse response = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(1000));
|
||||
assertEquals(new HashSet<>(expected), new HashSet<>(response.instanceTypeOfferings()));
|
||||
assertTrue(response.nextToken() == null || response.nextToken().isBlank());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, 0, 4, 1001})
|
||||
void invalidPageSizeReturnsAwsError(int maxResults) {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(maxResults)));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("InvalidMaxResults", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedTokenReturnsAwsError() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(5).nextToken("not-a-valid-token!")));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("InvalidParameterValue", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuationWithoutPageSizeReturnsRemainingOfferings() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
List<InstanceTypeOffering> expected = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION)).instanceTypeOfferings();
|
||||
DescribeInstanceTypeOfferingsResponse first = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).maxResults(5));
|
||||
assertTrue(first.nextToken() != null && !first.nextToken().isBlank());
|
||||
DescribeInstanceTypeOfferingsResponse rest = ec2.describeInstanceTypeOfferings(r -> r
|
||||
.locationType(LocationType.REGION).nextToken(first.nextToken()));
|
||||
List<InstanceTypeOffering> collected = new ArrayList<>(first.instanceTypeOfferings());
|
||||
collected.addAll(rest.instanceTypeOfferings());
|
||||
assertEquals(expected.size(), collected.size());
|
||||
assertEquals(new HashSet<>(expected), new HashSet<>(collected));
|
||||
assertTrue(rest.nextToken() == null || rest.nextToken().isBlank());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"add-filter", "remove-filter", "change-filter", "add-location-filter",
|
||||
"change-location-type", "change-region"})
|
||||
void changedSelectionRejectsContinuation(String change) {
|
||||
DescribeInstanceTypeOfferingsRequest.Builder original = DescribeInstanceTypeOfferingsRequest.builder()
|
||||
.locationType(LocationType.REGION).maxResults(5);
|
||||
if (change.equals("remove-filter") || change.equals("change-filter")) {
|
||||
original.filters(f -> f.name("instance-type").values("*"));
|
||||
}
|
||||
try (Ec2Client source = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1);
|
||||
Ec2Client destination = AwsTestClients.create(Ec2Client.builder(), "test",
|
||||
change.equals("change-region") ? Region.EU_WEST_1 : Region.US_EAST_1)) {
|
||||
DescribeInstanceTypeOfferingsResponse first = source.describeInstanceTypeOfferings(original.build());
|
||||
assertTrue(first.nextToken() != null && !first.nextToken().isBlank());
|
||||
DescribeInstanceTypeOfferingsRequest.Builder continuation = original.nextToken(first.nextToken());
|
||||
DescribeInstanceTypeOfferingsRequest changed = switch (change) {
|
||||
case "add-filter" -> continuation.filters(f -> f.name("instance-type").values("t3.*")).build();
|
||||
case "remove-filter" -> continuation.filters(List.of()).build();
|
||||
case "change-filter" -> continuation.filters(f -> f.name("instance-type").values("t4g.*")).build();
|
||||
case "add-location-filter" -> continuation.filters(f -> f.name("location").values("us-east-1")).build();
|
||||
case "change-location-type" -> continuation.locationType(LocationType.AVAILABILITY_ZONE).build();
|
||||
case "change-region" -> continuation.build();
|
||||
default -> throw new IllegalArgumentException(change);
|
||||
};
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class,
|
||||
() -> destination.describeInstanceTypeOfferings(changed));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertEquals("InvalidPaginationToken.DataMismatch", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuationAllowsChangingPageSize() {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
DescribeInstanceTypeOfferingsRequest request = DescribeInstanceTypeOfferingsRequest.builder()
|
||||
.locationType(LocationType.REGION).maxResults(5).build();
|
||||
DescribeInstanceTypeOfferingsResponse first = ec2.describeInstanceTypeOfferings(request);
|
||||
assertTrue(first.nextToken() != null && !first.nextToken().isBlank());
|
||||
DescribeInstanceTypeOfferingsRequest continuation = request.toBuilder().nextToken(first.nextToken()).build();
|
||||
List<InstanceTypeOffering> expected = ec2.describeInstanceTypeOfferings(continuation).instanceTypeOfferings();
|
||||
List<InstanceTypeOffering> largerPage = ec2.describeInstanceTypeOfferings(continuation.toBuilder()
|
||||
.maxResults(10).build()).instanceTypeOfferings();
|
||||
assertEquals(10, largerPage.size());
|
||||
assertEquals(expected, largerPage.subList(0, 5));
|
||||
assertTrue(first.instanceTypeOfferings().stream().noneMatch(largerPage::contains));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedExplicitInstanceTypesRejectContinuation() {
|
||||
String token = ec2("DescribeInstanceTypeOfferings", "test", "us-east-1")
|
||||
.formParam("LocationType", "region").formParam("MaxResults", 5)
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("DescribeInstanceTypeOfferingsResponse.nextToken");
|
||||
assertTrue(token != null && !token.isBlank());
|
||||
ec2("DescribeInstanceTypeOfferings", "test", "us-east-1")
|
||||
.formParam("LocationType", "region").formParam("MaxResults", 5)
|
||||
.formParam("NextToken", token).formParam("InstanceType.1", "t3.micro")
|
||||
.post("/").then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidPaginationToken.DataMismatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonIntegerPageSizeReturnsXmlError() {
|
||||
ec2("DescribeInstanceTypeOfferings", "test", "us-east-1")
|
||||
.formParam("MaxResults", "five")
|
||||
.post("/").then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidMaxResults"));
|
||||
}
|
||||
|
||||
private void assertCompleteTraversal(DescribeInstanceTypeOfferingsRequest request) {
|
||||
try (Ec2Client ec2 = AwsTestClients.create(Ec2Client.builder(), "test", Region.US_EAST_1)) {
|
||||
List<InstanceTypeOffering> expected = ec2.describeInstanceTypeOfferings(request).instanceTypeOfferings();
|
||||
assertTrue(expected.size() > 5, "The filtered catalogue must contain more than one page");
|
||||
List<InstanceTypeOffering> collected = new ArrayList<>();
|
||||
Set<String> tokens = new HashSet<>();
|
||||
String token = null;
|
||||
do {
|
||||
DescribeInstanceTypeOfferingsResponse page = ec2.describeInstanceTypeOfferings(request.toBuilder()
|
||||
.maxResults(5).nextToken(token).build());
|
||||
assertFalse(page.instanceTypeOfferings().isEmpty());
|
||||
assertTrue(page.instanceTypeOfferings().size() <= 5);
|
||||
assertTrue(expected.containsAll(page.instanceTypeOfferings()), "Every page must satisfy the filters");
|
||||
collected.addAll(page.instanceTypeOfferings());
|
||||
assertTrue(collected.size() <= expected.size());
|
||||
token = page.nextToken();
|
||||
if (token != null && !token.isBlank()) {
|
||||
assertTrue(tokens.add(token), "Continuation tokens must advance");
|
||||
}
|
||||
} while (token != null && !token.isBlank());
|
||||
assertEquals(expected.size(), collected.size());
|
||||
assertEquals(new HashSet<>(expected), new HashSet<>(collected));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user