mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): paginate base path mappings (#9430)
This commit is contained in:
1 parent
c9a4443f50
commit
ffc3bdffa1
4 files changed
+206
-6
No files matched your search
@@ -195,6 +195,15 @@ Tagged REST APIs, stages, API keys, usage plans and custom domain names are disc
|
||||
[Resource Groups Tagging API](resource-groups-tagging.md), whose `TagResources` and `UntagResources`
|
||||
write REST API, stage, API key, usage plan and custom domain name tags through to API Gateway.
|
||||
|
||||
### Base Path Mapping Pagination
|
||||
|
||||
`GetBasePathMappings` accepts `limit` and `position`, returns 25 mappings by default,
|
||||
and caps pages at 500. A nonterminal page includes an opaque `position`; the final
|
||||
page omits it. Positions belong to the caller's account, Region and domain. Paging
|
||||
uses each record's stored base path, so legacy root records that report the same
|
||||
`basePath` remain distinct. Looking up a missing domain still returns `NotFoundException`.
|
||||
|
||||
|
||||
### API Key Behaviour Notes
|
||||
|
||||
#### `CreateApiKey` and `ImportApiKeys` share one route
|
||||
|
||||
@@ -1370,13 +1370,11 @@ public class ApiGatewayController {
|
||||
|
||||
@GET
|
||||
@Path("/domainnames/{domainName}/basepathmappings")
|
||||
public Response getBasePathMappings(@Context HttpHeaders headers, @PathParam("domainName") String domainName) {
|
||||
public Response getBasePathMappings(@Context HttpHeaders headers, @PathParam("domainName") String domainName,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<BasePathMapping> mappings = service.getBasePathMappings(region, domainName);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
mappings.forEach(m -> items.add(toMappingNode(m)));
|
||||
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
PaginatedResult<BasePathMapping> page = service.getBasePathMappingsPage(region, domainName, limit, position);
|
||||
return pageResponse(page, this::toMappingNode);
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -17,6 +17,7 @@ import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.CloudFrontEdgeDomain;
|
||||
import io.floci.aws.core.common.ModelTemplateGenerator;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.ReservedTags;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
@@ -2980,6 +2981,17 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
|
||||
return basePathMappingStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
public PaginatedResult<BasePathMapping> getBasePathMappingsPage(String region, String domainName,
|
||||
String limit, String position) {
|
||||
List<Map.Entry<String, BasePathMapping>> mappings =
|
||||
new ArrayList<>(basePathMappingsByStoredPath(region, domainName).entrySet());
|
||||
String scope = regionResolver.getAccountId() + "::" + mappingKey(region, domainName, "");
|
||||
PaginatedResult<Map.Entry<String, BasePathMapping>> page = pageById(mappings,
|
||||
entry -> scope + entry.getKey(), Pagination.parseMaxResults(limit, "limit", "BadRequestException"),
|
||||
position);
|
||||
return new PaginatedResult<>(page.items().stream().map(Map.Entry::getValue).toList(), page.nextToken());
|
||||
}
|
||||
|
||||
public void deleteBasePathMapping(String region, String domainName, String basePath) {
|
||||
synchronized (domainNameLock) {
|
||||
getBasePathMapping(region, domainName, basePath);
|
||||
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.apigateway.model.BasePathMapping;
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
|
||||
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.EndpointType;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetBasePathMappingsResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayBasePathMappingsPaginationIntegrationTest {
|
||||
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
@Test
|
||||
void explicitLimitAndSdkTraversalKeepEveryMappingExactlyOnce() {
|
||||
try (Fixture fixture = new Fixture(3)) {
|
||||
GetBasePathMappingsResponse first = fixture.client.getBasePathMappings(r ->
|
||||
r.domainName(fixture.domain).limit(1));
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
Set<String> seen = new HashSet<>();
|
||||
fixture.client.getBasePathMappingsPaginator(r -> r.domainName(fixture.domain).limit(1))
|
||||
.forEach(page -> {
|
||||
assertEquals(1, page.items().size());
|
||||
page.items().forEach(mapping -> assertTrue(seen.add(mapping.basePath())));
|
||||
});
|
||||
assertEquals(fixture.paths, seen);
|
||||
GetBasePathMappingsResponse complete = fixture.client.getBasePathMappings(r ->
|
||||
r.domainName(fixture.domain).limit(500));
|
||||
assertEquals(fixture.paths, complete.items().stream()
|
||||
.map(mapping -> mapping.basePath()).collect(Collectors.toSet()));
|
||||
assertNull(complete.position());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedLimitUsesTwentyFiveAndTheLastPageHasNoPosition() {
|
||||
try (Fixture fixture = new Fixture(26)) {
|
||||
GetBasePathMappingsResponse first = fixture.client.getBasePathMappings(r ->
|
||||
r.domainName(fixture.domain));
|
||||
assertEquals(25, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
GetBasePathMappingsResponse last = fixture.client.getBasePathMappings(r ->
|
||||
r.domainName(fixture.domain).position(first.position()));
|
||||
assertEquals(1, last.items().size());
|
||||
assertNull(last.position());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyDomainAndMissingParentKeepTheirExistingResponses() {
|
||||
try (Fixture fixture = new Fixture(0)) {
|
||||
GetBasePathMappingsResponse empty = fixture.client.getBasePathMappings(r ->
|
||||
r.domainName(fixture.domain).limit(1));
|
||||
assertTrue(empty.items().isEmpty());
|
||||
assertNull(empty.position());
|
||||
assertThrows(NotFoundException.class, () -> fixture.client.getBasePathMappings(r ->
|
||||
r.domainName("missing-" + fixture.domain).limit(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void positionCannotBeReplayedOnAnotherDomainAccountOrRegion() {
|
||||
String account = account();
|
||||
try (Fixture first = new Fixture(account, Region.US_EAST_1, 2);
|
||||
Fixture otherDomain = new Fixture(account, Region.US_EAST_1, 2);
|
||||
Fixture otherAccount = new Fixture(account(), Region.US_EAST_1, 2, first.domain);
|
||||
ApiGatewayClient otherRegion = AwsTestClients.create(
|
||||
ApiGatewayClient.builder(), account, Region.US_WEST_2)) {
|
||||
String position = first.client.getBasePathMappings(r ->
|
||||
r.domainName(first.domain).limit(1)).position();
|
||||
assertNotNull(position);
|
||||
for (Fixture other : List.of(otherDomain, otherAccount)) {
|
||||
assertThrows(BadRequestException.class, () -> other.client.getBasePathMappings(r ->
|
||||
r.domainName(other.domain).limit(1).position(position)));
|
||||
}
|
||||
assertThrows(NotFoundException.class, () -> otherRegion.getBasePathMappings(r ->
|
||||
r.domainName(first.domain).limit(1).position(position)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyRecordsWithTheSameReportedBasePathRemainDistinct() {
|
||||
try (Fixture fixture = new Fixture(0)) {
|
||||
AccountAwareStorageBackend<BasePathMapping> store = mappings();
|
||||
List<String> rawPaths = List.of("", "/", "(none)");
|
||||
Set<String> expectedIds = new HashSet<>();
|
||||
for (int i = 0; i < rawPaths.size(); i++) {
|
||||
String id = "legacy-api-" + i;
|
||||
expectedIds.add(id);
|
||||
store.putForAccount(fixture.account, fixture.region.id() + "::" + fixture.domain
|
||||
+ "::" + rawPaths.get(i), new BasePathMapping("(none)", id, "prod"));
|
||||
}
|
||||
Set<String> seen = new HashSet<>();
|
||||
fixture.client.getBasePathMappingsPaginator(r -> r.domainName(fixture.domain).limit(1))
|
||||
.forEach(page -> {
|
||||
assertEquals(1, page.items().size());
|
||||
page.items().forEach(mapping -> {
|
||||
assertEquals("(none)", mapping.basePath());
|
||||
assertTrue(seen.add(mapping.restApiId()));
|
||||
});
|
||||
});
|
||||
assertEquals(expectedIds, seen);
|
||||
}
|
||||
}
|
||||
|
||||
private AccountAwareStorageBackend<BasePathMapping> mappings() {
|
||||
return storageFactory.create("apigateway", "apigateway-mappings.json",
|
||||
new TypeReference<Map<String, BasePathMapping>>() {});
|
||||
}
|
||||
|
||||
private static String account() {
|
||||
return String.format("%012d", Math.floorMod(System.nanoTime(), 1_000_000_000_000L));
|
||||
}
|
||||
|
||||
private static final class Fixture implements AutoCloseable {
|
||||
private final String account;
|
||||
private final Region region;
|
||||
private final ApiGatewayClient client;
|
||||
private final String domain;
|
||||
private final String apiId;
|
||||
private final Set<String> paths = new HashSet<>();
|
||||
|
||||
private Fixture(int count) {
|
||||
this(account(), Region.US_EAST_1, count);
|
||||
}
|
||||
|
||||
private Fixture(String account, Region region, int count) {
|
||||
this(account, region, count, "basepath-page-" + UUID.randomUUID() + ".example.test");
|
||||
}
|
||||
|
||||
private Fixture(String account, Region region, int count, String domain) {
|
||||
this.domain = domain;
|
||||
this.account = account;
|
||||
this.region = region;
|
||||
client = AwsTestClients.create(ApiGatewayClient.builder(), account, region);
|
||||
apiId = client.createRestApi(r -> r.name("basepath-page-" + UUID.randomUUID())).id();
|
||||
client.createDomainName(r -> r.domainName(domain)
|
||||
.endpointConfiguration(c -> c.types(EndpointType.REGIONAL)));
|
||||
for (int i = 0; i < count; i++) {
|
||||
String path = "path-" + i;
|
||||
paths.add(path);
|
||||
client.createBasePathMapping(r -> r.domainName(domain).basePath(path)
|
||||
.restApiId(apiId).stage("prod"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
client.deleteDomainName(r -> r.domainName(domain));
|
||||
client.deleteRestApi(r -> r.restApiId(apiId));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user