mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(eventbridge): page ListRules with Limit and NextToken (#6223)
ListRules ignored Limit and NextToken and always returned every matching rule. It now pages through the bus's rules in name order using the shared Pagination cursor, keeping NamePrefix and EventBusName filtering intact. - Limit defaults to 100, the documented per-page maximum; an integral value outside 1 to 100, including one wider than an int, is a ValidationException. - A string, boolean or fractional Limit is a SerializationException rather than being coerced into a page size. - NextToken is returned only when more rules remain. - A token Floci did not mint is rejected with HTTP 400 InvalidToken, as the API reference documents for a bad pagination token. Fixes #6220
This commit is contained in:
1 parent
71cc704319
commit
007ac9268e
6 files changed
+330
-3
No files matched your search
+27
-2
@@ -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.AwsJson11Controller;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.ApiDestination;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Archive;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.ArchiveState;
|
||||
@@ -193,15 +194,39 @@ public class EventBridgeHandler {
|
||||
private Response handleListRules(JsonNode request, String region) {
|
||||
String busName = request.path("EventBusName").asText(null);
|
||||
String namePrefix = request.path("NamePrefix").asText(null);
|
||||
List<Rule> rules = eventBridgeService.listRules(busName, namePrefix, region);
|
||||
Integer limit = readLimit(request.path("Limit"));
|
||||
String nextToken = request.path("NextToken").asText(null);
|
||||
PaginatedResult<Rule> page = eventBridgeService.listRules(busName, namePrefix, limit, nextToken, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode rulesArray = response.putArray("Rules");
|
||||
for (Rule rule : rules) {
|
||||
for (Rule rule : page.items()) {
|
||||
rulesArray.add(buildRuleNode(rule));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an integer {@code Limit} without coercion: a string, boolean or fractional value is
|
||||
* a SerializationException, and an integral value wider than an int fails the range check
|
||||
* instead of being truncated into it.
|
||||
*/
|
||||
private static Integer readLimit(JsonNode limit) {
|
||||
if (limit.isMissingNode() || limit.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!limit.isIntegralNumber()) {
|
||||
throw new AwsException("SerializationException",
|
||||
"'" + limit.asText() + "' can not be converted to Integer", 400);
|
||||
}
|
||||
if (!limit.canConvertToInt()) {
|
||||
throw EventBridgeService.limitOutOfRange(limit.asText(), limit.bigIntegerValue().signum() < 0);
|
||||
}
|
||||
return limit.intValue();
|
||||
}
|
||||
|
||||
private Response handleEnableRule(JsonNode request, String region) {
|
||||
String name = request.path("Name").asText(null);
|
||||
String busName = request.path("EventBusName").asText(null);
|
||||
|
||||
@@ -13,6 +13,8 @@ import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
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.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
@@ -58,6 +60,7 @@ import java.util.regex.Pattern;
|
||||
public class EventBridgeService implements ResourceProvider {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(EventBridgeService.class);
|
||||
private static final int MAX_LIST_LIMIT = 100;
|
||||
private static final List<String> REQUIRED_ENTRY_FIELDS = List.of("Source", "DetailType", "Detail");
|
||||
|
||||
private final StorageBackend<String, EventBus> busStore;
|
||||
@@ -399,6 +402,29 @@ public class EventBridgeService implements ResourceProvider {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* One page of a bus's rules in name order. {@code limit} defaults to 100, the documented
|
||||
* per-page maximum, and the token is the shared opaque cursor, so a page stays resumable
|
||||
* when rules are added or removed between requests.
|
||||
*/
|
||||
public PaginatedResult<Rule> listRules(String busName, String namePrefix, Integer limit,
|
||||
String nextToken, String region) {
|
||||
if (limit != null && (limit < 1 || limit > MAX_LIST_LIMIT)) {
|
||||
throw limitOutOfRange(limit.toString(), limit < 1);
|
||||
}
|
||||
return Pagination.paginate(listRules(busName, namePrefix, region), Rule::getName,
|
||||
limit != null ? limit : MAX_LIST_LIMIT, nextToken, "rule",
|
||||
token -> new AwsException("InvalidToken", "Invalid NextToken: " + token, 400));
|
||||
}
|
||||
|
||||
static AwsException limitOutOfRange(String value, boolean belowMinimum) {
|
||||
String constraint = belowMinimum
|
||||
? "Member must have value greater than or equal to 1"
|
||||
: "Member must have value less than or equal to " + MAX_LIST_LIMIT;
|
||||
return SmithyValidationErrors.validationException(
|
||||
"Value '" + value + "' at 'limit' failed to satisfy constraint: " + constraint);
|
||||
}
|
||||
|
||||
public void enableRule(String name, String busName, String region) {
|
||||
String effectiveBus = resolvedBusName(busName);
|
||||
String key = ruleKey(region, effectiveBus, name);
|
||||
|
||||
Reference in new issue
Block a user