diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md index 97e6f7b405..cf59569745 100644 --- a/docs/services/step-functions.md +++ b/docs/services/step-functions.md @@ -429,6 +429,9 @@ the wire and the task fails with `Sfn.StateMachineDoesNotExistException`. | `arn:aws:states:::aws-sdk:scheduler:deleteSchedule` | `{}` | `Scheduler.ResourceNotFoundException` | | `arn:aws:states:::aws-sdk:sns:publish` | `{MessageId}` | `Sns.NotFoundException` when the topic does not exist | +Scheduler create and update tasks accept `StartDate` and `EndDate` as RFC 3339 strings, including +offsets and fractional seconds. The direct Scheduler API continues to use numeric epoch seconds. + `sendTaskSuccess` and `sendTaskFailure` resolve a token a `.waitForTaskToken` task is parked on. A token nobody is waiting for fails the calling task rather than reporting a delivery that never happened. diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java index 4f7651cf01..a8aa2fdcea 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java @@ -84,6 +84,7 @@ import java.security.NoSuchAlgorithmException; import java.time.Instant; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -1392,7 +1393,8 @@ public class AslExecutor { input.path("GroupName").asText(null), region); return objectMapper.createObjectNode(); } - ScheduleRequest request = schedulerController.parseScheduleRequest(input); + ScheduleRequest request = schedulerController.parseScheduleRequest( + normalizeAwsSdkSchedulerInput(input)); request.setName(input.path("Name").asText(null)); Schedule schedule = creating ? schedulerService.createSchedule(request, region) @@ -1405,6 +1407,37 @@ public class AslExecutor { } } + /** Converts the SDK's RFC 3339 timestamps to the epoch-second values used by the wire parser. */ + private JsonNode normalizeAwsSdkSchedulerInput(JsonNode input) { + JsonNode normalized = input.deepCopy(); + if (normalized instanceof ObjectNode object) { + normalizeAwsSdkSchedulerTimestamp(object, "StartDate"); + normalizeAwsSdkSchedulerTimestamp(object, "EndDate"); + } + return normalized; + } + + private void normalizeAwsSdkSchedulerTimestamp(ObjectNode input, String field) { + JsonNode value = input.get(field); + if (value == null || value.isNull() || value.isNumber()) { + return; + } + if (!value.isTextual()) { + throw malformedAwsSdkSchedulerTimestamp(field); + } + try { + Instant instant = Instant.parse(value.textValue()); + input.put(field, instant.getEpochSecond() + instant.getNano() / 1_000_000_000d); + } catch (DateTimeParseException ignored) { + // AWS exposes SDK timestamp deserialization failures as SerializationException. + throw malformedAwsSdkSchedulerTimestamp(field); + } + } + + private static AwsException malformedAwsSdkSchedulerTimestamp(String field) { + return new AwsException("SerializationException", field + " must be an RFC 3339 timestamp.", 400); + } + /** * Optimized EventBridge integration for {@code events:putEvents}. The task result is the * PutEvents response itself, and one failed entry fails the whole task with diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsAwsSdkTaskIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsAwsSdkTaskIntegrationTest.java index e702f9fd9a..c5f3bc61be 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsAwsSdkTaskIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsAwsSdkTaskIntegrationTest.java @@ -545,6 +545,106 @@ class StepFunctionsAwsSdkTaskIntegrationTest { assertEquals("Scheduler.ValidationException", result.path("caughtError").asText()); } + @Test + @Order(20) + void createScheduleAcceptsRfc3339DatesFromJsonata() throws Exception { + String stateMachineArn = createStateMachine("aws-sdk-create-schedule-dates", """ + { + "QueryLanguage": "JSONata", + "StartAt": "Schedule", + "States": { + "Schedule": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule", + "Arguments": { + "Name": "payout-dated", + "ScheduleExpression": "rate(1 day)", + "StartDate": "2099-01-01T05:30:00.500+05:30", + "EndDate": "2099-01-02T00:00:00Z", + "FlexibleTimeWindow": {"Mode": "OFF"}, + "Target": {"Arn": "TARGET_ARN", "RoleArn": "ROLE"} + }, + "End": true + } + } + } + """.replace("TARGET_ARN", quickChildArn).replace("ROLE", ROLE_ARN)); + + succeedingOutputOf(stateMachineArn, "{}"); + + JsonNode schedule = mapper.readTree(given().when().get("/schedules/payout-dated") + .then().statusCode(200).extract().body().asString()); + assertEquals(4070908800.5d, schedule.path("StartDate").asDouble(), 0.001d); + assertEquals(4070995200L, Math.round(schedule.path("EndDate").asDouble())); + } + + @Test + @Order(21) + void updateScheduleAcceptsRfc3339DatesFromJsonPath() throws Exception { + String stateMachineArn = createStateMachine("aws-sdk-update-schedule-dates", """ + { + "StartAt": "Schedule", + "States": { + "Schedule": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:scheduler:updateSchedule", + "Parameters": { + "Name": "payout-dated", + "ScheduleExpression": "rate(2 days)", + "StartDate": "2099-02-01T00:00:00Z", + "EndDate": "2099-02-02T00:00:00Z", + "FlexibleTimeWindow": {"Mode": "OFF"}, + "Target": {"Arn": "TARGET_ARN", "RoleArn": "ROLE"} + }, + "End": true + } + } + } + """.replace("TARGET_ARN", quickChildArn).replace("ROLE", ROLE_ARN)); + + succeedingOutputOf(stateMachineArn, "{}"); + + JsonNode schedule = mapper.readTree(given().when().get("/schedules/payout-dated") + .then().statusCode(200).extract().body().asString()); + assertEquals(4073587200L, Math.round(schedule.path("StartDate").asDouble())); + assertEquals(4073673600L, Math.round(schedule.path("EndDate").asDouble())); + given().when().delete("/schedules/payout-dated").then().statusCode(200); + } + + @Test + @Order(22) + void malformedScheduleDateIsCatchableAsSerializationException() throws Exception { + String stateMachineArn = createStateMachine("aws-sdk-create-schedule-bad-date", """ + { + "QueryLanguage": "JSONata", + "StartAt": "Schedule", + "States": { + "Schedule": { + "Type": "Task", + "Resource": "arn:aws:states:::aws-sdk:scheduler:createSchedule", + "Arguments": { + "Name": "payout-bad-date", + "ScheduleExpression": "rate(1 day)", + "StartDate": "tomorrow", + "FlexibleTimeWindow": {"Mode": "OFF"}, + "Target": {"Arn": "TARGET_ARN", "RoleArn": "ROLE"} + }, + "Catch": [{ + "ErrorEquals": ["Scheduler.SerializationException"], + "Next": "Recovered", + "Output": {"caughtError": "{% $states.errorOutput.Error %}"} + }], + "End": true + }, + "Recovered": {"Type": "Pass", "End": true} + } + } + """.replace("TARGET_ARN", quickChildArn).replace("ROLE", ROLE_ARN)); + + JsonNode result = mapper.readTree(succeedingOutputOf(stateMachineArn, "{}")); + assertEquals("Scheduler.SerializationException", result.path("caughtError").asText()); + } + private static String deleteScheduleTask(String scheduleName, String groupName) { String groupArgument = groupName == null ? "" : ", \"GroupName\": \"" + groupName + "\""; return """