diff --git a/pom.xml b/pom.xml
index d8018c1785..8f1cf5d973 100644
--- a/pom.xml
+++ b/pom.xml
@@ -432,6 +432,12 @@
${aws.sdk.version}
test
+
+ software.amazon.awssdk
+ route53
+ ${aws.sdk.version}
+ test
+
software.amazon.awssdk
sns
diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
index 95a990f7f0..bf605dab7f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
+++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java
@@ -143,9 +143,10 @@ public class Route53Controller {
public Response listHostedZones(@QueryParam("marker") String marker,
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
try {
- List zones = service.listHostedZones(marker, maxItems);
- long total = service.getHostedZoneCount();
- boolean truncated = zones.size() == maxItems && zones.size() < total;
+ int lookaheadLimit = maxItems > 0 && maxItems < Integer.MAX_VALUE ? maxItems + 1 : maxItems;
+ List remaining = service.listHostedZones(marker, lookaheadLimit);
+ boolean truncated = maxItems > 0 && remaining.size() > maxItems;
+ List zones = truncated ? remaining.subList(0, maxItems) : remaining;
XmlBuilder xml = new XmlBuilder()
.start("ListHostedZonesResponse", NS)
@@ -156,8 +157,8 @@ public class Route53Controller {
xml.end("HostedZones")
.elem("Marker", marker != null ? marker : "")
.elem("IsTruncated", String.valueOf(truncated));
- if (truncated && !zones.isEmpty()) {
- xml.elem("NextMarker", zones.get(zones.size() - 1).getId());
+ if (truncated) {
+ xml.elem("NextMarker", remaining.get(maxItems).getId());
}
xml.elem("MaxItems", String.valueOf(maxItems))
.end("ListHostedZonesResponse");
diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
index c14df9e678..8f8f8383f9 100644
--- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Service.java
@@ -26,6 +26,7 @@ import java.security.SecureRandom;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -163,7 +164,7 @@ public class Route53Service implements Resettable {
public List listHostedZones(String marker, int maxItems) {
List all = new ArrayList<>(zoneStore.scan(k -> true));
- all.sort((a, b) -> a.getName().compareTo(b.getName()));
+ all.sort(Comparator.comparing(HostedZone::getName).thenComparing(HostedZone::getId));
for (HostedZone zone : all) {
zone.setResourceRecordSetCount(recordCount(zone.getId()));
}
@@ -171,7 +172,7 @@ public class Route53Service implements Resettable {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getId().equals(marker)) {
- idx = i + 1;
+ idx = i;
break;
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/route53/Route53HostedZonesPaginationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/route53/Route53HostedZonesPaginationIntegrationTest.java
new file mode 100644
index 0000000000..619103ecfe
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/route53/Route53HostedZonesPaginationIntegrationTest.java
@@ -0,0 +1,134 @@
+package io.github.hectorvent.floci.services.route53;
+
+import io.github.hectorvent.floci.services.route53.model.HostedZone;
+import io.github.hectorvent.floci.testing.AwsTestClients;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.path.xml.XmlPath;
+import jakarta.inject.Inject;
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.route53.Route53Client;
+import software.amazon.awssdk.services.route53.model.ListHostedZonesResponse;
+
+import java.util.List;
+
+import static io.restassured.RestAssured.given;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class Route53HostedZonesPaginationIntegrationTest {
+
+ @Inject
+ Route53Service service;
+
+ @Test
+ void listHostedZonesUsesTheNextPagesFirstZoneAsMarker() {
+ String firstZoneId = createZone("pagination-first.example.com.");
+ String secondZoneId = null;
+
+ try {
+ secondZoneId = createZone("pagination-second.example.com.");
+ List allZones = service.listHostedZones(null, Integer.MAX_VALUE);
+ List allZoneIds = allZones.stream().map(HostedZone::getId).toList();
+ assertTrue(allZoneIds.contains(firstZoneId));
+ assertTrue(allZoneIds.contains(secondZoneId));
+
+ XmlPath initialPage = given()
+ .queryParam("maxitems", 1)
+ .when().get("/2013-04-01/hostedzone")
+ .then().statusCode(200)
+ .extract().xmlPath();
+
+ assertEquals("/hostedzone/" + allZones.getFirst().getId(),
+ initialPage.getString("ListHostedZonesResponse.HostedZones.HostedZone.Id"));
+ assertTrue(initialPage.getBoolean("ListHostedZonesResponse.IsTruncated"));
+ assertEquals(allZones.get(1).getId(), initialPage.getString("ListHostedZonesResponse.NextMarker"));
+
+ String penultimateId = allZones.get(allZones.size() - 2).getId();
+ String lastId = allZones.getLast().getId();
+
+ XmlPath firstPage = given()
+ .queryParam("marker", penultimateId)
+ .queryParam("maxitems", 1)
+ .when().get("/2013-04-01/hostedzone")
+ .then().statusCode(200)
+ .extract().xmlPath();
+
+ assertEquals("/hostedzone/" + penultimateId,
+ firstPage.getString("ListHostedZonesResponse.HostedZones.HostedZone.Id"));
+ assertEquals(penultimateId, firstPage.getString("ListHostedZonesResponse.Marker"));
+ assertTrue(firstPage.getBoolean("ListHostedZonesResponse.IsTruncated"));
+ assertEquals(lastId, firstPage.getString("ListHostedZonesResponse.NextMarker"));
+
+ String lastPageBody = given()
+ .queryParam("marker", firstPage.getString("ListHostedZonesResponse.NextMarker"))
+ .queryParam("maxitems", 1)
+ .when().get("/2013-04-01/hostedzone")
+ .then().statusCode(200)
+ .extract().body().asString();
+ XmlPath lastPage = new XmlPath(lastPageBody);
+
+ assertEquals("/hostedzone/" + lastId,
+ lastPage.getString("ListHostedZonesResponse.HostedZones.HostedZone.Id"));
+ assertEquals(lastId, lastPage.getString("ListHostedZonesResponse.Marker"));
+ assertFalse(lastPage.getBoolean("ListHostedZonesResponse.IsTruncated"));
+ assertFalse(lastPageBody.contains(""));
+ } finally {
+ if (secondZoneId != null) {
+ given().when().delete("/2013-04-01/hostedzone/" + secondZoneId).then().statusCode(200);
+ }
+ given().when().delete("/2013-04-01/hostedzone/" + firstZoneId).then().statusCode(200);
+ }
+ }
+
+ @Test
+ void awsSdkPaginatesListHostedZonesWithStableDuplicateNames() {
+ String firstZoneId = createZone("sdk-pagination.example.com.");
+ String secondZoneId = null;
+
+ try {
+ secondZoneId = createZone("sdk-pagination.example.com.");
+ String createdSecondZoneId = secondZoneId;
+ List expected = service.listHostedZones(null, Integer.MAX_VALUE);
+ assertEquals(List.of(firstZoneId, secondZoneId).stream().sorted().toList(),
+ expected.stream().map(HostedZone::getId)
+ .filter(id -> id.equals(firstZoneId) || id.equals(createdSecondZoneId)).toList());
+ try (Route53Client client = AwsTestClients.create(Route53Client.builder(), "test", Region.US_EAST_1)) {
+ List pages = client.listHostedZonesPaginator(request -> request.maxItems("1"))
+ .stream().toList();
+ assertEquals(expected.size(), pages.size());
+ for (int i = 0; i < pages.size(); i++) {
+ ListHostedZonesResponse page = pages.get(i);
+ assertEquals(1, page.hostedZones().size());
+ assertEquals(expected.get(i).getId(), page.hostedZones().getFirst().id());
+ boolean hasNext = i + 1 < pages.size();
+ assertEquals(hasNext, page.isTruncated());
+ assertEquals(hasNext ? expected.get(i + 1).getId() : null, page.nextMarker());
+ }
+ }
+ } finally {
+ if (secondZoneId != null) {
+ given().when().delete("/2013-04-01/hostedzone/" + secondZoneId).then().statusCode(200);
+ }
+ given().when().delete("/2013-04-01/hostedzone/" + firstZoneId).then().statusCode(200);
+ }
+ }
+
+ private String createZone(String name) {
+ String request = """
+
+ %s
+ pagination-%s-%s
+
+ """.formatted(name, name, System.nanoTime());
+ String location = given()
+ .contentType("application/xml")
+ .body(request)
+ .when().post("/2013-04-01/hostedzone")
+ .then().statusCode(201)
+ .extract().header("Location");
+ return location.substring(location.lastIndexOf('/') + 1);
+ }
+}