fix(route53): paginate hosted zones by name (#6997)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-30 07:23:30 -04:00
1 parent 07f2d62c17
commit fb1d2d1fdb
5 files changed
+195 -17

No files matched your search

@@ -25,6 +25,7 @@ import jakarta.ws.rs.core.Response;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.net.URI;
import java.time.Instant;
import java.util.ArrayList;
@@ -169,24 +170,33 @@ public class Route53Controller {
@GET
@Path("/hostedzonesbyname")
public Response listHostedZonesByName(@QueryParam("dnsname") String dnsName,
@QueryParam("hostedzoneid") String hostedZoneId,
@QueryParam("maxitems") @DefaultValue("100") int maxItems) {
try {
List<HostedZone> zones = service.listHostedZonesByName(dnsName, maxItems);
long total = service.getHostedZoneCount();
boolean truncated = zones.size() == maxItems && zones.size() < total;
if (maxItems <= 0) {
throw new AwsException("InvalidInput", "MaxItems must be a positive integer.", 400);
}
Route53Pagination.Page<HostedZone> page = service.pageHostedZonesByName(dnsName, hostedZoneId, maxItems);
XmlBuilder xml = new XmlBuilder()
.start("ListHostedZonesByNameResponse", NS)
.start("HostedZones");
for (HostedZone zone : zones) {
for (HostedZone zone : page.items()) {
xml.raw(xmlHostedZone(zone));
}
xml.end("HostedZones")
.elem("IsTruncated", String.valueOf(truncated))
.elem("IsTruncated", String.valueOf(page.isTruncated()))
.elem("MaxItems", String.valueOf(maxItems));
if (dnsName != null && !dnsName.isEmpty()) {
xml.elem("DNSName", dnsName);
}
if (hostedZoneId != null && !hostedZoneId.isEmpty()) {
xml.elem("HostedZoneId", hostedZoneId);
}
if (page.isTruncated()) {
xml.elem("NextDNSName", page.nextItem().getName())
.elem("NextHostedZoneId", page.nextMarker());
}
xml.end("ListHostedZonesByNameResponse");
return Response.ok(xml.build(), XML).build();
@@ -21,13 +21,14 @@ final class Route53Pagination {
int remaining = sortedItems.size() - start;
if (pageSize <= 0 || remaining <= pageSize) {
return new Page<>(sortedItems.subList(start, sortedItems.size()), null);
return new Page<>(sortedItems.subList(start, sortedItems.size()), null, null);
}
int end = start + pageSize;
return new Page<>(sortedItems.subList(start, end), id.apply(sortedItems.get(end)));
T nextItem = sortedItems.get(end);
return new Page<>(sortedItems.subList(start, end), id.apply(nextItem), nextItem);
}
record Page<T>(List<T> items, String nextMarker) {
record Page<T>(List<T> items, String nextMarker, T nextItem) {
boolean isTruncated() {
return nextMarker != null;
}
@@ -176,22 +176,52 @@ public class Route53Service implements Resettable {
}
public List<HostedZone> listHostedZonesByName(String dnsName, int maxItems) {
return pageHostedZonesByName(dnsName, null, maxItems).items();
}
Route53Pagination.Page<HostedZone> pageHostedZonesByName(String dnsName, String hostedZoneId, int maxItems) {
List<HostedZone> all = new ArrayList<>(zoneStore.scan(k -> true));
all.sort((a, b) -> a.getName().compareTo(b.getName()));
all.sort(Comparator.comparing((HostedZone zone) -> reversedDnsName(zone.getName()))
.thenComparing(HostedZone::getId));
for (HostedZone zone : all) {
zone.setResourceRecordSetCount(recordCount(zone.getId()));
}
if (dnsName != null && !dnsName.isEmpty()) {
String normalized = normalizeName(dnsName);
all = all.stream()
.filter(z -> z.getName().compareTo(normalized) >= 0)
.toList();
all = new ArrayList<>(all);
String startName = reversedDnsName(normalizeName(dnsName));
all.removeIf(zone -> {
int comparison = reversedDnsName(zone.getName()).compareTo(startName);
return comparison < 0 || (comparison == 0 && hostedZoneId != null && !hostedZoneId.isEmpty()
&& zone.getId().compareTo(hostedZoneId) < 0);
});
}
if (maxItems > 0 && all.size() > maxItems) {
return all.subList(0, maxItems);
int pageSize = maxItems <= 0 ? maxItems : Math.min(maxItems, 100);
return Route53Pagination.page(all, null, pageSize, HostedZone::getId);
}
static String reversedDnsName(String name) {
String normalized = normalizeName(name);
List<String> labels = new ArrayList<>();
int labelStart = 0;
boolean escaped = false;
for (int i = 0; i < normalized.length(); i++) {
char character = normalized.charAt(i);
if (character == '.' && !escaped) {
if (i > labelStart) {
labels.add(normalized.substring(labelStart, i));
}
labelStart = i + 1;
}
if (character == '\\' && !escaped) {
escaped = true;
} else {
escaped = false;
}
}
return all;
StringBuilder reversed = new StringBuilder(normalized.length());
for (int i = labels.size() - 1; i >= 0; i--) {
reversed.append(labels.get(i)).append('.');
}
return reversed.toString();
}
public long getHostedZoneCount() {