mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(appconfig): paginate ListApplications results (#7787)
This commit is contained in:
1 parent
13640785a9
commit
c9fc00a8d5
4 files changed
+148
-8
No files matched your search
@@ -1,11 +1,36 @@
|
|||||||
"""AppConfig names, deployed content and session tokens belong to their request scope."""
|
"""AppConfig names, deployed content and session tokens belong to their request scope."""
|
||||||
|
|
||||||
from contextlib import ExitStack
|
from contextlib import ExitStack, closing
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
import pytest
|
import pytest
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
|
from botocore.stub import Stubber
|
||||||
|
|
||||||
|
|
||||||
|
def application_ids_with_name(client, name):
|
||||||
|
return [
|
||||||
|
app["Id"]
|
||||||
|
for page in client.get_paginator("list_applications").paginate()
|
||||||
|
for app in page["Items"]
|
||||||
|
if app["Name"] == name
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_application_name_lookup_reads_later_pages():
|
||||||
|
client = boto3.client(
|
||||||
|
"appconfig", region_name="us-east-1", aws_access_key_id="test",
|
||||||
|
aws_secret_access_key="test")
|
||||||
|
with closing(client), Stubber(client) as stubber:
|
||||||
|
stubber.add_response("list_applications", {
|
||||||
|
"Items": [{"Id": "first", "Name": "other"}], "NextToken": "later",
|
||||||
|
})
|
||||||
|
stubber.add_response("list_applications", {
|
||||||
|
"Items": [{"Id": "second", "Name": "target"}],
|
||||||
|
}, {"NextToken": "later"})
|
||||||
|
assert application_ids_with_name(client, "target") == ["second"]
|
||||||
|
stubber.assert_no_pending_responses()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("regions", [
|
@pytest.mark.parametrize("regions", [
|
||||||
@@ -63,10 +88,7 @@ def test_same_names_in_other_accounts_and_regions_do_not_alias(
|
|||||||
client = selection["client"]
|
client = selection["client"]
|
||||||
data = selection["data"]
|
data = selection["data"]
|
||||||
assert client.get_application(ApplicationId=unique_name)["Id"] == selection["app"]
|
assert client.get_application(ApplicationId=unique_name)["Id"] == selection["app"]
|
||||||
assert [
|
assert application_ids_with_name(client, unique_name) == [selection["app"]]
|
||||||
app["Id"] for app in client.list_applications()["Items"]
|
|
||||||
if app["Name"] == unique_name
|
|
||||||
] == [selection["app"]]
|
|
||||||
response = client.get_configuration(
|
response = client.get_configuration(
|
||||||
Application=unique_name, Environment="environment", Configuration="settings",
|
Application=unique_name, Environment="environment", Configuration="settings",
|
||||||
ClientId=unique_name)
|
ClientId=unique_name)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package io.floci.aws.services.appconfig;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import io.floci.aws.core.common.PaginatedResult;
|
||||||
import io.floci.aws.core.common.Pagination;
|
import io.floci.aws.core.common.Pagination;
|
||||||
import io.floci.aws.services.appconfig.AppConfigDataService.LegacyConfiguration;
|
import io.floci.aws.services.appconfig.AppConfigDataService.LegacyConfiguration;
|
||||||
import io.floci.aws.services.appconfig.model.Application;
|
import io.floci.aws.services.appconfig.model.Application;
|
||||||
@@ -75,11 +76,19 @@ public class AppConfigController {
|
|||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/applications")
|
@Path("/applications")
|
||||||
public Response listApplications() {
|
public Response listApplications(@QueryParam("max_results") String maxResults,
|
||||||
List<Application> apps = service.listApplications();
|
@QueryParam("next_token") String nextToken,
|
||||||
|
@Context UriInfo uriInfo) {
|
||||||
|
String token = nextToken == null && uriInfo.getQueryParameters().containsKey("next_token")
|
||||||
|
? "" : nextToken;
|
||||||
|
PaginatedResult<Application> page = service.listApplicationsPage(
|
||||||
|
Pagination.parseMaxResults(maxResults, "BadRequestException"), token);
|
||||||
ObjectNode root = objectMapper.createObjectNode();
|
ObjectNode root = objectMapper.createObjectNode();
|
||||||
ArrayNode items = root.putArray("Items");
|
ArrayNode items = root.putArray("Items");
|
||||||
apps.forEach(items::addPOJO);
|
page.items().forEach(items::addPOJO);
|
||||||
|
if (page.nextToken() != null) {
|
||||||
|
root.put("NextToken", page.nextToken());
|
||||||
|
}
|
||||||
return Response.ok(root).build();
|
return Response.ok(root).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import io.floci.aws.core.common.AwsArnUtils;
|
import io.floci.aws.core.common.AwsArnUtils;
|
||||||
import io.floci.aws.core.common.AwsException;
|
import io.floci.aws.core.common.AwsException;
|
||||||
|
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.RegionResolver;
|
||||||
import io.floci.aws.core.storage.StorageBackend;
|
import io.floci.aws.core.storage.StorageBackend;
|
||||||
import io.floci.aws.core.storage.StorageFactory;
|
import io.floci.aws.core.storage.StorageFactory;
|
||||||
@@ -105,6 +107,12 @@ public class AppConfigService {
|
|||||||
return applicationStore.scan(k -> true);
|
return applicationStore.scan(k -> true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PaginatedResult<Application> listApplicationsPage(Integer maxResults, String nextToken) {
|
||||||
|
String scope = "appconfig-applications:" + regions.getAccountId() + ":" + regions.getRegion();
|
||||||
|
return Pagination.paginate(listApplications(), Application::getId, maxResults, nextToken,
|
||||||
|
50, 50, 2048, "BadRequestException", scope, true);
|
||||||
|
}
|
||||||
|
|
||||||
/** The application whose ID is exactly {@code applicationId}; GetApplication also takes a name. */
|
/** The application whose ID is exactly {@code applicationId}; GetApplication also takes a name. */
|
||||||
public Optional<Application> findApplication(String applicationId) {
|
public Optional<Application> findApplication(String applicationId) {
|
||||||
return applicationStore.get(applicationId);
|
return applicationStore.get(applicationId);
|
||||||
|
|||||||
+101
@@ -0,0 +1,101 @@
|
|||||||
|
package io.floci.aws.services.appconfig;
|
||||||
|
|
||||||
|
import io.quarkus.test.junit.QuarkusTest;
|
||||||
|
import io.restassured.http.ContentType;
|
||||||
|
import io.restassured.response.Response;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static io.restassured.RestAssured.given;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@QuarkusTest
|
||||||
|
class AppConfigListApplicationsPaginationTest {
|
||||||
|
private static final String SCOPE =
|
||||||
|
"AWS4-HMAC-SHA256 Credential=123456789012/20261109/us-east-1/appconfig/aws4_request";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void listApplicationsRespectsMaxResultsAndReturnsNextToken() {
|
||||||
|
String prefix = "pagination-" + UUID.randomUUID() + "-";
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
String id = given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.contentType(ContentType.JSON)
|
||||||
|
.body("{\"Name\":\"" + prefix + i + "\"}")
|
||||||
|
.when().post("/applications")
|
||||||
|
.then().statusCode(201)
|
||||||
|
.extract().path("Id");
|
||||||
|
ids.add(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Response firstPage = given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("max_results", 1)
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(200)
|
||||||
|
.extract().response();
|
||||||
|
assertEquals(1, firstPage.jsonPath().getList("Items").size());
|
||||||
|
String nextToken = firstPage.jsonPath().getString("NextToken");
|
||||||
|
assertNotNull(nextToken);
|
||||||
|
|
||||||
|
Response secondPage = given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("max_results", 1)
|
||||||
|
.queryParam("next_token", nextToken)
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(200)
|
||||||
|
.extract().response();
|
||||||
|
assertEquals(1, secondPage.jsonPath().getList("Items").size());
|
||||||
|
assertNull(secondPage.jsonPath().getString("NextToken"));
|
||||||
|
assertTrue(ids.contains(firstPage.jsonPath().getString("Items[0].Id")));
|
||||||
|
assertTrue(ids.contains(secondPage.jsonPath().getString("Items[0].Id")));
|
||||||
|
assertNotEquals(firstPage.jsonPath().getString("Items[0].Id"),
|
||||||
|
secondPage.jsonPath().getString("Items[0].Id"));
|
||||||
|
|
||||||
|
given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("max_results", 0)
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(400);
|
||||||
|
given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("max_results", 51)
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(400);
|
||||||
|
given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("next_token", "not-a-token")
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(400);
|
||||||
|
given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.when().get("/applications?next_token=")
|
||||||
|
.then().statusCode(400);
|
||||||
|
given()
|
||||||
|
.header("Authorization", SCOPE)
|
||||||
|
.queryParam("next_token", "x".repeat(2049))
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(400);
|
||||||
|
given()
|
||||||
|
.header("Authorization",
|
||||||
|
"AWS4-HMAC-SHA256 Credential=210987654321/20261109/us-east-1/appconfig/aws4_request")
|
||||||
|
.queryParam("next_token", nextToken)
|
||||||
|
.when().get("/applications")
|
||||||
|
.then().statusCode(400);
|
||||||
|
} finally {
|
||||||
|
for (String id : ids) {
|
||||||
|
given().header("Authorization", SCOPE)
|
||||||
|
.when().delete("/applications/" + id).then().statusCode(204);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user