Light, fluffy, and always free - The AWS Local Emulator alternative
fix(backup): paginate backup plan listings (#6714)
Aniket Shuklaecb39dd7e0fb
diff --git a/docs/services/backup.md b/docs/services/backup.mdindex 644dd797db..4537809c0f 100644--- a/docs/services/backup.md+++ b/docs/services/backup.md@@ -247,13 +247,22 @@ Actual backup is simulated — no data is read from or written to the referenced- Report plans (`CreateReportPlan`, `DescribeReportPlan`, etc.)- Framework operations (`CreateFramework`, etc.)- Legal holds-- Pagination tokens on list operations other than `ListBackupVaults`+- Pagination tokens on list operations other than `ListBackupVaults` and `ListBackupPlans``ListBackupVaults` accepts `maxResults` from 1 to 1000 and returns `NextToken` whenmore vaults remain. Pass it as `nextToken` with the same region and `vaultType`filter to continue. Floci uses vault-name order and a page size of 1000 when thesize is omitted; AWS does not document a default order or page size.+`ListBackupPlans` also accepts `maxResults` from 1 to 1000. Floci uses plan-ID+order and defaults to 1000; AWS does not document a default order or page size.+Continue with `nextToken` in the same account and region, keeping `includeDeleted`+unchanged. Region and deletion-date filters apply before paging. Final pages omit+`NextToken`, and deleting or renaming the cursor plan does not skip the next plan.+`includeDeleted` defaults to false. When true, retained records include their+`DeletionDate`; normal plan deletion still removes the record, so deleted history+is not retained.+## Examples```bashdiff --git a/pom.xml b/pom.xmlindex 737108d0d5..a80885f25d 100644--- a/pom.xml+++ b/pom.xml@@ -336,6 +336,12 @@<version>${aws.sdk.version}</version><scope>test</scope></dependency>+ <dependency>+ <groupId>software.amazon.awssdk</groupId>+ <artifactId>backup</artifactId>+ <version>${aws.sdk.version}</version>+ <scope>test</scope>+ </dependency><dependency><groupId>software.amazon.awssdk</groupId><artifactId>sagemaker</artifactId>diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java b/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.javaindex f535361979..846132b8cc 100644--- a/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java+++ b/src/main/java/io/github/hectorvent/floci/services/backup/BackupController.java@@ -314,19 +314,30 @@ public class BackupController {@GET@Path("/backup/plans/")- public Response listBackupPlans() {- List<BackupPlan> plans = service.listBackupPlans();+ public Response listBackupPlans(@Context HttpHeaders headers,+ @QueryParam("includeDeleted") @DefaultValue("false") boolean includeDeleted,+ @QueryParam("maxResults") String maxResults,+ @QueryParam("nextToken") String nextToken) {+ String region = regionResolver.resolveRegion(headers);+ PaginatedResult<BackupPlan> page = service.listBackupPlans(region, includeDeleted,+ Pagination.parseMaxResults(maxResults, "InvalidParameterValueException"), nextToken);ObjectNode out = objectMapper.createObjectNode();ArrayNode list = out.putArray("BackupPlansList");- for (BackupPlan plan : plans) {+ for (BackupPlan plan : page.items()) {ObjectNode item = objectMapper.createObjectNode();item.put("BackupPlanId", plan.getBackupPlanId());item.put("BackupPlanArn", plan.getBackupPlanArn());item.put("BackupPlanName", plan.getBackupPlanName());item.put("CreationDate", plan.getCreationDate());item.put("VersionId", plan.getVersionId());+ if (plan.getDeletionDate() != null) {+ item.put("DeletionDate", plan.getDeletionDate());+ }list.add(item);}+ if (page.nextToken() != null) {+ out.put("NextToken", page.nextToken());+ }return Response.ok(out).build();}diff --git a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.javaindex 478e703a24..ff2c3c55ac 100644--- a/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java+++ b/src/main/java/io/github/hectorvent/floci/services/backup/BackupService.java@@ -43,6 +43,7 @@ import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;+import java.util.function.Function;@ApplicationScoped@Startup@@ -283,13 +284,18 @@ public class BackupService {public PaginatedResult<BackupVault> listBackupVaults(String region, String byVaultType,Integer maxResults, String nextToken) {+ String namespace = "backup:ListBackupVaults:" + regionResolver.getAccountId() + ":" + region + ":"+ + (byVaultType == null ? "all" : "type:" + byVaultType);+ return paginate(listBackupVaults(region, byVaultType), BackupVault::getBackupVaultName,+ maxResults, nextToken, namespace);+ }++ private static <T> PaginatedResult<T> paginate(List<T> items, Function<T, String> cursorOf,+ Integer maxResults, String nextToken, String namespace) {if (maxResults != null && (maxResults < 1 || maxResults > 1000)) {throw new AwsException("InvalidParameterValueException", "maxResults must be between 1 and 1000", 400);}- String namespace = "backup:ListBackupVaults:" + regionResolver.getAccountId() + ":" + region + ":"- + (byVaultType == null ? "all" : "type:" + byVaultType);- return Pagination.paginate(listBackupVaults(region, byVaultType), BackupVault::getBackupVaultName,- maxResults == null ? 1000 : maxResults, nextToken, namespace,+ return Pagination.paginate(items, cursorOf, maxResults == null ? 1000 : maxResults, nextToken, namespace,token -> new AwsException("InvalidParameterValueException", "Invalid nextToken.", 400));}@@ -676,6 +682,17 @@ public class BackupService {return planStore.scan(k -> true);}+ public PaginatedResult<BackupPlan> listBackupPlans(String region, boolean includeDeleted,+ Integer maxResults, String nextToken) {+ List<BackupPlan> plans = listBackupPlans().stream()+ .filter(plan -> region.equals(AwsArnUtils.regionOrDefault(plan.getBackupPlanArn(),+ regionResolver.getDefaultRegion())))+ .filter(plan -> includeDeleted || plan.getDeletionDate() == null)+ .toList();+ String namespace = "backup:ListBackupPlans:" + regionResolver.getAccountId() + ":" + region + ":" + includeDeleted;+ return paginate(plans, BackupPlan::getBackupPlanId, maxResults, nextToken, namespace);+ }+// ── Selection ──────────────────────────────────────────────────────────────public BackupSelection createBackupSelection(String planId, String selectionName,diff --git a/src/test/java/io/github/hectorvent/floci/services/backup/BackupListPlansPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/backup/BackupListPlansPaginationIntegrationTest.javanew file mode 100644index 0000000000..b59f019d55--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/backup/BackupListPlansPaginationIntegrationTest.java@@ -0,0 +1,251 @@+package io.github.hectorvent.floci.services.backup;++import io.github.hectorvent.floci.core.common.RequestScopes;+import io.github.hectorvent.floci.services.backup.model.BackupRule;+import io.github.hectorvent.floci.testing.AwsTestClients;+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.CsvSource;+import org.junit.jupiter.params.provider.ValueSource;+import software.amazon.awssdk.regions.Region;+import software.amazon.awssdk.services.backup.BackupClient;+import software.amazon.awssdk.services.backup.model.BackupException;+import software.amazon.awssdk.services.backup.model.BackupPlansListMember;+import software.amazon.awssdk.services.backup.model.BackupRuleInput;+import software.amazon.awssdk.services.backup.model.ListBackupPlansRequest;+import software.amazon.awssdk.services.backup.model.ListBackupPlansResponse;++import java.time.Instant;+import java.util.ArrayList;+import java.util.List;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.equalTo;+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 BackupListPlansPaginationIntegrationTest {++ private static final String ACCOUNT = "741664900001";+ private static final String REGION = "eu-north-1";+ private final String name = "bp-" + UUID.randomUUID();+ private final List<String> plans = new ArrayList<>();+ private BackupClient client;+ private int sequence;++ @Inject+ BackupService service;++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @BeforeEach+ void openClient() {+ client = AwsTestClients.create(BackupClient.builder(), ACCOUNT, Region.of(REGION));+ client.createBackupVault(request -> request.backupVaultName(name));+ }++ @AfterEach+ void cleanUp() {+ try {+ RequestScopes.runAs(ACCOUNT, REGION, () -> plans.forEach(service::deleteBackupPlan));+ client.deleteBackupVault(request -> request.backupVaultName(name));+ } finally {+ client.close();+ }+ }++ @Test+ void sdkPagesPlansByStableIdsAcrossNameChanges() {+ create();+ create();+ create();+ ListBackupPlansRequest request = ListBackupPlansRequest.builder().maxResults(1).build();+ ListBackupPlansResponse first = client.listBackupPlans(request);+ assertEquals(1, first.backupPlansList().size());+ BackupPlansListMember plan = first.backupPlansList().getFirst();+ assertTrue(plan.backupPlanName().startsWith(name + "-"));+ assertNotNull(plan.backupPlanArn());+ assertNotNull(plan.creationDate());+ assertNotNull(plan.versionId());+ assertNotNull(first.nextToken());+ client.updateBackupPlan(update -> update.backupPlanId(plan.backupPlanId()).backupPlan(body -> body+ .backupPlanName(name + "-renamed").rules(BackupRuleInput.builder().ruleName("daily")+ .targetBackupVaultName(name).scheduleExpression("cron(0 12 * * ? *)").build())));+ ListBackupPlansResponse second = client.listBackupPlans(request.toBuilder().nextToken(first.nextToken()).build());+ assertNotNull(second.nextToken());+ ListBackupPlansResponse last = client.listBackupPlans(request.toBuilder().nextToken(second.nextToken()).build());+ assertNull(last.nextToken());+ assertEquals(plans.stream().sorted().toList(), List.of(first, second, last).stream()+ .flatMap(page -> page.backupPlansList().stream()).map(BackupPlansListMember::backupPlanId).toList());+ }++ @Test+ void includeDeletedIsBoundToTheListingToken() {+ create();+ create();+ String token = client.listBackupPlans(request -> request.maxResults(1)).nextToken();+ assertNotNull(token);+ assertEquals(1, client.listBackupPlans(request -> request.maxResults(1).includeDeleted(false)+ .nextToken(token)).backupPlansList().size());+ BackupException error = assertThrows(BackupException.class,+ () -> client.listBackupPlans(request -> request.maxResults(1).includeDeleted(true).nextToken(token)));+ assertEquals("InvalidParameterValueException", error.awsErrorDetails().errorCode());+ assertEquals(2, client.listBackupPlans(request -> request.includeDeleted(true)).backupPlansList().size());+ }++ @Test+ void retainedDeletionDatesAreFilteredBeforePagingAndIncludedInSummaries() {+ create();+ create();+ create();+ String deletedId = plans.getFirst();+ Instant deletion = Instant.ofEpochSecond(1720000000);+ RequestScopes.runAs(ACCOUNT, REGION,+ () -> service.getBackupPlan(deletedId).setDeletionDate(deletion.getEpochSecond()));+ assertEquals(2, client.listBackupPlans(request -> request.includeDeleted(false)).backupPlansList().size());+ ListBackupPlansResponse first = client.listBackupPlans(request -> request.maxResults(1));+ assertEquals(1, first.backupPlansList().size());+ assertNotNull(first.nextToken());+ ListBackupPlansResponse last = client.listBackupPlans(request -> request.maxResults(1).nextToken(first.nextToken()));+ assertNull(last.nextToken());+ assertEquals(plans.stream().filter(id -> !id.equals(deletedId)).sorted().toList(), List.of(first, last).stream()+ .flatMap(page -> page.backupPlansList().stream()).map(BackupPlansListMember::backupPlanId).toList());+ ListBackupPlansResponse all = client.listBackupPlans(request -> request.includeDeleted(true));+ assertEquals(3, all.backupPlansList().size());+ assertEquals(deletion, all.backupPlansList().stream().filter(plan -> plan.backupPlanId().equals(deletedId))+ .findFirst().orElseThrow().deletionDate());+ }++ @Test+ void vaultListingTokenCannotBeUsedForPlans() {+ List<String> vaults = new ArrayList<>();+ try {+ for (String vault : List.of(name + "-a", name + "-b")) {+ client.createBackupVault(request -> request.backupVaultName(vault));+ vaults.add(vault);+ }+ String token = client.listBackupVaults(request -> request.maxResults(1)).nextToken();+ assertNotNull(token);+ BackupException error = assertThrows(BackupException.class,+ () -> client.listBackupPlans(request -> request.nextToken(token)));+ assertEquals("InvalidParameterValueException", error.awsErrorDetails().errorCode());+ } finally {+ vaults.forEach(vault -> client.deleteBackupVault(request -> request.backupVaultName(vault)));+ }+ }++ @ParameterizedTest+ @CsvSource({"741664900002,eu-north-1", "741664900001,us-west-2"})+ void listingsAndTokensStayWithinTheirAccountAndRegion(String account, String region) {+ create();+ create();+ try (BackupClient other = AwsTestClients.create(BackupClient.builder(), account, Region.of(region))) {+ other.createBackupVault(request -> request.backupVaultName(name));+ String otherId = create(other);+ try {+ assertEquals(plans.stream().sorted().toList(), client.listBackupPlans().backupPlansList().stream()+ .map(BackupPlansListMember::backupPlanId).sorted().toList());+ assertEquals(List.of(otherId), other.listBackupPlans().backupPlansList().stream()+ .map(BackupPlansListMember::backupPlanId).toList());+ String token = client.listBackupPlans(request -> request.maxResults(1)).nextToken();+ assertNotNull(token);+ BackupException error = assertThrows(BackupException.class,+ () -> other.listBackupPlans(request -> request.nextToken(token)));+ assertEquals("InvalidParameterValueException", error.awsErrorDetails().errorCode());+ } finally {+ other.deleteBackupPlan(request -> request.backupPlanId(otherId));+ other.deleteBackupVault(request -> request.backupVaultName(name));+ }+ }+ }++ @Test+ void deletingTheCursorPlanDoesNotSkipTheNextPlan() {+ create();+ create();+ create();+ ListBackupPlansResponse first = client.listBackupPlans(request -> request.maxResults(1));+ assertNotNull(first.nextToken());+ String removed = first.backupPlansList().getFirst().backupPlanId();+ client.deleteBackupPlan(request -> request.backupPlanId(removed));+ plans.remove(removed);+ ListBackupPlansResponse last = client.listBackupPlans(request -> request.nextToken(first.nextToken()));+ assertEquals(plans.stream().sorted().toList(), last.backupPlansList().stream()+ .map(BackupPlansListMember::backupPlanId).toList());+ assertNull(last.nextToken());+ }++ @Test+ void omittedAndMaximumPageSizesReturnAtMostOneThousandPlans() {+ RequestScopes.runAs(ACCOUNT, REGION, () -> {+ for (int i = 0; i <= 1000; i++) {+ BackupRule rule = new BackupRule();+ rule.setRuleName("daily");+ rule.setTargetBackupVaultName(name);+ rule.setScheduleExpression("cron(0 12 * * ? *)");+ plans.add(service.createBackupPlan(name + "-" + ++sequence, List.of(rule), null, REGION)+ .getBackupPlanId());+ }+ });+ for (ListBackupPlansRequest request : List.of(ListBackupPlansRequest.builder().build(),+ ListBackupPlansRequest.builder().maxResults(1000).build())) {+ ListBackupPlansResponse first = client.listBackupPlans(request);+ assertEquals(1000, first.backupPlansList().size());+ assertNotNull(first.nextToken());+ ListBackupPlansResponse last = client.listBackupPlans(request.toBuilder().nextToken(first.nextToken()).build());+ assertEquals(1, last.backupPlansList().size());+ assertNull(last.nextToken());+ }+ }++ @ParameterizedTest+ @ValueSource(strings = {"0", "-1", "1001", "1.5", "abc", "2147483648"})+ void invalidPageSizesReturnAwsErrors(String size) {+ given().header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261027/" + REGION+ + "/backup/aws4_request").queryParam("maxResults", size)+ .get("/backup/plans/").then().statusCode(400)+ .body("__type", equalTo("InvalidParameterValueException"));+ }++ @ParameterizedTest+ @ValueSource(strings = {"not-a-token!", "MQ=="})+ void malformedTokensReturnAwsErrors(String token) {+ BackupException error = assertThrows(BackupException.class,+ () -> client.listBackupPlans(request -> request.nextToken(token)));+ assertEquals(400, error.statusCode());+ assertEquals("InvalidParameterValueException", error.awsErrorDetails().errorCode());+ }++ @Test+ void emptyListingHasNoContinuationToken() {+ ListBackupPlansResponse result = client.listBackupPlans(request -> request.maxResults(1));+ assertTrue(result.backupPlansList().isEmpty());+ assertNull(result.nextToken());+ }++ private void create() {+ plans.add(create(client));+ }++ private String create(BackupClient sdk) {+ String planName = name + "-" + ++sequence;+ return sdk.createBackupPlan(request -> request.backupPlan(plan -> plan.backupPlanName(planName)+ .rules(BackupRuleInput.builder().ruleName("daily").targetBackupVaultName(name)+ .scheduleExpression("cron(0 12 * * ? *)").build()))).backupPlanId();+ }++}diff --git a/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java b/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.javanew file mode 100644index 0000000000..570c8df8f6--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/testing/AwsTestClients.java@@ -0,0 +1,26 @@+package io.github.hectorvent.floci.testing;++import io.restassured.RestAssured;+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;+import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;+import software.amazon.awssdk.awscore.client.builder.AwsSyncClientBuilder;+import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;+import software.amazon.awssdk.regions.Region;++import java.net.URI;++/** Synchronous AWS SDK clients pointed at the Floci instance a {@code @QuarkusTest} runs. */+public final class AwsTestClients {++ private AwsTestClients() {+ }++ public static <B extends AwsClientBuilder<B, C> & AwsSyncClientBuilder<B, C>, C> C create(+ B builder, String account, Region region) {+ return builder.endpointOverride(URI.create("http://localhost:" + RestAssured.port))+ .httpClientBuilder(UrlConnectionHttpClient.builder()).region(region)+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(account, "test")))+ .build();+ }+}