mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 10:38:00 -05:00
fix(route53): honor hosted zone pagination markers (#6932)
* fix(route53): honor hosted zone pagination markers * test(route53): reuse shared SDK client fixture
This commit is contained in:
1 parent
b063f7ff8c
commit
dfefef2437
4 files changed
+149
-7
No files matched your search
@@ -432,6 +432,12 @@
|
|||||||
<version>${aws.sdk.version}</version>
|
<version>${aws.sdk.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
|
<artifactId>route53</artifactId>
|
||||||
|
<version>${aws.sdk.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>software.amazon.awssdk</groupId>
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
<artifactId>sns</artifactId>
|
<artifactId>sns</artifactId>
|
||||||
|
|||||||
@@ -143,9 +143,10 @@ public class Route53Controller {
|
|||||||
public Response listHostedZones(@QueryParam("marker") String marker,
|
public Response listHostedZones(@QueryParam("marker") String marker,
|
||||||
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
|
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
|
||||||
try {
|
try {
|
||||||
List<HostedZone> zones = service.listHostedZones(marker, maxItems);
|
int lookaheadLimit = maxItems > 0 && maxItems < Integer.MAX_VALUE ? maxItems + 1 : maxItems;
|
||||||
long total = service.getHostedZoneCount();
|
List<HostedZone> remaining = service.listHostedZones(marker, lookaheadLimit);
|
||||||
boolean truncated = zones.size() == maxItems && zones.size() < total;
|
boolean truncated = maxItems > 0 && remaining.size() > maxItems;
|
||||||
|
List<HostedZone> zones = truncated ? remaining.subList(0, maxItems) : remaining;
|
||||||
|
|
||||||
XmlBuilder xml = new XmlBuilder()
|
XmlBuilder xml = new XmlBuilder()
|
||||||
.start("ListHostedZonesResponse", NS)
|
.start("ListHostedZonesResponse", NS)
|
||||||
@@ -156,8 +157,8 @@ public class Route53Controller {
|
|||||||
xml.end("HostedZones")
|
xml.end("HostedZones")
|
||||||
.elem("Marker", marker != null ? marker : "")
|
.elem("Marker", marker != null ? marker : "")
|
||||||
.elem("IsTruncated", String.valueOf(truncated));
|
.elem("IsTruncated", String.valueOf(truncated));
|
||||||
if (truncated && !zones.isEmpty()) {
|
if (truncated) {
|
||||||
xml.elem("NextMarker", zones.get(zones.size() - 1).getId());
|
xml.elem("NextMarker", remaining.get(maxItems).getId());
|
||||||
}
|
}
|
||||||
xml.elem("MaxItems", String.valueOf(maxItems))
|
xml.elem("MaxItems", String.valueOf(maxItems))
|
||||||
.end("ListHostedZonesResponse");
|
.end("ListHostedZonesResponse");
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.security.SecureRandom;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -163,7 +164,7 @@ public class Route53Service implements Resettable {
|
|||||||
|
|
||||||
public List<HostedZone> listHostedZones(String marker, int maxItems) {
|
public List<HostedZone> listHostedZones(String marker, int maxItems) {
|
||||||
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
|
List<HostedZone> 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) {
|
for (HostedZone zone : all) {
|
||||||
zone.setResourceRecordSetCount(recordCount(zone.getId()));
|
zone.setResourceRecordSetCount(recordCount(zone.getId()));
|
||||||
}
|
}
|
||||||
@@ -171,7 +172,7 @@ public class Route53Service implements Resettable {
|
|||||||
int idx = 0;
|
int idx = 0;
|
||||||
for (int i = 0; i < all.size(); i++) {
|
for (int i = 0; i < all.size(); i++) {
|
||||||
if (all.get(i).getId().equals(marker)) {
|
if (all.get(i).getId().equals(marker)) {
|
||||||
idx = i + 1;
|
idx = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+134
@@ -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<HostedZone> allZones = service.listHostedZones(null, Integer.MAX_VALUE);
|
||||||
|
List<String> 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("<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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void awsSdkPaginatesListHostedZonesWithStableDuplicateNames() {
|
||||||
|
String firstZoneId = createZone("sdk-pagination.example.com.");
|
||||||
|
String secondZoneId = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
secondZoneId = createZone("sdk-pagination.example.com.");
|
||||||
|
String createdSecondZoneId = secondZoneId;
|
||||||
|
List<HostedZone> 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<ListHostedZonesResponse> 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 = """
|
||||||
|
<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
||||||
|
<Name>%s</Name>
|
||||||
|
<CallerReference>pagination-%s-%s</CallerReference>
|
||||||
|
</CreateHostedZoneRequest>
|
||||||
|
""".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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user