diff --git a/docs/services/lambda.md b/docs/services/lambda.md index c506281caf..43c088e4d5 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -23,7 +23,7 @@ Floci Lambda runs your function code locally inside real Docker containers - clo | `UpdateEventSourceMapping` | Update a mapping | | `DeleteEventSourceMapping` | Remove a mapping | | `PublishVersion` | Publish an immutable version | -| `ListVersionsByFunction` | List all published versions of a function | +| `ListVersionsByFunction` | List all published versions of a function, `$LATEST` first and then published versions in ascending numeric order | | `CreateAlias` | Create a named alias pointing to a version | | `GetAlias` | Get alias details | | `ListAliases` | List all aliases for a function | diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java index 9a322be87c..aeeb79b0f0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaFunctionStore.java @@ -10,6 +10,8 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; @@ -78,7 +80,7 @@ public class LambdaFunctionStore implements Resettable { public void save(String region, LambdaFunction fn) { // Remove old index entry if URL changed or was removed get(region, fn.getFunctionName(), fn.getVersion()).ifPresent(this::deindexFunction); - + backend.put(regionKey(region, fn.getFunctionName(), fn.getVersion()), fn); indexFunction(fn); } @@ -140,7 +142,17 @@ public class LambdaFunctionStore implements Resettable { public List listVersions(String region, String functionName) { String prefix = "lambda::" + region + "::" + functionName + "::"; - return backend.scan(key -> key.startsWith(prefix)); + List versions = new ArrayList<>(backend.scan(key -> key.startsWith(prefix))); + versions.sort(Comparator.comparingLong(LambdaFunctionStore::versionRank)); + return versions; + } + + private static long versionRank(LambdaFunction fn) { + String version = fn.getVersion(); + if (version == null || version.isEmpty() || !version.chars().allMatch(Character::isDigit)) { + return -1L; + } + return Long.parseLong(version); } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java index e5bb7a8859..7d212eb8f3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaVersionIntegrationTest.java @@ -122,7 +122,7 @@ class LambdaVersionIntegrationTest { .get(BASE_PATH + "/functions/" + FUNCTION_NAME) .then() .statusCode(404); - + // Verify versions are gone (actually listVersionsByFunction should return 404 if function doesn't exist) given() .when() @@ -258,6 +258,44 @@ class LambdaVersionIntegrationTest { } } + @Test + @Order(10) + void listVersionsByFunctionReturnsLatestFirstThenNumericOrder() { + String fnName = "ordered-versions-function"; + given() + .contentType("application/json") + .body(""" + { + "FunctionName": "%s", + "Runtime": "nodejs20.x", + "Role": "arn:aws:iam::000000000000:role/lambda-role", + "Handler": "index.handler" + } + """.formatted(fnName)) + .when() + .post(BASE_PATH + "/functions") + .then() + .statusCode(201); + + for (int i = 1; i <= 11; i++) { + given() + .contentType("application/json") + .body("{\"Description\": \"v" + i + "\"}") + .when() + .post(BASE_PATH + "/functions/" + fnName + "/versions") + .then() + .statusCode(201) + .body("Version", equalTo(String.valueOf(i))); + } + + given() + .when() + .get(BASE_PATH + "/functions/" + fnName + "/versions") + .then() + .statusCode(200) + .body("Versions.Version", contains("$LATEST", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")); + } + private void createInvocableFunction(String fnName) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) {