mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): paginate REST deployments (#9437)
Use the shared ID pager and response envelope for GetDeployments. Preserve unpaged internal reads, missing-parent responses and deployment metadata. Closes #8603
This commit is contained in:
1 parent
d5fdfbaa63
commit
208b07b54a
4 files changed
+158
-7
No files matched your search
@@ -204,6 +204,14 @@ 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`.
|
||||
|
||||
|
||||
### Deployment Pagination
|
||||
|
||||
`GetDeployments` accepts `limit` and `position`, returns 25 deployments by default,
|
||||
and caps pages at 500. Use the returned `position` to continue a partial collection;
|
||||
the final page omits it. Positions belong to the caller's account, Region and REST
|
||||
API. A missing parent API returns `NotFoundException`.
|
||||
|
||||
|
||||
### API Key Behaviour Notes
|
||||
|
||||
#### `CreateApiKey` and `ImportApiKeys` share one route
|
||||
|
||||
@@ -660,14 +660,10 @@ public class ApiGatewayController {
|
||||
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/deployments")
|
||||
public Response getDeployments(@Context HttpHeaders headers,
|
||||
@PathParam("apiId") String apiId) {
|
||||
public Response getDeployments(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<io.floci.aws.services.apigateway.model.Deployment> deployments = service.getDeployments(region, apiId);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
deployments.forEach(d -> items.add(toDeploymentNode(d)));
|
||||
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
return pageResponse(service.getDeploymentsPage(region, apiId, limit, position), this::toDeploymentNode);
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -939,6 +939,14 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
|
||||
return deploymentStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
public PaginatedResult<Deployment> getDeploymentsPage(String region, String apiId,
|
||||
String limit, String position) {
|
||||
List<Deployment> deployments = getDeployments(region, apiId);
|
||||
String scope = regionResolver.getAccountId() + "::" + region + "::" + apiId + "::";
|
||||
return pageById(deployments, deployment -> scope + deployment.id(),
|
||||
Pagination.parseMaxResults(limit, "limit", "BadRequestException"), position);
|
||||
}
|
||||
|
||||
public Deployment getDeployment(String region, String apiId, String deploymentId) {
|
||||
return deploymentStore.get(deploymentKey(region, apiId, deploymentId))
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "Deployment not found", 404));
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
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.GetDeploymentsResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.IntegrationType;
|
||||
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
|
||||
|
||||
import java.util.HashSet;
|
||||
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 ApiGatewayDeploymentsPaginationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void explicitLimitAndSdkTraversalPreserveDeploymentFields() {
|
||||
try (Fixture fixture = new Fixture(3)) {
|
||||
GetDeploymentsResponse first = fixture.client.getDeployments(r ->
|
||||
r.restApiId(fixture.apiId).limit(1));
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
Set<String> seen = new HashSet<>();
|
||||
fixture.client.getDeploymentsPaginator(r -> r.restApiId(fixture.apiId).limit(1))
|
||||
.forEach(page -> {
|
||||
assertEquals(1, page.items().size());
|
||||
page.items().forEach(deployment -> {
|
||||
assertTrue(seen.add(deployment.id()));
|
||||
assertNotNull(deployment.createdDate());
|
||||
assertTrue(deployment.description().startsWith("deployment-"));
|
||||
});
|
||||
});
|
||||
assertEquals(fixture.ids, seen);
|
||||
GetDeploymentsResponse complete = fixture.client.getDeployments(r ->
|
||||
r.restApiId(fixture.apiId).limit(500));
|
||||
assertEquals(fixture.ids, complete.items().stream().map(deployment -> deployment.id())
|
||||
.collect(Collectors.toSet()));
|
||||
assertNull(complete.position());
|
||||
fixture.ids.forEach(id -> assertEquals(id, fixture.client.getDeployment(r ->
|
||||
r.restApiId(fixture.apiId).deploymentId(id)).id()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedLimitUsesTwentyFiveAndTheLastPageOmitsPosition() {
|
||||
try (Fixture fixture = new Fixture(26)) {
|
||||
GetDeploymentsResponse first = fixture.client.getDeployments(r -> r.restApiId(fixture.apiId));
|
||||
assertEquals(25, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
GetDeploymentsResponse last = fixture.client.getDeployments(r ->
|
||||
r.restApiId(fixture.apiId).position(first.position()));
|
||||
assertEquals(1, last.items().size());
|
||||
assertNull(last.position());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyApiAndMissingParentKeepTheirExistingResponses() {
|
||||
try (Fixture fixture = new Fixture(0)) {
|
||||
GetDeploymentsResponse empty = fixture.client.getDeployments(r ->
|
||||
r.restApiId(fixture.apiId).limit(1));
|
||||
assertTrue(empty.items().isEmpty());
|
||||
assertNull(empty.position());
|
||||
assertThrows(NotFoundException.class, () -> fixture.client.getDeployments(r ->
|
||||
r.restApiId("missing-api").limit(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void positionStaysWithItsApiAndDoesNotExposeForeignParents() {
|
||||
String account = account();
|
||||
try (Fixture first = new Fixture(account, 2);
|
||||
Fixture otherApi = new Fixture(account, 2);
|
||||
ApiGatewayClient otherAccount = AwsTestClients.create(
|
||||
ApiGatewayClient.builder(), account(), Region.US_EAST_1);
|
||||
ApiGatewayClient otherRegion = AwsTestClients.create(
|
||||
ApiGatewayClient.builder(), account, Region.US_WEST_2)) {
|
||||
String position = first.client.getDeployments(r ->
|
||||
r.restApiId(first.apiId).limit(1)).position();
|
||||
assertNotNull(position);
|
||||
assertThrows(BadRequestException.class, () -> otherApi.client.getDeployments(r ->
|
||||
r.restApiId(otherApi.apiId).limit(1).position(position)));
|
||||
assertThrows(NotFoundException.class, () -> otherAccount.getDeployments(r ->
|
||||
r.restApiId(first.apiId).limit(1).position(position)));
|
||||
assertThrows(NotFoundException.class, () -> otherRegion.getDeployments(r ->
|
||||
r.restApiId(first.apiId).limit(1).position(position)));
|
||||
}
|
||||
}
|
||||
|
||||
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 ApiGatewayClient client;
|
||||
private final String apiId;
|
||||
private final Set<String> ids = new HashSet<>();
|
||||
|
||||
private Fixture(int count) {
|
||||
this(account(), count);
|
||||
}
|
||||
|
||||
private Fixture(String account, int count) {
|
||||
client = AwsTestClients.create(ApiGatewayClient.builder(), account, Region.US_EAST_1);
|
||||
apiId = client.createRestApi(r -> r.name("deployment-page-" + UUID.randomUUID())).id();
|
||||
String root = client.getResources(r -> r.restApiId(apiId)).items().stream()
|
||||
.filter(resource -> "/".equals(resource.path())).findFirst().orElseThrow().id();
|
||||
client.putMethod(r -> r.restApiId(apiId).resourceId(root).httpMethod("GET")
|
||||
.authorizationType("NONE"));
|
||||
client.putIntegration(r -> r.restApiId(apiId).resourceId(root).httpMethod("GET")
|
||||
.type(IntegrationType.MOCK).requestTemplates(Map.of("application/json", "{\"statusCode\":200}")));
|
||||
for (int i = 0; i < count; i++) {
|
||||
String description = "deployment-" + i;
|
||||
ids.add(client.createDeployment(r -> r.restApiId(apiId).description(description)).id());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
client.deleteRestApi(r -> r.restApiId(apiId));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user