mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(stepfunctions): paginate state machine listings (#6717)
This commit is contained in:
1 parent
2eaf268727
commit
28e72d2496
5 files changed
+267
-3
No files matched your search
+32
-2
@@ -37,11 +37,13 @@ import java.util.Set;
|
||||
public class StepFunctionsJsonHandler {
|
||||
|
||||
private static final Duration HISTORY_TOKEN_LIFETIME = Duration.ofHours(24);
|
||||
private static final Duration LIST_TOKEN_LIFETIME = Duration.ofHours(24);
|
||||
|
||||
private final StepFunctionsService service;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Clock clock;
|
||||
private final SignedMarkers historyTokens = new SignedMarkers();
|
||||
private final SignedMarkers listTokens = new SignedMarkers();
|
||||
|
||||
@Inject
|
||||
public StepFunctionsJsonHandler(StepFunctionsService service, ObjectMapper objectMapper, Clock clock) {
|
||||
@@ -289,19 +291,47 @@ public class StepFunctionsJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleListStateMachines(JsonNode request, String region) {
|
||||
List<StateMachine> list = service.listStateMachines(region);
|
||||
Integer maxResults = parseOptionalInt(request, "maxResults");
|
||||
String nextToken = optionalText(request, "nextToken");
|
||||
StepFunctionsPaging.pageSize(maxResults, nextToken);
|
||||
PaginatedResult<StateMachine> page = service.listStateMachines(region, maxResults,
|
||||
nextToken == null ? null : listTokenPosition(nextToken, region));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode array = response.putArray("stateMachines");
|
||||
for (StateMachine sm : list) {
|
||||
for (StateMachine sm : page.items()) {
|
||||
ObjectNode item = array.addObject();
|
||||
item.put("stateMachineArn", sm.getStateMachineArn());
|
||||
item.put("name", sm.getName());
|
||||
item.put("type", sm.getType());
|
||||
item.put("creationDate", sm.getCreationDate());
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("nextToken", listTokens.issue(
|
||||
clock.millis() + ":" + Digests.sha256Hex(region) + ":" + page.nextToken()));
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private String listTokenPosition(String nextToken, String region) {
|
||||
String issued = listTokens.cursorOf(nextToken);
|
||||
if (issued == null) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
String[] fields = issued.split(":", 3);
|
||||
long now = clock.millis();
|
||||
long issuedAt;
|
||||
try {
|
||||
issuedAt = Long.parseLong(fields[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
if (fields.length != 3 || !fields[1].equals(Digests.sha256Hex(region))
|
||||
|| issuedAt > now || now - issuedAt > LIST_TOKEN_LIFETIME.toMillis()) {
|
||||
throw new AwsException("InvalidToken", "The provided token is not valid.", 400);
|
||||
}
|
||||
return fields[2];
|
||||
}
|
||||
|
||||
private Response handleDeleteStateMachine(JsonNode request) {
|
||||
service.deleteStateMachine(request.path("stateMachineArn").asText());
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/**
|
||||
* The page request ListActivities and GetExecutionHistory check: a {@code maxResults} of 0 to 1000,
|
||||
* The page request ListActivities, ListStateMachines and GetExecutionHistory check: a {@code maxResults} of 0 to 1000,
|
||||
* where 0 or none means 100, and a {@code nextToken} of 1 to 1024 characters. Callers read both
|
||||
* fields before calling it, so a wrongly typed field is reported before either bound.
|
||||
*/
|
||||
|
||||
+7
@@ -528,6 +528,13 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
return stateMachineStore.scan(k -> k.startsWith(regionArnPrefix(region)));
|
||||
}
|
||||
|
||||
public PaginatedResult<StateMachine> listStateMachines(String region, Integer maxResults, String nextToken) {
|
||||
int pageSize = StepFunctionsPaging.pageSize(maxResults, nextToken);
|
||||
return Pagination.paginate(listStateMachines(region), StateMachine::getStateMachineArn, pageSize, nextToken,
|
||||
"states:state-machines:" + region + ":" + regionResolver.getAccountId(),
|
||||
token -> new AwsException("InvalidToken", "Invalid nextToken.", 400));
|
||||
}
|
||||
|
||||
// ── State machine versions ──────────────────────────────────────────────
|
||||
|
||||
public StateMachineVersion publishStateMachineVersion(String stateMachineArn) {
|
||||
|
||||
Reference in new issue
Block a user