mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudmap): paginate namespace service and operation lists (#6629)
* fix(cloudmap): paginate namespace service and operation lists * refactor(core): share bounded pagination validation * refactor(cloudmap): reuse shared pagination checks for cloud map and bcm
This commit is contained in:
1 parent
9b36aa6980
commit
a1ebc10bd1
9 files changed
+553
-47
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
@@ -71,6 +73,45 @@ public final class Pagination {
|
||||
return slice(all, cursorOf, limit, decodeCursor(nextToken, prefix, invalidToken), prefix);
|
||||
}
|
||||
|
||||
/** Validates service-specific bounds before applying the existing namespaced cursor format. */
|
||||
public static <T> PaginatedResult<T> paginate(List<T> all, Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken,
|
||||
int defaultPageSize, int maxResultsLimit, int maxTokenLength,
|
||||
String errorCode, String namespace) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maxResultsLimit)) {
|
||||
throw new AwsException(errorCode, "Page size must be between 1 and " + maxResultsLimit + ".", 400);
|
||||
}
|
||||
if (nextToken != null && nextToken.length() > maxTokenLength) {
|
||||
throw new AwsException(errorCode, "Pagination token must not exceed " + maxTokenLength + " characters.", 400);
|
||||
}
|
||||
return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,
|
||||
token -> new AwsException(errorCode, "Invalid pagination token.", 400));
|
||||
}
|
||||
|
||||
/** Reads an optional JSON page size without coercing strings, fractions, or overflowing integers. */
|
||||
public static Integer parseMaxResults(JsonNode request, String field, String errorCode) {
|
||||
JsonNode value = request.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isIntegralNumber() || !value.canConvertToInt()) {
|
||||
throw new AwsException(errorCode, field + " must be an integer.", 400);
|
||||
}
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
/** Reads an optional JSON cursor; bounds and scope are checked by the namespaced pager. */
|
||||
public static String parseNextToken(JsonNode request, String field, String errorCode) {
|
||||
JsonNode value = request.get(field);
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isTextual()) {
|
||||
throw new AwsException(errorCode, field + " must be a string.", 400);
|
||||
}
|
||||
return value.textValue();
|
||||
}
|
||||
|
||||
private static <T> PaginatedResult<T> slice(List<T> all, Function<T, String> cursorOf, int limit,
|
||||
String after, String tokenPrefix) {
|
||||
List<T> sorted = all.stream().sorted(Comparator.comparing(cursorOf)).collect(Collectors.toList());
|
||||
|
||||
+7
-24
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.services.bcmdataexports.model.DataQuery;
|
||||
import io.github.hectorvent.floci.services.bcmdataexports.model.DestinationConfiguration;
|
||||
import io.github.hectorvent.floci.services.bcmdataexports.model.Export;
|
||||
@@ -86,7 +87,9 @@ public class BcmDataExportsJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleList(JsonNode request) {
|
||||
PaginatedResult<Export> page = service.listExports(maxResults(request), nextToken(request));
|
||||
PaginatedResult<Export> page = service.listExports(
|
||||
Pagination.parseMaxResults(request, "MaxResults", "ValidationException"),
|
||||
Pagination.parseNextToken(request, "NextToken", "ValidationException"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Exports");
|
||||
for (Export export : page.items()) {
|
||||
@@ -122,7 +125,9 @@ public class BcmDataExportsJsonHandler {
|
||||
|
||||
private Response handleListExecutions(JsonNode request) {
|
||||
String arn = stringOrNull(request, "ExportArn");
|
||||
PaginatedResult<ExportExecution> page = service.listExecutions(arn, maxResults(request), nextToken(request));
|
||||
PaginatedResult<ExportExecution> page = service.listExecutions(arn,
|
||||
Pagination.parseMaxResults(request, "MaxResults", "ValidationException"),
|
||||
Pagination.parseNextToken(request, "NextToken", "ValidationException"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Executions");
|
||||
for (ExportExecution exec : page.items()) {
|
||||
@@ -134,28 +139,6 @@ public class BcmDataExportsJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static Integer maxResults(JsonNode request) {
|
||||
JsonNode value = request.get("MaxResults");
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isIntegralNumber() || !value.canConvertToInt()) {
|
||||
throw new AwsException("ValidationException", "MaxResults must be an integer between 1 and 300.", 400);
|
||||
}
|
||||
return value.intValue();
|
||||
}
|
||||
|
||||
private static String nextToken(JsonNode request) {
|
||||
JsonNode value = request.get("NextToken");
|
||||
if (value == null || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isTextual()) {
|
||||
throw new AwsException("ValidationException", "NextToken must be a string.", 400);
|
||||
}
|
||||
return value.textValue();
|
||||
}
|
||||
|
||||
private Response handleGetExecution(JsonNode request) {
|
||||
String arn = stringOrNull(request, "ExportArn");
|
||||
String executionId = stringOrNull(request, "ExecutionId");
|
||||
|
||||
+5
-17
@@ -26,7 +26,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -118,7 +117,8 @@ public class BcmDataExportsService {
|
||||
|
||||
/** Returns one account-scoped page for {@code ListExports}. */
|
||||
public PaginatedResult<Export> listExports(Integer maxResults, String nextToken) {
|
||||
return paginate(exportStore.scan(key -> true), Export::getExportArn, maxResults, nextToken,
|
||||
return Pagination.paginate(exportStore.scan(key -> true), Export::getExportArn, maxResults, nextToken,
|
||||
MAX_PAGE_SIZE, MAX_PAGE_SIZE, 8192, "ValidationException",
|
||||
"ListExports:" + regionResolver.getAccountId());
|
||||
}
|
||||
|
||||
@@ -199,21 +199,9 @@ public class BcmDataExportsService {
|
||||
"Export " + exportArn + " not found.", 400);
|
||||
}
|
||||
String prefix = exportArn + "::";
|
||||
return paginate(executionStore.scan(key -> key.startsWith(prefix)), ExportExecution::getExecutionId,
|
||||
maxResults, nextToken, "ListExecutions:" + regionResolver.getAccountId() + ":" + exportArn);
|
||||
}
|
||||
|
||||
private static <T> PaginatedResult<T> paginate(List<T> items, Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken, String namespace) {
|
||||
int limit = maxResults != null ? maxResults : MAX_PAGE_SIZE;
|
||||
if (limit < 1 || limit > MAX_PAGE_SIZE) {
|
||||
throw new AwsException("ValidationException", "MaxResults must be between 1 and 300.", 400);
|
||||
}
|
||||
if (nextToken != null && nextToken.length() > 8192) {
|
||||
throw new AwsException("ValidationException", "NextToken must not exceed 8192 characters.", 400);
|
||||
}
|
||||
return Pagination.paginate(items, cursorOf, limit, nextToken, namespace,
|
||||
token -> new AwsException("ValidationException", "Invalid NextToken.", 400));
|
||||
return Pagination.paginate(executionStore.scan(key -> key.startsWith(prefix)), ExportExecution::getExecutionId,
|
||||
maxResults, nextToken, MAX_PAGE_SIZE, MAX_PAGE_SIZE, 8192, "ValidationException",
|
||||
"ListExecutions:" + regionResolver.getAccountId() + ":" + exportArn);
|
||||
}
|
||||
|
||||
/** {@code GetExecution} — returns one execution record. */
|
||||
|
||||
@@ -7,6 +7,8 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsJson11Controller;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Instance;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Namespace;
|
||||
import io.github.hectorvent.floci.services.cloudmap.model.Operation;
|
||||
@@ -53,7 +55,7 @@ public class CloudMapHandler {
|
||||
text(request, "Name"), text(request, "Vpc"), text(request, "CreatorRequestId"),
|
||||
text(request, "Description"), parseTags(request), region));
|
||||
case "GetNamespace" -> handleGetNamespace(request);
|
||||
case "ListNamespaces" -> handleListNamespaces(region);
|
||||
case "ListNamespaces" -> handleListNamespaces(request, region);
|
||||
case "DeleteNamespace" -> opResponse(service.deleteNamespace(text(request, "Id"), region));
|
||||
case "GetOperation" -> handleGetOperation(request);
|
||||
case "ListOperations" -> handleListOperations(request, region);
|
||||
@@ -100,12 +102,18 @@ public class CloudMapHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleListNamespaces(String region) {
|
||||
private Response handleListNamespaces(JsonNode request, String region) {
|
||||
PaginatedResult<Namespace> page = service.listNamespaces(region,
|
||||
Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"),
|
||||
Pagination.parseNextToken(request, "NextToken", "InvalidInput"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Namespaces");
|
||||
for (Namespace ns : service.listNamespaces(region)) {
|
||||
for (Namespace ns : page.items()) {
|
||||
arr.add(buildNamespaceNode(ns));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -120,14 +128,20 @@ public class CloudMapHandler {
|
||||
|
||||
private Response handleListOperations(JsonNode request, String region) {
|
||||
Map<String, List<String>> filters = parseFilters(request);
|
||||
PaginatedResult<Operation> page = service.listOperations(
|
||||
region, filters, Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"),
|
||||
Pagination.parseNextToken(request, "NextToken", "InvalidInput"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Operations");
|
||||
for (Operation op : service.listOperations(region, filters)) {
|
||||
for (Operation op : page.items()) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Id", op.getId());
|
||||
node.put("Status", op.getStatus());
|
||||
arr.add(node);
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -155,11 +169,17 @@ public class CloudMapHandler {
|
||||
Map<String, List<String>> filters = parseFilters(request);
|
||||
String namespaceId = filters.containsKey("NAMESPACE_ID") && !filters.get("NAMESPACE_ID").isEmpty()
|
||||
? filters.get("NAMESPACE_ID").get(0) : null;
|
||||
PaginatedResult<Service> page = service.listServices(
|
||||
region, namespaceId, Pagination.parseMaxResults(request, "MaxResults", "InvalidInput"),
|
||||
Pagination.parseNextToken(request, "NextToken", "InvalidInput"));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode arr = response.putArray("Services");
|
||||
for (Service s : service.listServices(region, namespaceId)) {
|
||||
for (Service s : page.items()) {
|
||||
arr.add(buildServiceSummaryNode(s));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -426,4 +446,4 @@ public class CloudMapHandler {
|
||||
LOG.warnv("Failed to parse stored {0} JSON; field omitted. Value: {1}", field, rawJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsAnswer;
|
||||
import io.github.hectorvent.floci.core.common.dns.DnsRecord;
|
||||
@@ -35,11 +38,13 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Cloud Map (AWS service discovery) business logic. Mirrors the EventBridge service
|
||||
@@ -166,6 +171,11 @@ public class CloudMapService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public PaginatedResult<Namespace> listNamespaces(String region, Integer maxResults, String nextToken) {
|
||||
return paginate(listNamespaces(region), Namespace::getId, maxResults, nextToken,
|
||||
"ListNamespaces", region, null);
|
||||
}
|
||||
|
||||
public void updateNamespace(String id, String description) {
|
||||
Namespace ns = requireNamespace(id);
|
||||
ns.setDescription(description);
|
||||
@@ -256,6 +266,16 @@ public class CloudMapService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public PaginatedResult<Service> listServices(String region, String namespaceIdFilter,
|
||||
Integer maxResults, String nextToken) {
|
||||
PaginatedResult<Service> page = paginate(listServices(region, null), Service::getId,
|
||||
maxResults, nextToken, "ListServices", region, namespaceIdFilter);
|
||||
// AWS applies filters after selecting the page, so an empty page can still have a token.
|
||||
List<Service> items = page.items().stream()
|
||||
.filter(s -> namespaceIdFilter == null || namespaceIdFilter.equals(s.getNamespaceId())).toList();
|
||||
return new PaginatedResult<>(items, page.nextToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Only the TTLs of the existing DNS records can change, as on AWS: any other DnsConfig change
|
||||
* (record types or count, RoutingPolicy), or a DnsConfig for a service without one, is rejected.
|
||||
@@ -728,6 +748,26 @@ public class CloudMapService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public PaginatedResult<Operation> listOperations(String region, Map<String, List<String>> filters,
|
||||
Integer maxResults, String nextToken) {
|
||||
Map<String, List<String>> normalized = new TreeMap<>();
|
||||
if (filters != null) {
|
||||
filters.forEach((name, values) -> normalized.put(name, values.stream().distinct().sorted().toList()));
|
||||
}
|
||||
PaginatedResult<Operation> page = paginate(listOperations(region, null), Operation::getId,
|
||||
maxResults, nextToken, "ListOperations", region, normalized);
|
||||
List<Operation> items = page.items().stream().filter(op -> matchesOperationFilters(op, filters)).toList();
|
||||
return new PaginatedResult<>(items, page.nextToken());
|
||||
}
|
||||
|
||||
private <T> PaginatedResult<T> paginate(List<T> items, Function<T, String> cursorOf,
|
||||
Integer maxResults, String nextToken,
|
||||
String action, String region, Object query) {
|
||||
String namespace = action + ":" + regionResolver.getAccountId() + ":" + region + ":"
|
||||
+ Digests.sha256Hex(objectMapper.valueToTree(query).toString());
|
||||
return Pagination.paginate(items, cursorOf, maxResults, nextToken, 100, 100, 4096, "InvalidInput", namespace);
|
||||
}
|
||||
|
||||
private boolean matchesOperationFilters(Operation op, Map<String, List<String>> filters) {
|
||||
if (filters == null || filters.isEmpty()) {
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user