mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(sagemaker): honor list sorting options (#6745)
This commit is contained in:
1 parent
3370d0ff62
commit
4bf399b5a1
3 files changed
+664
-83
No files matched your search
+249
-81
@@ -143,33 +143,24 @@ public class SageMakerService {
|
||||
}
|
||||
|
||||
public ObjectNode listModels(JsonNode request, String region) {
|
||||
String sortBy = text(request, "SortBy");
|
||||
String sortOrder = text(request, "SortOrder");
|
||||
List<String> violations = new ArrayList<>();
|
||||
if (sortBy != null && !sortBy.equals("Name") && !sortBy.equals("CreationTime")) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(sortBy, "sortBy", List.of("Name", "CreationTime")));
|
||||
}
|
||||
if (sortOrder != null && !sortOrder.equals("Ascending") && !sortOrder.equals("Descending")) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(sortOrder, "sortOrder", List.of("Ascending", "Descending")));
|
||||
}
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations);
|
||||
}
|
||||
Comparator<ModelResource> comparator = "Name".equals(sortBy)
|
||||
? Comparator.comparing(m -> m.modelName)
|
||||
: Comparator.comparingLong((ModelResource m) -> m.creationTime).thenComparing(m -> m.modelName);
|
||||
if (sortOrder == null || sortOrder.equals("Descending")) {
|
||||
comparator = comparator.reversed();
|
||||
}
|
||||
SortOptions sort = sortOptions(request, "CreationTime", "Descending", "Name", "CreationTime");
|
||||
String sortBy = sort.sortBy();
|
||||
String sortOrder = sort.sortOrder();
|
||||
Comparator<ModelResource> comparator = modelComparator(sortBy, sortOrder);
|
||||
List<ModelResource> models = modelStore.scan(k -> k.startsWith(region + "::")).stream().sorted(comparator).toList();
|
||||
String nameContains = text(request, "NameContains");
|
||||
if (nameContains != null && !nameContains.isBlank()) {
|
||||
models = models.stream().filter(m -> m.modelName.contains(nameContains)).toList();
|
||||
}
|
||||
return paginateModels(models, request, comparator, region,
|
||||
sortBy == null ? "CreationTime" : sortBy,
|
||||
sortOrder == null ? "Descending" : sortOrder,
|
||||
nameContains == null || nameContains.isBlank() ? "" : nameContains);
|
||||
return paginate(models, request, sortBy, sortOrder,
|
||||
new CursorScope("ListModels", region,
|
||||
nameContains == null || nameContains.isBlank() ? "" : nameContains, ""),
|
||||
m -> modelCursor(m, sortBy, sortOrder),
|
||||
m -> modelAnchor(m, sortBy), comparator, (m, n) -> {
|
||||
n.put("ModelName", m.modelName);
|
||||
n.put("ModelArn", m.modelArn);
|
||||
n.put("CreationTime", epoch(m.creationTime));
|
||||
}, "Models");
|
||||
}
|
||||
|
||||
public synchronized ObjectNode createEndpointConfig(JsonNode request, String region) {
|
||||
@@ -224,9 +215,16 @@ public class SageMakerService {
|
||||
}
|
||||
|
||||
public ObjectNode listEndpointConfigs(JsonNode request, String region) {
|
||||
SortOptions sort = sortOptions(request, "CreationTime", "Descending", "Name", "CreationTime");
|
||||
String sortBy = sort.sortBy();
|
||||
String sortOrder = sort.sortOrder();
|
||||
Comparator<EndpointConfigResource> comparator = endpointConfigComparator(sortBy, sortOrder);
|
||||
List<EndpointConfigResource> configs = endpointConfigStore.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.sorted(Comparator.comparing(c -> c.endpointConfigName)).toList();
|
||||
return paginate(configs, request, c -> c.endpointConfigName, (c, n) -> {
|
||||
.sorted(comparator).toList();
|
||||
return paginate(configs, request, sortBy, sortOrder,
|
||||
new CursorScope("ListEndpointConfigs", region, "", ""),
|
||||
c -> endpointConfigCursor(c, sortBy, sortOrder),
|
||||
c -> endpointConfigAnchor(c, sortBy), comparator, (c, n) -> {
|
||||
n.put("EndpointConfigName", c.endpointConfigName);
|
||||
n.put("EndpointConfigArn", c.endpointConfigArn);
|
||||
n.put("CreationTime", epoch(c.creationTime));
|
||||
@@ -310,9 +308,17 @@ public class SageMakerService {
|
||||
}
|
||||
|
||||
public ObjectNode listEndpoints(JsonNode request, String region) {
|
||||
SortOptions sort = sortOptions(request, "CreationTime", "Descending", "Name", "CreationTime", "Status");
|
||||
String sortBy = sort.sortBy();
|
||||
String sortOrder = sort.sortOrder();
|
||||
Comparator<EndpointResource> comparator = endpointComparator(sortBy, sortOrder);
|
||||
List<EndpointResource> endpoints = endpointStore.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.sorted(Comparator.comparing(e -> e.endpointName)).toList();
|
||||
return paginate(endpoints, request, e -> e.endpointName, (e, n) -> {
|
||||
.map(SageMakerService::endpointListSnapshot)
|
||||
.sorted(comparator).toList();
|
||||
return paginate(endpoints, request, sortBy, sortOrder,
|
||||
new CursorScope("ListEndpoints", region, "", ""),
|
||||
e -> endpointCursor(e, sortBy, sortOrder),
|
||||
e -> endpointAnchor(e, sortBy), comparator, (e, n) -> {
|
||||
n.put("EndpointName", e.endpointName);
|
||||
n.put("EndpointArn", e.endpointArn);
|
||||
n.put("EndpointStatus", e.endpointStatus);
|
||||
@@ -394,8 +400,13 @@ public class SageMakerService {
|
||||
}
|
||||
|
||||
public ObjectNode listTrainingJobs(JsonNode request, String region) {
|
||||
SortOptions sort = sortOptions(request, "CreationTime", "Ascending", "Name", "CreationTime", "Status");
|
||||
String sortBy = sort.sortBy();
|
||||
String sortOrder = sort.sortOrder();
|
||||
Comparator<TrainingJobResource> comparator = trainingJobComparator(sortBy, sortOrder);
|
||||
List<TrainingJobResource> jobs = trainingJobStore.scan(k -> k.startsWith(region + "::")).stream()
|
||||
.sorted(Comparator.comparing(j -> j.trainingJobName)).toList();
|
||||
.map(SageMakerService::trainingJobListSnapshot)
|
||||
.sorted(comparator).toList();
|
||||
String nameContains = text(request, "NameContains");
|
||||
if (nameContains != null && !nameContains.isBlank()) {
|
||||
jobs = jobs.stream().filter(j -> j.trainingJobName.contains(nameContains)).toList();
|
||||
@@ -404,7 +415,12 @@ public class SageMakerService {
|
||||
if (statusEquals != null && !statusEquals.isBlank()) {
|
||||
jobs = jobs.stream().filter(j -> statusEquals.equals(j.trainingJobStatus)).toList();
|
||||
}
|
||||
return paginate(jobs, request, j -> j.trainingJobName, (j, n) -> {
|
||||
return paginate(jobs, request, sortBy, sortOrder,
|
||||
new CursorScope("ListTrainingJobs", region,
|
||||
nameContains == null || nameContains.isBlank() ? "" : nameContains,
|
||||
statusEquals == null || statusEquals.isBlank() ? "" : statusEquals),
|
||||
j -> trainingJobCursor(j, sortBy, sortOrder),
|
||||
j -> trainingJobAnchor(j, sortBy), comparator, (j, n) -> {
|
||||
n.put("TrainingJobName", j.trainingJobName);
|
||||
n.put("TrainingJobArn", j.trainingJobArn);
|
||||
n.put("TrainingJobStatus", j.trainingJobStatus);
|
||||
@@ -626,76 +642,228 @@ public class SageMakerService {
|
||||
return arr;
|
||||
}
|
||||
|
||||
/** Applies name-ascending pagination for the existing endpoint and training-job lists. */
|
||||
private <T> ObjectNode paginate(List<T> sorted, JsonNode request, Function<T, String> nameOf,
|
||||
private <T> ObjectNode paginate(List<T> sorted, JsonNode request, String sortBy, String sortOrder,
|
||||
CursorScope scope,
|
||||
Function<T, SortCursor> cursorOf,
|
||||
Function<SortCursor, T> anchorOf, Comparator<T> comparator,
|
||||
BiConsumer<T, ObjectNode> render, String itemsField) {
|
||||
int max = maxResults(request);
|
||||
String nextToken = text(request, "NextToken");
|
||||
List<T> remaining = sorted;
|
||||
if (nextToken != null && !nextToken.isBlank()) {
|
||||
remaining = remaining.stream().dropWhile(v -> nameOf.apply(v).compareTo(nextToken) <= 0).toList();
|
||||
SortCursor cursor = decodeCursor(nextToken, sortBy, sortOrder, scope);
|
||||
T anchor = anchorOf.apply(cursor);
|
||||
remaining = remaining.stream().dropWhile(v -> comparator.compare(v, anchor) <= 0).toList();
|
||||
}
|
||||
return page(remaining, max, render, itemsField, nameOf);
|
||||
return page(remaining, max, render, itemsField, value -> encodeCursor(cursorOf.apply(value), scope));
|
||||
}
|
||||
|
||||
private ObjectNode paginateModels(List<ModelResource> sorted, JsonNode request,
|
||||
Comparator<ModelResource> comparator, String region,
|
||||
String sortBy, String sortOrder, String nameContains) {
|
||||
int max = maxResults(request);
|
||||
String nextToken = text(request, "NextToken");
|
||||
List<ModelResource> remaining = sorted;
|
||||
if (nextToken != null && !nextToken.isBlank()) {
|
||||
ModelResource anchor;
|
||||
if (sortBy.equals("Name")) {
|
||||
anchor = new ModelResource();
|
||||
anchor.modelName = nextToken;
|
||||
} else {
|
||||
anchor = decodeModelCursor(nextToken, region, sortOrder, nameContains);
|
||||
}
|
||||
remaining = remaining.stream().dropWhile(m -> comparator.compare(m, anchor) <= 0).toList();
|
||||
private static SortOptions sortOptions(JsonNode request, String defaultSortBy, String defaultSortOrder,
|
||||
String... allowed) {
|
||||
String sortBy = text(request, "SortBy");
|
||||
String sortOrder = text(request, "SortOrder");
|
||||
sortBy = sortBy == null ? defaultSortBy : sortBy;
|
||||
sortOrder = sortOrder == null ? defaultSortOrder : sortOrder;
|
||||
List<String> violations = new ArrayList<>();
|
||||
if (!List.of(allowed).contains(sortBy)) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(sortBy, "sortBy", List.of(allowed)));
|
||||
}
|
||||
return page(remaining, max, (m, n) -> {
|
||||
n.put("ModelName", m.modelName);
|
||||
n.put("ModelArn", m.modelArn);
|
||||
n.put("CreationTime", epoch(m.creationTime));
|
||||
}, "Models", m -> sortBy.equals("Name") ? m.modelName
|
||||
: encodeModelCursor(m, region, sortOrder, nameContains));
|
||||
if (!sortOrder.equals("Ascending") && !sortOrder.equals("Descending")) {
|
||||
violations.add(SmithyValidationErrors.notInEnum(sortOrder, "sortOrder",
|
||||
List.of("Ascending", "Descending")));
|
||||
}
|
||||
if (!violations.isEmpty()) {
|
||||
throw SmithyValidationErrors.validationException(violations);
|
||||
}
|
||||
return new SortOptions(sortBy, sortOrder);
|
||||
}
|
||||
|
||||
private String encodeModelCursor(ModelResource model, String region, String sortOrder, String nameContains) {
|
||||
ObjectNode cursor = mapper.createObjectNode();
|
||||
cursor.put("version", 1);
|
||||
cursor.put("region", region);
|
||||
cursor.put("sortBy", "CreationTime");
|
||||
cursor.put("sortOrder", sortOrder);
|
||||
cursor.put("nameContains", nameContains);
|
||||
cursor.put("creationTime", model.creationTime);
|
||||
cursor.put("modelName", model.modelName);
|
||||
return Base64Url.encode(cursor.toString());
|
||||
private record SortOptions(String sortBy, String sortOrder) {
|
||||
}
|
||||
|
||||
private ModelResource decodeModelCursor(String token, String region, String sortOrder, String nameContains) {
|
||||
JsonNode cursor;
|
||||
private static Comparator<ModelResource> modelComparator(String sortBy, String sortOrder) {
|
||||
Comparator<ModelResource> comparator = "Name".equals(sortBy)
|
||||
? Comparator.comparing(m -> m.modelName)
|
||||
: Comparator.comparingLong((ModelResource m) -> m.creationTime).thenComparing(m -> m.modelName);
|
||||
return "Descending".equals(sortOrder) ? comparator.reversed() : comparator;
|
||||
}
|
||||
|
||||
private static Comparator<EndpointConfigResource> endpointConfigComparator(String sortBy, String sortOrder) {
|
||||
Comparator<EndpointConfigResource> comparator = switch (sortBy) {
|
||||
case "Name" -> Comparator.comparing(c -> c.endpointConfigName);
|
||||
case "CreationTime" -> Comparator.comparingLong((EndpointConfigResource c) -> c.creationTime)
|
||||
.thenComparing(c -> c.endpointConfigName);
|
||||
default -> throw new IllegalStateException("Unexpected endpoint config sort key: " + sortBy);
|
||||
};
|
||||
return "Descending".equals(sortOrder) ? comparator.reversed() : comparator;
|
||||
}
|
||||
|
||||
private static EndpointResource endpointListSnapshot(EndpointResource endpoint) {
|
||||
EndpointResource snapshot = new EndpointResource();
|
||||
snapshot.endpointName = endpoint.endpointName;
|
||||
snapshot.endpointArn = endpoint.endpointArn;
|
||||
snapshot.endpointStatus = endpoint.endpointStatus;
|
||||
snapshot.creationTime = endpoint.creationTime;
|
||||
snapshot.lastModifiedTime = endpoint.lastModifiedTime;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private static TrainingJobResource trainingJobListSnapshot(TrainingJobResource job) {
|
||||
TrainingJobResource snapshot = new TrainingJobResource();
|
||||
snapshot.trainingJobName = job.trainingJobName;
|
||||
snapshot.trainingJobArn = job.trainingJobArn;
|
||||
snapshot.trainingJobStatus = job.trainingJobStatus;
|
||||
snapshot.creationTime = job.creationTime;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private static Comparator<EndpointResource> endpointComparator(String sortBy, String sortOrder) {
|
||||
Comparator<EndpointResource> comparator = switch (sortBy) {
|
||||
case "Name" -> Comparator.comparing(e -> e.endpointName);
|
||||
case "CreationTime" -> Comparator.comparingLong((EndpointResource e) -> e.creationTime)
|
||||
.thenComparing(e -> e.endpointName);
|
||||
case "Status" -> Comparator.comparing((EndpointResource e) -> e.endpointStatus)
|
||||
.thenComparing(e -> e.endpointName);
|
||||
default -> throw new IllegalStateException("Unexpected endpoint sort key: " + sortBy);
|
||||
};
|
||||
return "Descending".equals(sortOrder) ? comparator.reversed() : comparator;
|
||||
}
|
||||
|
||||
private static Comparator<TrainingJobResource> trainingJobComparator(String sortBy, String sortOrder) {
|
||||
Comparator<TrainingJobResource> comparator = switch (sortBy) {
|
||||
case "Name" -> Comparator.comparing(j -> j.trainingJobName);
|
||||
case "CreationTime" -> Comparator.comparingLong((TrainingJobResource j) -> j.creationTime)
|
||||
.thenComparing(j -> j.trainingJobName);
|
||||
case "Status" -> Comparator.comparing((TrainingJobResource j) -> j.trainingJobStatus)
|
||||
.thenComparing(j -> j.trainingJobName);
|
||||
default -> throw new IllegalStateException("Unexpected training job sort key: " + sortBy);
|
||||
};
|
||||
return "Descending".equals(sortOrder) ? comparator.reversed() : comparator;
|
||||
}
|
||||
|
||||
private static SortCursor endpointConfigCursor(EndpointConfigResource config, String sortBy, String sortOrder) {
|
||||
String value = "Name".equals(sortBy) ? config.endpointConfigName : Long.toString(config.creationTime);
|
||||
return new SortCursor(sortBy, sortOrder, value, config.endpointConfigName);
|
||||
}
|
||||
|
||||
private static EndpointConfigResource endpointConfigAnchor(SortCursor cursor, String sortBy) {
|
||||
EndpointConfigResource anchor = new EndpointConfigResource();
|
||||
anchor.endpointConfigName = cursor.name();
|
||||
if ("CreationTime".equals(sortBy)) {
|
||||
anchor.creationTime = parseCursorTime(cursor.value());
|
||||
}
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private static SortCursor endpointCursor(EndpointResource endpoint, String sortBy, String sortOrder) {
|
||||
String value = switch (sortBy) {
|
||||
case "Name" -> endpoint.endpointName;
|
||||
case "CreationTime" -> Long.toString(endpoint.creationTime);
|
||||
case "Status" -> endpoint.endpointStatus;
|
||||
default -> throw new IllegalStateException("Unexpected endpoint sort key: " + sortBy);
|
||||
};
|
||||
return new SortCursor(sortBy, sortOrder, value, endpoint.endpointName);
|
||||
}
|
||||
|
||||
private static EndpointResource endpointAnchor(SortCursor cursor, String sortBy) {
|
||||
EndpointResource anchor = new EndpointResource();
|
||||
anchor.endpointName = cursor.name();
|
||||
if ("CreationTime".equals(sortBy)) {
|
||||
anchor.creationTime = parseCursorTime(cursor.value());
|
||||
} else if ("Status".equals(sortBy)) {
|
||||
anchor.endpointStatus = cursor.value();
|
||||
}
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private static SortCursor trainingJobCursor(TrainingJobResource job, String sortBy, String sortOrder) {
|
||||
String value = switch (sortBy) {
|
||||
case "Name" -> job.trainingJobName;
|
||||
case "CreationTime" -> Long.toString(job.creationTime);
|
||||
case "Status" -> job.trainingJobStatus;
|
||||
default -> throw new IllegalStateException("Unexpected training job sort key: " + sortBy);
|
||||
};
|
||||
return new SortCursor(sortBy, sortOrder, value, job.trainingJobName);
|
||||
}
|
||||
|
||||
private static TrainingJobResource trainingJobAnchor(SortCursor cursor, String sortBy) {
|
||||
TrainingJobResource anchor = new TrainingJobResource();
|
||||
anchor.trainingJobName = cursor.name();
|
||||
if ("CreationTime".equals(sortBy)) {
|
||||
anchor.creationTime = parseCursorTime(cursor.value());
|
||||
} else if ("Status".equals(sortBy)) {
|
||||
anchor.trainingJobStatus = cursor.value();
|
||||
}
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private static SortCursor modelCursor(ModelResource model, String sortBy, String sortOrder) {
|
||||
String value = "Name".equals(sortBy) ? model.modelName : Long.toString(model.creationTime);
|
||||
return new SortCursor(sortBy, sortOrder, value, model.modelName);
|
||||
}
|
||||
|
||||
private static ModelResource modelAnchor(SortCursor cursor, String sortBy) {
|
||||
ModelResource anchor = new ModelResource();
|
||||
anchor.modelName = cursor.name();
|
||||
if ("CreationTime".equals(sortBy)) {
|
||||
anchor.creationTime = parseCursorTime(cursor.value());
|
||||
}
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private static long parseCursorTime(String value) {
|
||||
try {
|
||||
cursor = mapper.readTree(Base64Url.decode(token));
|
||||
return Long.parseLong(value);
|
||||
} catch (NumberFormatException e) {
|
||||
throw validation("Invalid NextToken.");
|
||||
}
|
||||
}
|
||||
|
||||
private static String encodeCursor(SortCursor cursor, CursorScope scope) {
|
||||
if ("Name".equals(cursor.sortBy())) {
|
||||
return cursor.value();
|
||||
}
|
||||
ObjectNode token = STATIC_MAPPER.createObjectNode();
|
||||
token.put("version", 1);
|
||||
token.put("operation", scope.operation());
|
||||
token.put("region", scope.region());
|
||||
token.put("sortBy", cursor.sortBy());
|
||||
token.put("sortOrder", cursor.sortOrder());
|
||||
token.put("nameContains", scope.nameContains());
|
||||
token.put("statusEquals", scope.statusEquals());
|
||||
token.put("value", cursor.value());
|
||||
token.put("name", cursor.name());
|
||||
return Base64Url.encode(token.toString());
|
||||
}
|
||||
|
||||
private static SortCursor decodeCursor(String token, String sortBy, String sortOrder, CursorScope scope) {
|
||||
if ("Name".equals(sortBy)) {
|
||||
return new SortCursor(sortBy, sortOrder, token, token);
|
||||
}
|
||||
try {
|
||||
JsonNode cursor = STATIC_MAPPER.readTree(Base64Url.decode(token));
|
||||
if (cursor == null || cursor.path("version").asInt() != 1
|
||||
|| !scope.operation().equals(cursor.path("operation").asText(null))
|
||||
|| !scope.region().equals(cursor.path("region").asText(null))
|
||||
|| !sortBy.equals(cursor.path("sortBy").asText(null))
|
||||
|| !sortOrder.equals(cursor.path("sortOrder").asText(null))
|
||||
|| !scope.nameContains().equals(cursor.path("nameContains").asText(null))
|
||||
|| !scope.statusEquals().equals(cursor.path("statusEquals").asText(null))
|
||||
|| !cursor.path("value").isTextual()
|
||||
|| !cursor.path("name").isTextual()
|
||||
|| cursor.path("name").asText().isBlank()) {
|
||||
throw validation("Invalid NextToken.");
|
||||
}
|
||||
return new SortCursor(sortBy, sortOrder, cursor.path("value").asText(), cursor.path("name").asText());
|
||||
} catch (IllegalArgumentException | JsonProcessingException e) {
|
||||
throw validation("Invalid NextToken.");
|
||||
}
|
||||
if (cursor == null || !cursor.path("version").isInt() || cursor.path("version").asInt() != 1
|
||||
|| !region.equals(cursor.path("region").asText(null))
|
||||
|| !"CreationTime".equals(cursor.path("sortBy").asText(null))
|
||||
|| !sortOrder.equals(cursor.path("sortOrder").asText(null))
|
||||
|| !nameContains.equals(cursor.path("nameContains").asText(null))
|
||||
|| !cursor.path("creationTime").isIntegralNumber()
|
||||
|| !cursor.path("creationTime").canConvertToLong()
|
||||
|| !cursor.path("modelName").isTextual()
|
||||
|| cursor.path("modelName").asText().isBlank()) {
|
||||
throw validation("Invalid NextToken.");
|
||||
}
|
||||
ModelResource anchor = new ModelResource();
|
||||
anchor.creationTime = cursor.path("creationTime").asLong();
|
||||
anchor.modelName = cursor.path("modelName").asText();
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private record SortCursor(String sortBy, String sortOrder, String value, String name) {
|
||||
}
|
||||
|
||||
private record CursorScope(String operation, String region, String nameContains, String statusEquals) {
|
||||
}
|
||||
|
||||
private <T> ObjectNode page(List<T> remaining, int max, BiConsumer<T, ObjectNode> render,
|
||||
|
||||
+413
@@ -0,0 +1,413 @@
|
||||
package io.github.hectorvent.floci.services.sagemaker;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.sagemaker.model.SageMakerEntities.EndpointConfigResource;
|
||||
import io.github.hectorvent.floci.services.sagemaker.model.SageMakerEntities.EndpointResource;
|
||||
import io.github.hectorvent.floci.services.sagemaker.model.SageMakerEntities.ModelResource;
|
||||
import io.github.hectorvent.floci.services.sagemaker.model.SageMakerEntities.TrainingJobResource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class SageMakerListSortServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void listEndpointsHonorsDescendingNameSort() throws Exception {
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
endpoints.put(REGION + "::a-endpoint", endpoint("a-endpoint"));
|
||||
endpoints.put(REGION + "::z-endpoint", endpoint("z-endpoint"));
|
||||
|
||||
JsonNode response = service(new InMemoryStorage<>(), endpoints, new InMemoryStorage<>())
|
||||
.listEndpoints(mapper.readTree("{\"SortBy\":\"Name\",\"SortOrder\":\"Descending\"}"), REGION);
|
||||
|
||||
assertEquals(List.of("z-endpoint", "a-endpoint"), names(response, "Endpoints", "EndpointName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEndpointConfigsHonorsDescendingNameSort() throws Exception {
|
||||
InMemoryStorage<String, EndpointConfigResource> configs = new InMemoryStorage<>();
|
||||
configs.put(REGION + "::a-config", endpointConfig("a-config"));
|
||||
configs.put(REGION + "::z-config", endpointConfig("z-config"));
|
||||
|
||||
JsonNode response = service(configs, new InMemoryStorage<>(), new InMemoryStorage<>())
|
||||
.listEndpointConfigs(mapper.readTree("{\"SortBy\":\"Name\",\"SortOrder\":\"Descending\"}"), REGION);
|
||||
|
||||
assertEquals(List.of("z-config", "a-config"), names(response, "EndpointConfigs", "EndpointConfigName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTrainingJobsHonorsDescendingNameSort() throws Exception {
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
jobs.put(REGION + "::a-job", trainingJob("a-job"));
|
||||
jobs.put(REGION + "::z-job", trainingJob("z-job"));
|
||||
|
||||
JsonNode response = service(new InMemoryStorage<>(), new InMemoryStorage<>(), jobs)
|
||||
.listTrainingJobs(mapper.readTree("{\"SortBy\":\"Name\",\"SortOrder\":\"Descending\"}"), REGION);
|
||||
|
||||
assertEquals(List.of("z-job", "a-job"), names(response, "TrainingJobSummaries", "TrainingJobName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listDefaultsToModeledCreationTimeOrder() throws Exception {
|
||||
InMemoryStorage<String, EndpointConfigResource> configs = new InMemoryStorage<>();
|
||||
EndpointConfigResource oldConfig = endpointConfig("z-config");
|
||||
oldConfig.creationTime = 1000;
|
||||
EndpointConfigResource newConfig = endpointConfig("a-config");
|
||||
newConfig.creationTime = 3000;
|
||||
configs.put(REGION + "::z-config", oldConfig);
|
||||
configs.put(REGION + "::a-config", newConfig);
|
||||
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
EndpointResource oldEndpoint = endpoint("z-endpoint");
|
||||
oldEndpoint.creationTime = 1000;
|
||||
EndpointResource newEndpoint = endpoint("a-endpoint");
|
||||
newEndpoint.creationTime = 3000;
|
||||
endpoints.put(REGION + "::z-endpoint", oldEndpoint);
|
||||
endpoints.put(REGION + "::a-endpoint", newEndpoint);
|
||||
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
TrainingJobResource oldJob = trainingJob("z-job");
|
||||
oldJob.creationTime = 1000;
|
||||
TrainingJobResource newJob = trainingJob("a-job");
|
||||
newJob.creationTime = 3000;
|
||||
jobs.put(REGION + "::z-job", oldJob);
|
||||
jobs.put(REGION + "::a-job", newJob);
|
||||
|
||||
SageMakerService service = service(configs, endpoints, jobs);
|
||||
JsonNode emptyRequest = mapper.readTree("{}");
|
||||
assertEquals(List.of("a-config", "z-config"),
|
||||
names(service.listEndpointConfigs(emptyRequest, REGION), "EndpointConfigs", "EndpointConfigName"));
|
||||
assertEquals(List.of("a-endpoint", "z-endpoint"),
|
||||
names(service.listEndpoints(emptyRequest, REGION), "Endpoints", "EndpointName"));
|
||||
assertEquals(List.of("z-job", "a-job"),
|
||||
names(service.listTrainingJobs(emptyRequest, REGION), "TrainingJobSummaries", "TrainingJobName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listStatusSortUsesStatusThenName() throws Exception {
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
EndpointResource creating = endpoint("z-endpoint");
|
||||
creating.endpointStatus = "Creating";
|
||||
EndpointResource serving = endpoint("a-endpoint");
|
||||
serving.endpointStatus = "InService";
|
||||
endpoints.put(REGION + "::z-endpoint", creating);
|
||||
endpoints.put(REGION + "::a-endpoint", serving);
|
||||
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
TrainingJobResource completed = trainingJob("z-job");
|
||||
TrainingJobResource running = trainingJob("a-job");
|
||||
running.trainingJobStatus = "InProgress";
|
||||
jobs.put(REGION + "::z-job", completed);
|
||||
jobs.put(REGION + "::a-job", running);
|
||||
|
||||
SageMakerService service = service(new InMemoryStorage<>(), endpoints, jobs);
|
||||
JsonNode request = mapper.readTree("{\"SortBy\":\"Status\",\"SortOrder\":\"Ascending\"}");
|
||||
assertEquals(List.of("z-endpoint", "a-endpoint"),
|
||||
names(service.listEndpoints(request, REGION), "Endpoints", "EndpointName"));
|
||||
assertEquals(List.of("z-job", "a-job"),
|
||||
names(service.listTrainingJobs(request, REGION), "TrainingJobSummaries", "TrainingJobName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTrainingJobsUsesSortedStatusForCursorWhenJobChangesBeforeRendering() throws Exception {
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
TrainingJobResource first = trainingJob("b-job");
|
||||
first.trainingJobStatus = "InProgress";
|
||||
TrainingJobResource second = trainingJob("a-job");
|
||||
second.trainingJobStatus = "InProgress";
|
||||
jobs.put(REGION + "::b-job", first);
|
||||
jobs.put(REGION + "::a-job", second);
|
||||
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), jobs,
|
||||
mapperMutatingBeforeRender(() -> first.trainingJobStatus = "Completed"));
|
||||
ObjectNode request = mapper.createObjectNode()
|
||||
.put("SortBy", "Status").put("SortOrder", "Descending").put("MaxResults", 1);
|
||||
|
||||
JsonNode firstPage = service.listTrainingJobs(request, REGION);
|
||||
assertEquals(List.of("b-job"), names(firstPage, "TrainingJobSummaries", "TrainingJobName"));
|
||||
assertEquals("InProgress", firstPage.path("TrainingJobSummaries").get(0).path("TrainingJobStatus").asText());
|
||||
assertEquals("Completed", first.trainingJobStatus);
|
||||
|
||||
JsonNode secondPage = service.listTrainingJobs(request.deepCopy()
|
||||
.put("NextToken", firstPage.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("a-job"), names(secondPage, "TrainingJobSummaries", "TrainingJobName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEndpointsUsesSortedStatusForCursorWhenEndpointChangesBeforeRendering() throws Exception {
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
EndpointResource first = endpoint("b-endpoint");
|
||||
EndpointResource second = endpoint("a-endpoint");
|
||||
endpoints.put(REGION + "::b-endpoint", first);
|
||||
endpoints.put(REGION + "::a-endpoint", second);
|
||||
SageMakerService service = service(new InMemoryStorage<>(), endpoints, new InMemoryStorage<>(),
|
||||
mapperMutatingBeforeRender(() -> first.endpointStatus = "Creating"));
|
||||
ObjectNode request = mapper.createObjectNode()
|
||||
.put("SortBy", "Status").put("SortOrder", "Descending").put("MaxResults", 1);
|
||||
|
||||
JsonNode firstPage = service.listEndpoints(request, REGION);
|
||||
assertEquals(List.of("b-endpoint"), names(firstPage, "Endpoints", "EndpointName"));
|
||||
assertEquals("InService", firstPage.path("Endpoints").get(0).path("EndpointStatus").asText());
|
||||
assertEquals("Creating", first.endpointStatus);
|
||||
|
||||
JsonNode secondPage = service.listEndpoints(request.deepCopy()
|
||||
.put("NextToken", firstPage.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("a-endpoint"), names(secondPage, "Endpoints", "EndpointName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listCreationTimeCursorResumesAfterAnchorDeletion() throws Exception {
|
||||
InMemoryStorage<String, EndpointConfigResource> configs = new InMemoryStorage<>();
|
||||
for (String name : List.of("a", "m", "z")) {
|
||||
EndpointConfigResource config = endpointConfig(name);
|
||||
config.creationTime = name.equals("z") ? 3000 : 2000;
|
||||
configs.put(REGION + "::" + name, config);
|
||||
}
|
||||
SageMakerService service = service(configs, new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
JsonNode first = service.listEndpointConfigs(mapper.readTree("{\"MaxResults\":1}"), REGION);
|
||||
assertEquals(List.of("z"), names(first, "EndpointConfigs", "EndpointConfigName"));
|
||||
String cursor = first.path("NextToken").asText();
|
||||
configs.delete(REGION + "::z");
|
||||
|
||||
JsonNode second = service.listEndpointConfigs(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("NextToken", cursor), REGION);
|
||||
assertEquals(List.of("m"), names(second, "EndpointConfigs", "EndpointConfigName"));
|
||||
JsonNode third = service.listEndpointConfigs(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("NextToken", second.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("a"), names(third, "EndpointConfigs", "EndpointConfigName"));
|
||||
|
||||
AwsException changedOrder = assertThrows(AwsException.class, () -> service.listEndpointConfigs(
|
||||
mapper.createObjectNode().put("SortOrder", "Ascending").put("NextToken", cursor), REGION));
|
||||
assertEquals("ValidationException", changedOrder.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listEndpointAndTrainingCursorsResumeAfterAnchorDeletion() throws Exception {
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
EndpointResource olderEndpoint = endpoint("a-endpoint");
|
||||
olderEndpoint.creationTime = 1000;
|
||||
EndpointResource newerEndpoint = endpoint("z-endpoint");
|
||||
newerEndpoint.creationTime = 3000;
|
||||
endpoints.put(REGION + "::a-endpoint", olderEndpoint);
|
||||
endpoints.put(REGION + "::z-endpoint", newerEndpoint);
|
||||
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
TrainingJobResource olderJob = trainingJob("a-job");
|
||||
olderJob.creationTime = 1000;
|
||||
TrainingJobResource newerJob = trainingJob("z-job");
|
||||
newerJob.creationTime = 3000;
|
||||
jobs.put(REGION + "::a-job", olderJob);
|
||||
jobs.put(REGION + "::z-job", newerJob);
|
||||
|
||||
SageMakerService service = service(new InMemoryStorage<>(), endpoints, jobs);
|
||||
JsonNode endpointFirst = service.listEndpoints(mapper.readTree("{\"MaxResults\":1}"), REGION);
|
||||
assertEquals(List.of("z-endpoint"), names(endpointFirst, "Endpoints", "EndpointName"));
|
||||
endpoints.delete(REGION + "::z-endpoint");
|
||||
JsonNode endpointNext = service.listEndpoints(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("NextToken", endpointFirst.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("a-endpoint"), names(endpointNext, "Endpoints", "EndpointName"));
|
||||
|
||||
JsonNode jobFirst = service.listTrainingJobs(mapper.readTree("{\"MaxResults\":1}"), REGION);
|
||||
assertEquals(List.of("a-job"), names(jobFirst, "TrainingJobSummaries", "TrainingJobName"));
|
||||
jobs.delete(REGION + "::a-job");
|
||||
JsonNode jobNext = service.listTrainingJobs(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("NextToken", jobFirst.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("z-job"), names(jobNext, "TrainingJobSummaries", "TrainingJobName"));
|
||||
jobs.delete(REGION + "::z-job");
|
||||
|
||||
EndpointResource firstStatusEndpoint = endpoint("a-status");
|
||||
firstStatusEndpoint.endpointStatus = "Creating";
|
||||
EndpointResource nextStatusEndpoint = endpoint("z-status");
|
||||
nextStatusEndpoint.endpointStatus = "Creating";
|
||||
endpoints.put(REGION + "::a-status", firstStatusEndpoint);
|
||||
endpoints.put(REGION + "::z-status", nextStatusEndpoint);
|
||||
JsonNode statusEndpointFirst = service.listEndpoints(mapper.readTree(
|
||||
"{\"MaxResults\":1,\"SortBy\":\"Status\",\"SortOrder\":\"Ascending\"}"), REGION);
|
||||
assertEquals(List.of("a-status"), names(statusEndpointFirst, "Endpoints", "EndpointName"));
|
||||
endpoints.delete(REGION + "::a-status");
|
||||
JsonNode statusEndpointNext = service.listEndpoints(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("SortBy", "Status").put("SortOrder", "Ascending")
|
||||
.put("NextToken", statusEndpointFirst.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("z-status"), names(statusEndpointNext, "Endpoints", "EndpointName"));
|
||||
|
||||
TrainingJobResource firstStatusJob = trainingJob("a-status-job");
|
||||
firstStatusJob.trainingJobStatus = "Completed";
|
||||
TrainingJobResource nextStatusJob = trainingJob("z-status-job");
|
||||
nextStatusJob.trainingJobStatus = "Completed";
|
||||
jobs.put(REGION + "::a-status-job", firstStatusJob);
|
||||
jobs.put(REGION + "::z-status-job", nextStatusJob);
|
||||
JsonNode statusJobFirst = service.listTrainingJobs(mapper.readTree(
|
||||
"{\"MaxResults\":1,\"SortBy\":\"Status\",\"SortOrder\":\"Ascending\"}"), REGION);
|
||||
assertEquals(List.of("a-status-job"), names(statusJobFirst, "TrainingJobSummaries", "TrainingJobName"));
|
||||
jobs.delete(REGION + "::a-status-job");
|
||||
JsonNode statusJobNext = service.listTrainingJobs(mapper.createObjectNode()
|
||||
.put("MaxResults", 1).put("SortBy", "Status").put("SortOrder", "Ascending")
|
||||
.put("NextToken", statusJobFirst.path("NextToken").asText()), REGION);
|
||||
assertEquals(List.of("z-status-job"), names(statusJobNext, "TrainingJobSummaries", "TrainingJobName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listOpaqueCursorsRejectAnotherRegionOrOperation() throws Exception {
|
||||
InMemoryStorage<String, EndpointConfigResource> configs = new InMemoryStorage<>();
|
||||
EndpointConfigResource older = endpointConfig("older");
|
||||
older.creationTime = 1000;
|
||||
EndpointConfigResource newer = endpointConfig("newer");
|
||||
newer.creationTime = 3000;
|
||||
configs.put(REGION + "::older", older);
|
||||
configs.put(REGION + "::newer", newer);
|
||||
|
||||
InMemoryStorage<String, EndpointResource> endpoints = new InMemoryStorage<>();
|
||||
endpoints.put(REGION + "::endpoint", endpoint("endpoint"));
|
||||
SageMakerService service = service(configs, endpoints, new InMemoryStorage<>());
|
||||
JsonNode first = service.listEndpointConfigs(mapper.createObjectNode().put("MaxResults", 1), REGION);
|
||||
String cursor = first.path("NextToken").asText();
|
||||
JsonNode next = mapper.createObjectNode().put("NextToken", cursor);
|
||||
|
||||
AwsException otherRegion = assertThrows(AwsException.class,
|
||||
() -> service.listEndpointConfigs(next, "us-west-2"));
|
||||
assertEquals("ValidationException", otherRegion.getErrorCode());
|
||||
AwsException otherOperation = assertThrows(AwsException.class,
|
||||
() -> service.listEndpoints(next, REGION));
|
||||
assertEquals("ValidationException", otherOperation.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTrainingCursorRejectsChangedFilters() throws Exception {
|
||||
InMemoryStorage<String, TrainingJobResource> jobs = new InMemoryStorage<>();
|
||||
TrainingJobResource firstJob = trainingJob("alpha-first");
|
||||
firstJob.creationTime = 1000;
|
||||
TrainingJobResource secondJob = trainingJob("alpha-second");
|
||||
secondJob.creationTime = 3000;
|
||||
jobs.put(REGION + "::alpha-first", firstJob);
|
||||
jobs.put(REGION + "::alpha-second", secondJob);
|
||||
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), jobs);
|
||||
JsonNode first = service.listTrainingJobs(mapper.createObjectNode()
|
||||
.put("NameContains", "alpha").put("StatusEquals", "Completed").put("MaxResults", 1), REGION);
|
||||
String cursor = first.path("NextToken").asText();
|
||||
JsonNode sameFilters = service.listTrainingJobs(mapper.createObjectNode()
|
||||
.put("NameContains", "alpha").put("StatusEquals", "Completed").put("NextToken", cursor), REGION);
|
||||
assertEquals(List.of("alpha-second"), names(sameFilters, "TrainingJobSummaries", "TrainingJobName"));
|
||||
|
||||
AwsException changedName = assertThrows(AwsException.class, () -> service.listTrainingJobs(
|
||||
mapper.createObjectNode().put("NameContains", "beta").put("StatusEquals", "Completed")
|
||||
.put("NextToken", cursor), REGION));
|
||||
assertEquals("ValidationException", changedName.getErrorCode());
|
||||
AwsException changedStatus = assertThrows(AwsException.class, () -> service.listTrainingJobs(
|
||||
mapper.createObjectNode().put("NameContains", "alpha").put("StatusEquals", "Failed")
|
||||
.put("NextToken", cursor), REGION));
|
||||
assertEquals("ValidationException", changedStatus.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRejectsUnmodeledSortValues() throws Exception {
|
||||
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
AwsException invalidConfigKey = assertThrows(AwsException.class, () -> service.listEndpointConfigs(
|
||||
mapper.readTree("{\"SortBy\":\"Status\"}"), REGION));
|
||||
assertEquals("ValidationException", invalidConfigKey.getErrorCode());
|
||||
AwsException invalidOrder = assertThrows(AwsException.class, () -> service.listEndpoints(
|
||||
mapper.readTree("{\"SortOrder\":\"Newest\"}"), REGION));
|
||||
assertEquals("ValidationException", invalidOrder.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listOperationsReportBothInvalidSortValues() throws Exception {
|
||||
SageMakerService service = service(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
JsonNode request = mapper.readTree("{\"SortBy\":\"BadBy\",\"SortOrder\":\"BadOrder\"}");
|
||||
String sortOrderError = "Value 'BadOrder' at 'sortOrder' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [Ascending, Descending]";
|
||||
|
||||
String twoValueSortByError = "Value 'BadBy' at 'sortBy' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [Name, CreationTime, Status]";
|
||||
String twoValueMessage = "2 validation errors detected: " + twoValueSortByError + "; " + sortOrderError;
|
||||
for (Supplier<ObjectNode> operation : List.of(
|
||||
(Supplier<ObjectNode>) () -> service.listEndpoints(request, REGION),
|
||||
() -> service.listTrainingJobs(request, REGION))) {
|
||||
AwsException error = assertThrows(AwsException.class, operation::get);
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals(twoValueMessage, error.getMessage());
|
||||
}
|
||||
|
||||
String modelSortByError = "Value 'BadBy' at 'sortBy' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [Name, CreationTime]";
|
||||
String modelMessage = "2 validation errors detected: " + modelSortByError + "; " + sortOrderError;
|
||||
for (Supplier<ObjectNode> operation : List.of(
|
||||
(Supplier<ObjectNode>) () -> service.listModels(request, REGION),
|
||||
() -> service.listEndpointConfigs(request, REGION))) {
|
||||
AwsException error = assertThrows(AwsException.class, operation::get);
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
assertEquals(modelMessage, error.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private SageMakerService service(InMemoryStorage<String, EndpointConfigResource> configs,
|
||||
InMemoryStorage<String, EndpointResource> endpoints,
|
||||
InMemoryStorage<String, TrainingJobResource> jobs) {
|
||||
return service(configs, endpoints, jobs, mapper);
|
||||
}
|
||||
|
||||
private SageMakerService service(InMemoryStorage<String, EndpointConfigResource> configs,
|
||||
InMemoryStorage<String, EndpointResource> endpoints,
|
||||
InMemoryStorage<String, TrainingJobResource> jobs,
|
||||
ObjectMapper serviceMapper) {
|
||||
return new SageMakerService(new InMemoryStorage<String, ModelResource>(), configs, endpoints, jobs,
|
||||
new RegionResolver(REGION, "000000000000"), serviceMapper,
|
||||
mock(SageMakerEndpointManager.class), mock(SageMakerTrainingRunner.class));
|
||||
}
|
||||
|
||||
private static ObjectMapper mapperMutatingBeforeRender(Runnable mutation) {
|
||||
return new ObjectMapper() {
|
||||
private boolean mutated;
|
||||
|
||||
@Override
|
||||
public ObjectNode createObjectNode() {
|
||||
if (!mutated) {
|
||||
mutated = true;
|
||||
mutation.run();
|
||||
}
|
||||
return super.createObjectNode();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static EndpointResource endpoint(String name) {
|
||||
EndpointResource endpoint = new EndpointResource();
|
||||
endpoint.endpointName = name;
|
||||
endpoint.endpointArn = "arn:aws:sagemaker:us-east-1:000000000000:endpoint/" + name;
|
||||
endpoint.endpointStatus = "InService";
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
private static EndpointConfigResource endpointConfig(String name) {
|
||||
EndpointConfigResource config = new EndpointConfigResource();
|
||||
config.endpointConfigName = name;
|
||||
config.endpointConfigArn = "arn:aws:sagemaker:us-east-1:000000000000:endpoint-config/" + name;
|
||||
return config;
|
||||
}
|
||||
|
||||
private static TrainingJobResource trainingJob(String name) {
|
||||
TrainingJobResource job = new TrainingJobResource();
|
||||
job.trainingJobName = name;
|
||||
job.trainingJobArn = "arn:aws:sagemaker:us-east-1:000000000000:training-job/" + name;
|
||||
job.trainingJobStatus = "Completed";
|
||||
return job;
|
||||
}
|
||||
|
||||
private static List<String> names(JsonNode response, String itemsField, String nameField) {
|
||||
List<String> names = new ArrayList<>();
|
||||
response.path(itemsField).forEach(item -> names.add(item.path(nameField).asText()));
|
||||
return names;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -214,11 +214,11 @@ class SageMakerServiceTest {
|
||||
new RegionResolver("us-east-1", "000000000000"), mapper,
|
||||
mock(SageMakerEndpointManager.class), mock(SageMakerTrainingRunner.class));
|
||||
|
||||
JsonNode firstPage = service.listEndpointConfigs(mapper.readTree("{\"MaxResults\":1}"), "us-east-1");
|
||||
JsonNode firstPage = service.listEndpointConfigs(mapper.readTree("{\"MaxResults\":1,\"SortBy\":\"Name\",\"SortOrder\":\"Ascending\"}"), "us-east-1");
|
||||
assertEquals("a", firstPage.path("EndpointConfigs").get(0).path("EndpointConfigName").asText());
|
||||
assertEquals("a", firstPage.path("NextToken").asText());
|
||||
configs.delete("us-east-1::a");
|
||||
JsonNode secondPage = service.listEndpointConfigs(mapper.readTree("{\"MaxResults\":1,\"NextToken\":\"a\"}"), "us-east-1");
|
||||
JsonNode secondPage = service.listEndpointConfigs(mapper.readTree("{\"MaxResults\":1,\"SortBy\":\"Name\",\"SortOrder\":\"Ascending\",\"NextToken\":\"a\"}"), "us-east-1");
|
||||
assertEquals("b", secondPage.path("EndpointConfigs").get(0).path("EndpointConfigName").asText());
|
||||
assertTrue(secondPage.path("NextToken").isMissingNode());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user