mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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) {
|
||||
|
||||
Reference in new issue
Block a user