mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(route53): correct health check pagination (#6954)
This commit is contained in:
1 parent
358f34b72f
commit
8aa7bc98ad
5 files changed
+265
-41
No files matched your search
@@ -143,22 +143,19 @@ public class Route53Controller {
|
||||
public Response listHostedZones(@QueryParam("marker") String marker,
|
||||
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
|
||||
try {
|
||||
int lookaheadLimit = maxItems > 0 && maxItems < Integer.MAX_VALUE ? maxItems + 1 : maxItems;
|
||||
List<HostedZone> remaining = service.listHostedZones(marker, lookaheadLimit);
|
||||
boolean truncated = maxItems > 0 && remaining.size() > maxItems;
|
||||
List<HostedZone> zones = truncated ? remaining.subList(0, maxItems) : remaining;
|
||||
Route53Pagination.Page<HostedZone> page = service.pageHostedZones(marker, maxItems);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("ListHostedZonesResponse", NS)
|
||||
.start("HostedZones");
|
||||
for (HostedZone zone : zones) {
|
||||
for (HostedZone zone : page.items()) {
|
||||
xml.raw(xmlHostedZone(zone));
|
||||
}
|
||||
xml.end("HostedZones")
|
||||
.elem("Marker", marker != null ? marker : "")
|
||||
.elem("IsTruncated", String.valueOf(truncated));
|
||||
if (truncated) {
|
||||
xml.elem("NextMarker", remaining.get(maxItems).getId());
|
||||
.elem("IsTruncated", String.valueOf(page.isTruncated()));
|
||||
if (page.isTruncated()) {
|
||||
xml.elem("NextMarker", page.nextMarker());
|
||||
}
|
||||
xml.elem("MaxItems", String.valueOf(maxItems))
|
||||
.end("ListHostedZonesResponse");
|
||||
@@ -512,19 +509,22 @@ public class Route53Controller {
|
||||
public Response listHealthChecks(@QueryParam("marker") String marker,
|
||||
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
|
||||
try {
|
||||
List<HealthCheck> checks = service.listHealthChecks(marker, maxItems);
|
||||
boolean truncated = checks.size() == maxItems;
|
||||
int pageSize = Math.min(maxItems, 1000);
|
||||
Route53Pagination.Page<HealthCheck> page = service.pageHealthChecks(marker, pageSize);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("ListHealthChecksResponse", NS)
|
||||
.start("HealthChecks");
|
||||
for (HealthCheck hc : checks) {
|
||||
for (HealthCheck hc : page.items()) {
|
||||
xml.raw(xmlHealthCheck(hc));
|
||||
}
|
||||
xml.end("HealthChecks")
|
||||
.elem("Marker", marker != null ? marker : "")
|
||||
.elem("IsTruncated", String.valueOf(truncated))
|
||||
.elem("MaxItems", String.valueOf(maxItems))
|
||||
.elem("IsTruncated", String.valueOf(page.isTruncated()));
|
||||
if (page.isTruncated()) {
|
||||
xml.elem("NextMarker", page.nextMarker());
|
||||
}
|
||||
xml.elem("MaxItems", String.valueOf(maxItems))
|
||||
.end("ListHealthChecksResponse");
|
||||
|
||||
return Response.ok(xml.build(), XML).build();
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package io.github.hectorvent.floci.services.route53;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
final class Route53Pagination {
|
||||
|
||||
private Route53Pagination() {
|
||||
}
|
||||
|
||||
static <T> Page<T> page(List<T> sortedItems, String marker, int pageSize, Function<T, String> id) {
|
||||
int start = 0;
|
||||
if (marker != null && !marker.isEmpty()) {
|
||||
for (int i = 0; i < sortedItems.size(); i++) {
|
||||
if (id.apply(sortedItems.get(i)).equals(marker)) {
|
||||
start = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int remaining = sortedItems.size() - start;
|
||||
if (pageSize <= 0 || remaining <= pageSize) {
|
||||
return new Page<>(sortedItems.subList(start, sortedItems.size()), null);
|
||||
}
|
||||
int end = start + pageSize;
|
||||
return new Page<>(sortedItems.subList(start, end), id.apply(sortedItems.get(end)));
|
||||
}
|
||||
|
||||
record Page<T>(List<T> items, String nextMarker) {
|
||||
boolean isTruncated() {
|
||||
return nextMarker != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,25 +163,16 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
public List<HostedZone> listHostedZones(String marker, int maxItems) {
|
||||
return pageHostedZones(marker, maxItems).items();
|
||||
}
|
||||
|
||||
Route53Pagination.Page<HostedZone> pageHostedZones(String marker, int maxItems) {
|
||||
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
|
||||
all.sort(Comparator.comparing(HostedZone::getName).thenComparing(HostedZone::getId));
|
||||
for (HostedZone zone : all) {
|
||||
zone.setResourceRecordSetCount(recordCount(zone.getId()));
|
||||
}
|
||||
if (marker != null && !marker.isEmpty()) {
|
||||
int idx = 0;
|
||||
for (int i = 0; i < all.size(); i++) {
|
||||
if (all.get(i).getId().equals(marker)) {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
all = all.subList(idx, all.size());
|
||||
}
|
||||
if (maxItems > 0 && all.size() > maxItems) {
|
||||
return all.subList(0, maxItems);
|
||||
}
|
||||
return all;
|
||||
return Route53Pagination.page(all, marker, maxItems, HostedZone::getId);
|
||||
}
|
||||
|
||||
public List<HostedZone> listHostedZonesByName(String dnsName, int maxItems) {
|
||||
@@ -504,21 +495,13 @@ public class Route53Service implements Resettable {
|
||||
}
|
||||
|
||||
public List<HealthCheck> listHealthChecks(String marker, int maxItems) {
|
||||
return pageHealthChecks(marker, maxItems).items();
|
||||
}
|
||||
|
||||
Route53Pagination.Page<HealthCheck> pageHealthChecks(String marker, int maxItems) {
|
||||
List<HealthCheck> all = new ArrayList<>(healthCheckStore.scan(k -> true));
|
||||
if (marker != null && !marker.isEmpty()) {
|
||||
int idx = 0;
|
||||
for (int i = 0; i < all.size(); i++) {
|
||||
if (all.get(i).getId().equals(marker)) {
|
||||
idx = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
all = all.subList(idx, all.size());
|
||||
}
|
||||
if (maxItems > 0 && all.size() > maxItems) {
|
||||
return all.subList(0, maxItems);
|
||||
}
|
||||
return all;
|
||||
all.sort(Comparator.comparing(HealthCheck::getId));
|
||||
return Route53Pagination.page(all, marker, maxItems, HealthCheck::getId);
|
||||
}
|
||||
|
||||
public HealthCheck updateHealthCheck(String id, HealthCheckConfig cfg) {
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package io.github.hectorvent.floci.services.route53;
|
||||
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheck;
|
||||
import io.github.hectorvent.floci.services.route53.model.HealthCheckConfig;
|
||||
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.ListHealthChecksResponse;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
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 Route53HealthChecksPaginationIntegrationTest {
|
||||
|
||||
@Inject
|
||||
Route53Service service;
|
||||
|
||||
@Test
|
||||
void listHealthChecksUsesTheMarkerItemAndNextMarker() {
|
||||
List<String> createdIds = createHealthChecks(2);
|
||||
try {
|
||||
List<HealthCheck> allChecks = service.listHealthChecks(null, Integer.MAX_VALUE);
|
||||
assertTrue(allChecks.size() >= 2);
|
||||
String marker = allChecks.get(allChecks.size() - 2).getId();
|
||||
String lastId = allChecks.getLast().getId();
|
||||
|
||||
XmlPath penultimatePage = given()
|
||||
.queryParam("marker", marker)
|
||||
.queryParam("maxitems", 1)
|
||||
.when().get("/2013-04-01/healthcheck")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath();
|
||||
|
||||
assertEquals(marker,
|
||||
penultimatePage.getString("ListHealthChecksResponse.HealthChecks.HealthCheck.Id"));
|
||||
assertEquals(marker, penultimatePage.getString("ListHealthChecksResponse.Marker"));
|
||||
assertTrue(penultimatePage.getBoolean("ListHealthChecksResponse.IsTruncated"));
|
||||
assertEquals(lastId, penultimatePage.getString("ListHealthChecksResponse.NextMarker"));
|
||||
|
||||
String lastPageBody = given()
|
||||
.queryParam("marker", lastId)
|
||||
.queryParam("maxitems", 1)
|
||||
.when().get("/2013-04-01/healthcheck")
|
||||
.then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
XmlPath lastPage = new XmlPath(lastPageBody);
|
||||
|
||||
assertEquals(lastId,
|
||||
lastPage.getString("ListHealthChecksResponse.HealthChecks.HealthCheck.Id"));
|
||||
assertEquals(lastId, lastPage.getString("ListHealthChecksResponse.Marker"));
|
||||
assertFalse(lastPage.getBoolean("ListHealthChecksResponse.IsTruncated"));
|
||||
assertFalse(lastPageBody.contains("<NextMarker>"));
|
||||
} finally {
|
||||
createdIds.forEach(service::deleteHealthCheck);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsSdkPaginatesListHealthChecks() {
|
||||
List<String> createdIds = createHealthChecks(2);
|
||||
try {
|
||||
List<String> expected = service.listHealthChecks(null, Integer.MAX_VALUE).stream()
|
||||
.map(HealthCheck::getId)
|
||||
.toList();
|
||||
assertEquals(expected.stream().sorted().toList(), expected);
|
||||
try (Route53Client client = AwsTestClients.create(Route53Client.builder(), "test", Region.US_EAST_1)) {
|
||||
List<ListHealthChecksResponse> pages = client.listHealthChecksPaginator(request -> request.maxItems("1"))
|
||||
.stream().toList();
|
||||
assertEquals(expected.size(), pages.stream().mapToInt(page -> page.healthChecks().size()).sum());
|
||||
for (int i = 0; i < pages.size(); i++) {
|
||||
ListHealthChecksResponse page = pages.get(i);
|
||||
boolean hasNext = i + 1 < pages.size();
|
||||
assertEquals(1, page.healthChecks().size());
|
||||
assertEquals(expected.get(i), page.healthChecks().getFirst().id());
|
||||
assertEquals(hasNext, page.isTruncated());
|
||||
assertEquals(hasNext ? expected.get(i + 1) : null, page.nextMarker());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
createdIds.forEach(service::deleteHealthCheck);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listHealthChecksCapsOversizedPagesAtOneThousand() {
|
||||
List<String> createdIds = createHealthChecks(1001);
|
||||
try {
|
||||
List<String> expected = service.listHealthChecks(null, Integer.MAX_VALUE).stream()
|
||||
.map(HealthCheck::getId)
|
||||
.toList();
|
||||
XmlPath page = given()
|
||||
.queryParam("maxitems", 2000)
|
||||
.when().get("/2013-04-01/healthcheck")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath();
|
||||
|
||||
List<String> returnedIds = page.getList("ListHealthChecksResponse.HealthChecks.HealthCheck.Id");
|
||||
assertEquals(1000, returnedIds.size());
|
||||
IntStream.range(0, 1000).forEach(index ->
|
||||
assertEquals(expected.get(index), returnedIds.get(index), "health check at index " + index));
|
||||
assertTrue(page.getBoolean("ListHealthChecksResponse.IsTruncated"));
|
||||
assertEquals(expected.get(1000), page.getString("ListHealthChecksResponse.NextMarker"));
|
||||
assertEquals("2000", page.getString("ListHealthChecksResponse.MaxItems"));
|
||||
} finally {
|
||||
createdIds.forEach(service::deleteHealthCheck);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> createHealthChecks(int count) {
|
||||
HealthCheckConfig config = new HealthCheckConfig();
|
||||
config.setType("HTTPS");
|
||||
config.setFullyQualifiedDomainName("example.com");
|
||||
config.setPort(443);
|
||||
config.setResourcePath("/health");
|
||||
config.setRequestInterval(30);
|
||||
config.setFailureThreshold(3);
|
||||
return IntStream.range(0, count)
|
||||
.mapToObj(index -> service.createHealthCheck(
|
||||
"pagination-" + System.nanoTime() + "-" + index,
|
||||
config,
|
||||
"ResourcePath=/health"))
|
||||
.map(HealthCheck::getId)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package io.github.hectorvent.floci.services.route53;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class Route53PaginationTest {
|
||||
|
||||
private record Item(String id) {
|
||||
}
|
||||
|
||||
private static final List<Item> SORTED = List.of(new Item("A"), new Item("B"), new Item("C"));
|
||||
|
||||
@Test
|
||||
void markerIsInclusiveAndNextMarkerStartsTheFollowingPage() {
|
||||
Route53Pagination.Page<Item> first = Route53Pagination.page(SORTED, "A", 2, Item::id);
|
||||
assertEquals(List.of(SORTED.get(0), SORTED.get(1)), first.items());
|
||||
assertTrue(first.isTruncated());
|
||||
assertEquals("C", first.nextMarker());
|
||||
|
||||
Route53Pagination.Page<Item> last = Route53Pagination.page(SORTED, first.nextMarker(), 2, Item::id);
|
||||
assertEquals(List.of(SORTED.get(2)), last.items());
|
||||
assertFalse(last.isTruncated());
|
||||
assertNull(last.nextMarker());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactPageAndEmptyInputAreNotTruncated() {
|
||||
Route53Pagination.Page<Item> exact = Route53Pagination.page(SORTED, "B", 2, Item::id);
|
||||
assertEquals(List.of(SORTED.get(1), SORTED.get(2)), exact.items());
|
||||
assertFalse(exact.isTruncated());
|
||||
|
||||
Route53Pagination.Page<Item> empty = Route53Pagination.page(List.<Item>of(), null, 2, Item::id);
|
||||
assertEquals(List.of(), empty.items());
|
||||
assertFalse(empty.isTruncated());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownMarkerAndNonpositiveSizeKeepExistingBehavior() {
|
||||
Route53Pagination.Page<Item> unknown = Route53Pagination.page(SORTED, "missing", 2, Item::id);
|
||||
assertEquals(List.of(SORTED.get(0), SORTED.get(1)), unknown.items());
|
||||
assertEquals("C", unknown.nextMarker());
|
||||
|
||||
Route53Pagination.Page<Item> unlimited = Route53Pagination.page(SORTED, "B", 0, Item::id);
|
||||
assertEquals(List.of(SORTED.get(1), SORTED.get(2)), unlimited.items());
|
||||
assertFalse(unlimited.isTruncated());
|
||||
|
||||
Route53Pagination.Page<Item> negative = Route53Pagination.page(SORTED, "B", -1, Item::id);
|
||||
assertEquals(unlimited, negative);
|
||||
}
|
||||
|
||||
@Test
|
||||
void traversesTheSuppliedSortOrderWithoutAssumingIdOrder() {
|
||||
List<Item> byZoneName = List.of(new Item("Z"), new Item("A"), new Item("M"));
|
||||
Route53Pagination.Page<Item> page = Route53Pagination.page(byZoneName, "A", 1, Item::id);
|
||||
assertEquals(List.of(byZoneName.get(1)), page.items());
|
||||
assertEquals("M", page.nextMarker());
|
||||
}
|
||||
|
||||
@Test
|
||||
void maximumPageSizeDoesNotOverflow() {
|
||||
Route53Pagination.Page<Item> page = Route53Pagination.page(SORTED, null, Integer.MAX_VALUE, Item::id);
|
||||
assertEquals(SORTED, page.items());
|
||||
assertFalse(page.isTruncated());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user