mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(ses): apply the ListTenants filter (#5060)
ListTenants now honours Filter.TENANT_NAME_CONTAINS and Filter.SENDING_STATUS the way SES v2 matches them, with the validation messages and precedence SES answers: the filter's keys and values, then the page size and an empty token, then the filter's values. The filter is applied before the page is cut, and a token continues only the filter it was taken with.
This commit is contained in:
1 parent
4d980a361f
commit
db40361a3c
10 files changed
+254
-29
No files matched your search
+1
-1
@@ -155,7 +155,7 @@ public class SesConfigurationSetService {
|
||||
return list(region, paging, pageSize, nextToken);
|
||||
}
|
||||
// Validated here as well as in page() so the size error wins over the name's length.
|
||||
paging.pageSize(pageSize);
|
||||
paging.checkRequest(pageSize, nextToken);
|
||||
int length = nameContains.codePointCount(0, nameContains.length());
|
||||
if (length < 3 || length > 64) {
|
||||
throw new AwsException("BadRequestException",
|
||||
|
||||
@@ -26,17 +26,16 @@ import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.coerceBooleanOrFalse;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.filterValues;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.requireFilterKeysAndValues;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.requireObjectOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMapMemberOrAbsent;
|
||||
@@ -155,15 +154,7 @@ public class SesIdentityController {
|
||||
Map<String, String> filter = stringMapMemberOrAbsent(request, "Filter");
|
||||
Integer pageSize = intMemberOrAbsent(request, "PageSize");
|
||||
String nextToken = stringMemberOrAbsent(request, "NextToken");
|
||||
Map<String, String> present = new LinkedHashMap<>();
|
||||
if (filter != null) {
|
||||
requireFilterKeysAndValues(filter, IDENTITY_FILTER_KEYS);
|
||||
filter.forEach((key, value) -> {
|
||||
if (value != null) {
|
||||
present.put(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
Map<String, String> present = filterValues(filter, IDENTITY_FILTER_KEYS);
|
||||
return emailIdentitiesPage(region, present, pageSize, nextToken);
|
||||
}
|
||||
|
||||
|
||||
@@ -220,7 +220,7 @@ public class SesIdentityService {
|
||||
? SesListPaging.V2_LIST_EMAIL_IDENTITIES
|
||||
: SesListPaging.V2_LIST_EMAIL_IDENTITIES_FILTERED;
|
||||
// Validated here as well as in page() so the size error wins over the filter's values.
|
||||
paging.pageSize(pageSize);
|
||||
paging.checkRequest(pageSize, nextToken);
|
||||
String name = v2FilterName(filter.get("IDENTITY_NAME_CONTAINS"));
|
||||
String type = v2FilterType(filter.get("IDENTITY_TYPE"));
|
||||
String status = v2FilterStatus(filter.get("VERIFICATION_STATUS"));
|
||||
|
||||
@@ -236,6 +236,18 @@ public enum SesListPaging {
|
||||
*/
|
||||
<T> PaginatedResult<T> page(String region, String scope, List<T> all, Function<T, String> cursorOf,
|
||||
Integer pageSize, String nextToken) {
|
||||
int limit = checkRequest(pageSize, nextToken);
|
||||
String boundTo = scope.isEmpty() ? "" : "#" + scope.length() + "#" + scope;
|
||||
return Pagination.paginate(all, cursorOf, limit, nextToken, namespace + "@" + region + boundTo,
|
||||
invalidToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* What SES checks before anything else in the request: the page size and an empty token, which
|
||||
* the tenant-style lists report together. Returns the page size to serve. {@link #page} runs it
|
||||
* too, so a service calls it first only when its own checks must come after these.
|
||||
*/
|
||||
int checkRequest(Integer pageSize, String nextToken) {
|
||||
boolean emptyToken = nextToken != null && nextToken.isEmpty() && emptyTokenInvalid;
|
||||
if (emptyToken && sizeAndEmptyTokenInvalid != null && pageSize != null
|
||||
&& (pageSize < 1 || pageSize > maxPageSize)) {
|
||||
@@ -245,9 +257,7 @@ public enum SesListPaging {
|
||||
if (emptyToken) {
|
||||
throw invalidToken.apply(nextToken);
|
||||
}
|
||||
String boundTo = scope.isEmpty() ? "" : "#" + scope.length() + "#" + scope;
|
||||
return Pagination.paginate(all, cursorOf, limit, nextToken, namespace + "@" + region + boundTo,
|
||||
invalidToken);
|
||||
return limit;
|
||||
}
|
||||
|
||||
int pageSize(Integer requested) {
|
||||
|
||||
@@ -23,13 +23,16 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.filterValues;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.intMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.putTimestamp;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringArrayOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMapMemberOrAbsent;
|
||||
import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent;
|
||||
|
||||
/**
|
||||
@@ -47,6 +50,9 @@ public class SesTenantController {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SesTenantController.class);
|
||||
|
||||
/** In the order SES prints them when it refuses a key. */
|
||||
private static final List<String> TENANT_FILTER_KEYS = List.of("SENDING_STATUS", "TENANT_NAME_CONTAINS");
|
||||
|
||||
private final SesTenantService tenantService;
|
||||
private final SesService sesService;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -123,10 +129,11 @@ public class SesTenantController {
|
||||
? objectMapper.createObjectNode()
|
||||
: objectMapper.readTree(body);
|
||||
requireJsonObject(request);
|
||||
Map<String, String> filter = stringMapMemberOrAbsent(request, "Filter");
|
||||
Integer pageSize = intMemberOrAbsent(request, "PageSize");
|
||||
String nextToken = stringMemberOrAbsent(request, "NextToken");
|
||||
PaginatedResult<Tenant> page = tenantService.listTenants(region, SesListPaging.V2_LIST_TENANTS,
|
||||
pageSize, nextToken);
|
||||
Map<String, String> present = filterValues(filter, TENANT_FILTER_KEYS);
|
||||
PaginatedResult<Tenant> page = tenantService.listTenants(region, present, pageSize, nextToken);
|
||||
ObjectNode result = objectMapper.createObjectNode();
|
||||
ArrayNode tenants = result.putArray("Tenants");
|
||||
for (Tenant t : page.items()) {
|
||||
|
||||
@@ -19,8 +19,9 @@ import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -148,6 +149,50 @@ public class SesTenantService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* ListTenants with a {@code Filter}, probed 2026-10-03. The controller has already checked the
|
||||
* filter's keys and values; SES then checks the page size and an empty token, the filter's
|
||||
* values, and last the token. The name is not trimmed and is found anywhere in a tenant's name
|
||||
* without regard to case, the keys combine, and a token is bound to the filter it came from.
|
||||
* Whether SES checks the name or the status first could not be observed.
|
||||
*/
|
||||
public PaginatedResult<Tenant> listTenants(String region, Map<String, String> filter, Integer pageSize,
|
||||
String nextToken) {
|
||||
SesListPaging paging = SesListPaging.V2_LIST_TENANTS;
|
||||
paging.checkRequest(pageSize, nextToken);
|
||||
String name = filterName(filter.get("TENANT_NAME_CONTAINS"));
|
||||
String status = filterSendingStatus(filter.get("SENDING_STATUS"));
|
||||
List<Tenant> matching = listTenants(region).stream()
|
||||
.filter(t -> name == null || t.tenantName().toLowerCase(Locale.ROOT).contains(name))
|
||||
.filter(t -> status == null || status.equals(t.sendingStatus()))
|
||||
.toList();
|
||||
// The status goes first: its values never hold the separator, so no name can pass for
|
||||
// another filter.
|
||||
String scope = filter.isEmpty() ? "" : Objects.toString(status, "") + "/" + Objects.toString(name, "");
|
||||
return paging.page(region, scope, matching, Tenant::tenantName, pageSize, nextToken);
|
||||
}
|
||||
|
||||
/** SES sets no upper bound short of the name's own: 65 characters were accepted. */
|
||||
private static String filterName(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value.codePointCount(0, value.length()) < 3) {
|
||||
throw new AwsException("BadRequestException", "TENANT_NAME_CONTAINS must be at least 3 characters.", 400);
|
||||
}
|
||||
return value.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static String filterSendingStatus(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return switch (value) {
|
||||
case "ENABLED", "REINSTATED", "DISABLED" -> value;
|
||||
default -> throw new AwsException("BadRequestException", "Invalid sending status <" + value + ">.", 400);
|
||||
};
|
||||
}
|
||||
|
||||
public void deleteTenant(String tenantName, String region) {
|
||||
deleteTenant(tenantName, region, tenant -> { });
|
||||
}
|
||||
|
||||
@@ -201,11 +201,15 @@ final class SesV2Json {
|
||||
}
|
||||
|
||||
/**
|
||||
* The model's own constraints on a {@code Filter} map, as the SES validation layer reports them:
|
||||
* every key in the enum, listed in the order SES prints it, and no empty value. A key is checked
|
||||
* before a value.
|
||||
* A {@code Filter} map checked against the model's own constraints, as the SES validation layer
|
||||
* reports them: every key in the enum, listed in the order SES prints it, and no empty value, a
|
||||
* key being checked before a value. Returns the entries that carry a value; an absent filter is
|
||||
* an empty one.
|
||||
*/
|
||||
static void requireFilterKeysAndValues(Map<String, String> filter, List<String> keys) {
|
||||
static Map<String, String> filterValues(Map<String, String> filter, List<String> keys) {
|
||||
if (filter == null) {
|
||||
return Map.of();
|
||||
}
|
||||
if (!keys.containsAll(filter.keySet())) {
|
||||
throw new AwsException("BadRequestException", "1 validation error detected: Value at 'filter' "
|
||||
+ "failed to satisfy constraint: Map keys must satisfy constraint: "
|
||||
@@ -216,6 +220,13 @@ final class SesV2Json {
|
||||
+ "failed to satisfy constraint: Map value must satisfy constraint: "
|
||||
+ "[Member must have length greater than or equal to 1]", 400);
|
||||
}
|
||||
Map<String, String> values = new LinkedHashMap<>();
|
||||
filter.forEach((key, value) -> {
|
||||
if (value != null) {
|
||||
values.put(key, value);
|
||||
}
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
static AwsException unexpectedStartError(JsonNode node) {
|
||||
|
||||
Reference in new issue
Block a user