mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(scheduler): honor MaxResults and NextToken in ListScheduleGroups and ListSchedules (#7187)
Both list operations now page through the shared Pagination helper, return NextToken only while items remain, and answer an out-of-range MaxResults or a foreign or malformed NextToken with ValidationException. NamePrefix, State and ScheduleGroup filtering apply before paging. Fixes #6720 Let the namespaced Pagination overload take the reject-empty-token flag that paginateUnencoded already uses, and drop SchedulerService.requireNonEmptyToken. Explain why the compatibility test's best-effort cleanup ignores failures.
This commit is contained in:
1 parent
b93afa9e89
commit
4e5c60cd58
8 files changed
+340
-11
No files matched your search
@@ -142,8 +142,22 @@ public final class Pagination {
|
||||
Integer maxResults, String nextToken,
|
||||
int defaultPageSize, int maxResultsLimit, int maxTokenLength,
|
||||
String errorCode, String namespace) {
|
||||
return paginate(all, cursorOf, maxResults, nextToken, defaultPageSize, maxResultsLimit, maxTokenLength,
|
||||
errorCode, namespace, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above; when {@code tokenMustBeNonEmpty} is set, an empty token is refused instead of being
|
||||
* read as the first page, for operations whose model gives the token a minimum length of 1.
|
||||
*/
|
||||
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, boolean tokenMustBeNonEmpty) {
|
||||
validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,
|
||||
false, "Pagination token must not exceed " + maxTokenLength + " characters.");
|
||||
tokenMustBeNonEmpty, nextToken != null && nextToken.isEmpty()
|
||||
? tokenRangeMessage(maxTokenLength)
|
||||
: "Pagination token must not exceed " + maxTokenLength + " characters.");
|
||||
return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,
|
||||
token -> new AwsException(errorCode, "Invalid pagination token.", 400));
|
||||
}
|
||||
@@ -158,7 +172,7 @@ public final class Pagination {
|
||||
int defaultPageSize, int maxResultsLimit,
|
||||
int maxTokenLength, String errorCode) {
|
||||
validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,
|
||||
true, "Pagination token must be between 1 and " + maxTokenLength + " characters.");
|
||||
true, tokenRangeMessage(maxTokenLength));
|
||||
int limit = maxResults == null ? defaultPageSize : maxResults;
|
||||
return slice(all, cursorOf, limit, nextToken, "", Function.identity());
|
||||
}
|
||||
@@ -176,6 +190,10 @@ public final class Pagination {
|
||||
}
|
||||
}
|
||||
|
||||
private static String tokenRangeMessage(int maxTokenLength) {
|
||||
return "Pagination token must be between 1 and " + maxTokenLength + " characters.";
|
||||
}
|
||||
|
||||
/** 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);
|
||||
|
||||
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.scheduler.model.AwsVpcConfiguration;
|
||||
import io.floci.aws.services.scheduler.model.DeadLetterConfig;
|
||||
@@ -33,6 +35,7 @@ import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
@@ -109,14 +112,20 @@ public class SchedulerController {
|
||||
@GET
|
||||
@Path("/schedule-groups")
|
||||
public Response listScheduleGroups(@Context HttpHeaders headers,
|
||||
@QueryParam("NamePrefix") String namePrefix) {
|
||||
@QueryParam("NamePrefix") String namePrefix,
|
||||
@QueryParam("MaxResults") String maxResults,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<ScheduleGroup> groups = schedulerService.listScheduleGroups(namePrefix, region);
|
||||
PaginatedResult<ScheduleGroup> page = schedulerService.listScheduleGroups(namePrefix,
|
||||
Pagination.parseMaxResults(maxResults, "ValidationException"), rawNextToken(uriInfo), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode items = response.putArray("ScheduleGroups");
|
||||
for (ScheduleGroup group : groups) {
|
||||
for (ScheduleGroup group : page.items()) {
|
||||
items.add(objectMapper.valueToTree(buildGroupResponse(group)));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -197,19 +206,39 @@ public class SchedulerController {
|
||||
public Response listSchedules(@Context HttpHeaders headers,
|
||||
@QueryParam("ScheduleGroup") String groupName,
|
||||
@QueryParam("NamePrefix") String namePrefix,
|
||||
@QueryParam("State") String state) {
|
||||
@QueryParam("State") String state,
|
||||
@QueryParam("MaxResults") String maxResults,
|
||||
@Context UriInfo uriInfo) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<Schedule> schedules = schedulerService.listSchedules(groupName, namePrefix, state, region);
|
||||
PaginatedResult<Schedule> page = schedulerService.listSchedules(groupName, namePrefix, state,
|
||||
Pagination.parseMaxResults(maxResults, "ValidationException"), rawNextToken(uriInfo), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode items = response.putArray("Schedules");
|
||||
for (Schedule schedule : schedules) {
|
||||
for (Schedule schedule : page.items()) {
|
||||
items.add(objectMapper.valueToTree(buildScheduleSummary(schedule)));
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
/**
|
||||
* Reads {@code NextToken} from the raw query so an explicit empty value stays {@code ""}:
|
||||
* a {@code @QueryParam} binding reports it as absent, which would serve the first page
|
||||
* instead of the ValidationException the modeled minimum length of 1 calls for.
|
||||
*/
|
||||
private static String rawNextToken(UriInfo uriInfo) {
|
||||
List<String> values = uriInfo.getQueryParameters().get("NextToken");
|
||||
if (values == null || values.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String value = values.getFirst();
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildGroupResponse(ScheduleGroup group) {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("Name", group.getName());
|
||||
|
||||
@@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectReader;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
@@ -37,6 +39,9 @@ public class SchedulerService {
|
||||
"arn:(?:" + AwsArnUtils.PARTITION_REGEX + "):iam::\\d{12}:role/[\\w+=,.@/-]+");
|
||||
private static final String ROLE_ARN_MODEL_PATTERN = "^arn:aws(-[a-z]+)?:iam::\\d{12}:role\\/[\\w+=,.@\\/-]+$"; // partition-literal: the Scheduler model's RoleArn pattern, quoted verbatim in the error message
|
||||
private static final String DEFAULT_GROUP = "default";
|
||||
// ListScheduleGroups and ListSchedules model MaxResults as 1-100 and NextToken as 1-2048 chars.
|
||||
private static final int MAX_PAGE_SIZE = 100;
|
||||
private static final int MAX_TOKEN_LENGTH = 2048;
|
||||
// FAIL_ON_TRAILING_TOKENS matters here: without it an Input of "{} garbage" parses as the
|
||||
// leading object and the rest is silently dropped, so a value AWS rejects would be stored.
|
||||
private static final ObjectReader JSON = new ObjectMapper()
|
||||
@@ -185,6 +190,13 @@ public class SchedulerService {
|
||||
});
|
||||
}
|
||||
|
||||
public PaginatedResult<ScheduleGroup> listScheduleGroups(String namePrefix, Integer maxResults,
|
||||
String nextToken, String region) {
|
||||
return Pagination.paginate(listScheduleGroups(namePrefix, region), ScheduleGroup::getName,
|
||||
maxResults, nextToken, MAX_PAGE_SIZE, MAX_PAGE_SIZE, MAX_TOKEN_LENGTH,
|
||||
"ValidationException", pageNamespace("ListScheduleGroups", region), true);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Schedules ────────────────────────────
|
||||
|
||||
public Schedule createSchedule(ScheduleRequest req, String region) {
|
||||
@@ -324,6 +336,18 @@ public class SchedulerService {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
public PaginatedResult<Schedule> listSchedules(String groupName, String namePrefix, String state,
|
||||
Integer maxResults, String nextToken, String region) {
|
||||
return Pagination.paginate(listSchedules(groupName, namePrefix, state, region),
|
||||
schedule -> schedule.getGroupName() + ":" + schedule.getName(),
|
||||
maxResults, nextToken, MAX_PAGE_SIZE, MAX_PAGE_SIZE, MAX_TOKEN_LENGTH,
|
||||
"ValidationException", pageNamespace("ListSchedules", region), true);
|
||||
}
|
||||
|
||||
private String pageNamespace(String operation, String region) {
|
||||
return "scheduler:" + operation + ":" + regionResolver.getAccountId() + ":" + region;
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
public static boolean isValidName(String name) {
|
||||
|
||||
Reference in new issue
Block a user