feat(resourcegroups): support EC2 capacity reservation pools (#6011)

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-20 00:04:50 -04:00
1 parent 691499d335
commit be2cb677d0
28 files changed
+1772 -1

No files matched your search

@@ -391,6 +391,7 @@ public interface EmulatorConfig {
CodeDeployStorageConfig codedeploy();
TranscribeStorageConfig transcribe();
TaggingStorageConfig tagging();
ResourceGroupsStorageConfig resourceGroups();
ElasticBeanstalkStorageConfig elasticbeanstalk();
CloudTrailStorageConfig cloudtrail();
RumStorageConfig rum();
@@ -624,6 +625,13 @@ public interface EmulatorConfig {
long flushIntervalMs();
}
interface ResourceGroupsStorageConfig {
Optional<String> mode();
@WithDefault("5000")
long flushIntervalMs();
}
interface TaggingStorageConfig {
Optional<String> mode();
@@ -764,6 +772,7 @@ public interface EmulatorConfig {
AppConfigDataServiceConfig appconfigdata();
EcrServiceConfig ecr();
ResourceGroupsTaggingServiceConfig tagging();
ResourceGroupsServiceConfig resourceGroups();
BedrockServiceConfig bedrock();
BedrockRuntimeServiceConfig bedrockRuntime();
EksServiceConfig eks();
@@ -2330,6 +2339,11 @@ public interface EmulatorConfig {
String proxyImage();
}
interface ResourceGroupsServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface ResourceGroupsTaggingServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -58,6 +58,7 @@ import io.github.hectorvent.floci.services.opensearch.OpenSearchController;
import io.github.hectorvent.floci.services.pipes.PipesController;
import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
import io.github.hectorvent.floci.services.resourceexplorer2.ResourceExplorer2Controller;
import io.github.hectorvent.floci.services.resourcegroups.ResourceGroupsController;
import io.github.hectorvent.floci.services.route53.Route53Controller;
import io.github.hectorvent.floci.services.rum.RumController;
import io.github.hectorvent.floci.services.s3.S3Controller;
@@ -365,6 +366,11 @@ public class ResolvedServiceCatalog {
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("AmazonEC2ContainerRegistry_V20150921."), Set.of("ecr"), Set.of(), Set.of()),
descriptor("resource-groups", "resource-groups", config.services().resourceGroups().enabled(), true,
"resource-groups", storageMode(config.storage().services().resourceGroups().mode(), config.storage().mode()),
config.storage().services().resourceGroups().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON), Set.of(), Set.of("resource-groups"), Set.of(),
Set.of(ResourceGroupsController.class)),
descriptor("tagging", "tagging", config.services().tagging().enabled(), true,
"tagging", storageMode(config.storage().services().tagging().mode(), config.storage().mode()),
config.storage().services().tagging().flushIntervalMs(), null, ServiceProtocol.JSON,
@@ -269,6 +269,13 @@ public class IamActionRegistry {
rule("ses", "POST", "^/v2/email/update-configuration-sets/?$", "ses:UpdateConfigurationSet"),
rule("ses", "POST", "^/v2/email/vdm/recommendations/?$", "ses:ListRecommendations"),
// ── Resource Groups ─────────────────────────────────────────────────────
rule("resource-groups", "POST", "^/_rg/groups/?$", "resource-groups:CreateGroup"),
rule("resource-groups", "POST", "^/_rg/get-group/?$", "resource-groups:GetGroup"),
rule("resource-groups", "POST", "^/_rg/delete-group/?$", "resource-groups:DeleteGroup"),
rule("resource-groups", "POST", "^/_rg/group-resources/?$", "resource-groups:GroupResources"),
rule("resource-groups", "POST", "^/_rg/list-group-resources/?$", "resource-groups:ListGroupResources"),
// ── Kinesis ────────────────────────────────────────────────────────────
rule("kinesis", "POST", ".*", "kinesis:*")
);
@@ -108,10 +108,27 @@ public class ResourceArnBuilder {
case "ssm" -> List.of(buildSsmArn(ctx, region, accountId));
case "kms" -> List.of(buildKmsArn(path, region, accountId));
case "iam" -> buildIamArns(ctx, region, accountId);
case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));
default -> List.of("*");
};
}
private String buildResourceGroupArn(ContainerRequestContext ctx, String region, String accountId) {
JsonNode body = readJsonBody(ctx);
if (body == null) {
return "*";
}
String path = ctx.getUriInfo().getPath();
boolean create = path.endsWith("/groups") || path.endsWith("/groups/");
String group = create ? body.path("Name").asText()
: body.path("Group").asText(body.path("GroupName").asText());
if (group.isEmpty()) {
return "*";
}
return group.startsWith("arn:") ? group
: AwsArnUtils.Arn.of("resource-groups", region, accountId, "group/" + group).toString();
}
/** Uses the serving operation and the same raw names as the handler. */
public List<String> buildLogResources(String action, ContainerRequestContext ctx,
String region, String accountId, String partition) {
@@ -0,0 +1,92 @@
package io.github.hectorvent.floci.services.resourcegroups;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.services.resourcegroups.model.ResourceGroup;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.util.LinkedHashMap;
import java.util.Map;
@Path(ResourceGroupsRouteFilter.INTERNAL_PREFIX)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ResourceGroupsController {
private final ResourceGroupsService service;
private final RegionResolver regions;
private final RequestContext context;
private final ObjectMapper mapper;
@Inject
public ResourceGroupsController(ResourceGroupsService service, RegionResolver regions,
RequestContext context, ObjectMapper mapper) {
this.service = service;
this.regions = regions;
this.context = context;
this.mapper = mapper;
}
@POST
@Path("/groups")
public Response createGroup(@Context HttpHeaders headers, String body) {
ResourceGroup group = service.create(regions.resolveRegion(headers), context.getAccountId(), parse(body));
return Response.ok(Map.of("Group", groupMetadata(group), "Tags", group.tags(),
"GroupConfiguration", Map.of("Configuration", group.configuration(), "Status", "UPDATE_COMPLETE"))).build();
}
@POST
@Path("/get-group")
public Response getGroup(@Context HttpHeaders headers, String body) {
return Response.ok(Map.of("Group", groupMetadata(
service.get(regions.resolveRegion(headers), context.getAccountId(), parse(body))))).build();
}
@POST
@Path("/delete-group")
public Response deleteGroup(@Context HttpHeaders headers, String body) {
return Response.ok(Map.of("Group", groupMetadata(
service.delete(regions.resolveRegion(headers), context.getAccountId(), parse(body))))).build();
}
@POST
@Path("/group-resources")
public Response groupResources(@Context HttpHeaders headers, String body) {
return Response.ok(service.add(regions.resolveRegion(headers), context.getAccountId(), parse(body))).build();
}
@POST
@Path("/list-group-resources")
public Response listGroupResources(@Context HttpHeaders headers, String body) {
return Response.ok(service.list(regions.resolveRegion(headers), context.getAccountId(), parse(body))).build();
}
private Map<String, Object> groupMetadata(ResourceGroup group) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("GroupArn", group.arn());
result.put("Name", group.name());
if (group.description() != null) {
result.put("Description", group.description());
}
return result;
}
private JsonNode parse(String body) {
try {
return mapper.readTree(body == null ? "{}" : body);
} catch (JsonProcessingException error) {
throw new AwsException("BadRequestException", "The request must contain a JSON object.", 400);
}
}
}
@@ -0,0 +1,100 @@
package io.github.hectorvent.floci.services.resourcegroups;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.VendoredResources;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
final class ResourceGroupsRequestShapes {
private static final JsonNode MODEL = VendoredResources.read("aws/resource-groups-request-shapes.json",
"Resource Groups request shapes", input -> new ObjectMapper().readTree(input));
private static final Map<String, Pattern> PATTERNS = new HashMap<>();
static {
for (Map.Entry<String, JsonNode> shape : MODEL.path("shapes").properties()) {
if (shape.getValue().has("pattern")) {
PATTERNS.put(shape.getKey(), Pattern.compile(shape.getValue().get("pattern").asText()));
}
}
}
private ResourceGroupsRequestShapes() {
}
static void check(String operation, JsonNode request) {
checkValue(MODEL.path("operations").path(operation).asText(), request, operation);
}
private static void checkValue(String name, JsonNode value, String path) {
JsonNode shape = MODEL.path("shapes").path(name);
String type = shape.path("type").asText();
boolean validType = value != null && switch (type) {
case "structure", "map" -> value.isObject();
case "list" -> value.isArray();
case "string" -> value.isTextual();
case "integer", "long" -> value.isIntegralNumber() && value.canConvertToLong();
default -> false;
};
if (!validType) {
throw bad(path + " must be a " + type + ".");
}
long size = switch (type) {
case "string" -> value.textValue().codePointCount(0, value.textValue().length());
case "integer", "long" -> value.longValue();
default -> value.size();
};
if (shape.has("min") && size < shape.get("min").asLong()
|| shape.has("max") && size > shape.get("max").asLong()) {
throw bad(path + " is outside the modeled bounds.");
}
if (PATTERNS.containsKey(name) && !PATTERNS.get(name).matcher(value.asText()).matches()) {
throw bad(path + " does not match the modeled pattern.");
}
if (shape.has("enum")) {
boolean known = false;
for (JsonNode choice : shape.get("enum")) {
known |= choice.equals(value);
}
if (!known) {
throw bad(path + " is not a modeled value.");
}
}
switch (type) {
case "structure" -> {
for (JsonNode required : shape.path("required")) {
if (!value.hasNonNull(required.asText())) {
throw bad(path + "." + required.asText() + " is required.");
}
}
for (Map.Entry<String, JsonNode> field : value.properties()) {
JsonNode member = shape.path("members").get(field.getKey());
if (member != null && !field.getValue().isNull()) {
checkValue(member.asText(), field.getValue(), path + "." + field.getKey());
}
}
}
case "list" -> {
for (JsonNode item : value) {
checkValue(shape.get("member").asText(), item, path + "[]");
}
}
case "map" -> {
for (Map.Entry<String, JsonNode> field : value.properties()) {
checkValue(shape.get("key").asText(), TextNode.valueOf(field.getKey()), path + ".key");
checkValue(shape.get("value").asText(), field.getValue(), path + "." + field.getKey());
}
}
default -> {
}
}
}
private static AwsException bad(String message) {
return new AwsException("BadRequestException", message, 400);
}
}
@@ -0,0 +1,30 @@
package io.github.hectorvent.floci.services.resourcegroups;
import io.github.hectorvent.floci.core.common.ResolvedServiceCatalog;
import io.github.hectorvent.floci.core.common.SignedServiceRoute;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.ext.Provider;
import java.util.Set;
@Provider
@PreMatching
public class ResourceGroupsRouteFilter implements ContainerRequestFilter {
static final String INTERNAL_PREFIX = "/_rg";
private static final Set<String> PUBLIC_PATHS = Set.of(
"/groups", "/get-group", "/delete-group", "/group-resources", "/list-group-resources");
private final SignedServiceRoute route;
@Inject
public ResourceGroupsRouteFilter(ResolvedServiceCatalog catalog) {
route = new SignedServiceRoute(catalog, "resource-groups", INTERNAL_PREFIX, PUBLIC_PATHS::contains);
}
@Override
public void filter(ContainerRequestContext ctx) {
route.apply(ctx);
}
}
@@ -0,0 +1,345 @@
package io.github.hectorvent.floci.services.resourcegroups;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.CapacityReservation;
import io.github.hectorvent.floci.services.resourcegroups.model.ResourceGroup;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.util.ArrayList;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ApplicationScoped
public class ResourceGroupsService implements Resettable {
static final String RESOURCE_TYPE = "AWS::EC2::CapacityReservation";
private static final String POOL_TYPE = "AWS::EC2::CapacityReservationPool";
private static final String GENERIC_TYPE = "AWS::ResourceGroups::Generic";
static final int MAX_PAGES = 256;
static final long PAGE_IDLE_MILLIS = 15 * 60 * 1000L;
private final AccountAwareStorageBackend<ResourceGroup> groups;
private final Ec2Service ec2;
private final Clock clock;
private final Map<String, Page> pages = new LinkedHashMap<>();
private record Page(String groupArn, String generation, List<String> filter,
List<String> remaining, long lastRead) {}
@Inject
public ResourceGroupsService(StorageFactory storageFactory, Ec2Service ec2) {
this(storageFactory.create("resource-groups", "resource-groups.json",
new TypeReference<Map<String, ResourceGroup>>() {}), ec2, Clock.systemUTC());
}
ResourceGroupsService(AccountAwareStorageBackend<ResourceGroup> groups, Ec2Service ec2, Clock clock) {
this.groups = groups;
this.ec2 = ec2;
this.clock = clock;
}
public synchronized ResourceGroup create(String region, String account, JsonNode request) {
ResourceGroupsRequestShapes.check("CreateGroup", request);
String name = text(request, "Name", true);
validName(name);
if (name.toLowerCase(Locale.ROOT).startsWith("aws")) {
throw bad("Group names beginning with AWS are reserved.");
}
if (request.hasNonNull("ResourceQuery")) {
throw bad("Only service-configured capacity reservation pools are supported.");
}
List<Map<String, Object>> configuration = configuration(request.get("Configuration"));
if (request.hasNonNull("Criticality") || request.hasNonNull("Owner") || request.hasNonNull("DisplayName")) {
throw bad("Unsupported parameter for this operation");
}
String description = text(request, "Description", false);
Map<String, String> tags = tags(request.get("Tags"));
String key = key(region, name);
if (groups.getForAccount(account, key).isPresent()) {
throw bad("A resource group with this name already exists.");
}
ResourceGroup group = new ResourceGroup(name,
AwsArnUtils.Arn.of("resource-groups", region, account, "group/" + name).toString(),
description, tags, configuration, List.of(), UUID.randomUUID().toString());
groups.putForAccount(account, key, group);
return group;
}
public synchronized ResourceGroup get(String region, String account, JsonNode request) {
ResourceGroupsRequestShapes.check("GetGroup", request);
return selected(region, account, request);
}
public synchronized ResourceGroup delete(String region, String account, JsonNode request) {
ResourceGroupsRequestShapes.check("DeleteGroup", request);
ResourceGroup group = selected(region, account, request);
groups.deleteForAccount(account, key(region, group.name()));
pages.values().removeIf(page -> page.generation().equals(group.generation()));
return group;
}
public synchronized Map<String, Object> add(String region, String account, JsonNode request) {
ResourceGroupsRequestShapes.check("GroupResources", request);
ResourceGroup group = selected(region, account, request);
JsonNode resources = request.get("ResourceArns");
if (resources == null || !resources.isArray() || resources.isEmpty() || resources.size() > 10) {
throw bad("ResourceArns must contain one to ten resource ARNs.");
}
Set<String> requested = new LinkedHashSet<>();
for (JsonNode resource : resources) {
if (!resource.isTextual() || resource.asText().length() > 2048
|| !AwsArnUtils.isArn(resource.asText())) {
throw bad("ResourceArns must contain valid resource ARNs.");
}
requested.add(resource.asText());
}
List<String> succeeded = new ArrayList<>();
List<Map<String, String>> failed = new ArrayList<>();
for (String resource : requested) {
String failure = reservationFailure(region, account, resource);
if (failure == null) {
succeeded.add(resource);
} else {
failed.add(Map.of("ResourceArn", resource, "ErrorCode", failure,
"ErrorMessage", "The reservation identity or state does not allow membership."));
}
}
// Validate every lookup before changing the stored group, including unexpected backend failures.
Set<String> members = new LinkedHashSet<>(group.members());
members.addAll(succeeded);
groups.putForAccount(account, key(region, group.name()), group.withMembers(new ArrayList<>(members)));
return Map.of("Succeeded", succeeded, "Failed", failed, "Pending", List.of());
}
private String reservationFailure(String region, String account, String resource) {
AwsArnUtils.Arn arn = AwsArnUtils.parse(resource);
if (!arn.partition().equals(AwsRegions.partitionFor(region)) || !arn.service().equals("ec2")
|| !arn.resource().startsWith("capacity-reservation/")) {
return "ResourceArnValidationException";
}
if (!arn.region().equals(region)) {
return "ValidationException";
}
if (!arn.accountId().equals(account)
|| !arn.resource().matches("capacity-reservation/cr-[0-9a-f]{17}")) {
return "ResourceGroupsValidationException";
}
List<CapacityReservation> found;
try {
found = ec2.describeCapacityReservations(region,
List.of(arn.resource().substring("capacity-reservation/".length())), Map.of());
} catch (AwsException error) {
if ("InvalidCapacityReservationId.NotFound".equals(error.getErrorCode())) {
return "ResourceGroupsValidationException";
}
throw error;
}
if (found.size() != 1 || !resource.equals(found.getFirst().getCapacityReservationArn())
|| !account.equals(found.getFirst().getOwnerId())) {
throw new AwsException("InternalServerErrorException", "Reservation lookup returned an inconsistent identity.", 500);
}
return "active".equals(found.getFirst().getState()) ? null : "ResourceGroupsValidationException";
}
public synchronized Map<String, Object> list(String region, String account, JsonNode request) {
ResourceGroupsRequestShapes.check("ListGroupResources", request);
ResourceGroup group = selected(region, account, request);
int maximum = 50;
if (request.has("MaxResults")) {
JsonNode value = request.get("MaxResults");
if (!value.isIntegralNumber() || !value.canConvertToInt() || value.intValue() < 1 || value.intValue() > 50) {
throw bad("MaxResults must be between 1 and 50.");
}
maximum = value.intValue();
}
List<String> filter = filter(request.get("Filters"));
String token = text(request, "NextToken", false);
if (token != null && (token.length() > 8192 || !token.matches("[a-zA-Z0-9+/]*={0,2}"))) {
throw bad("NextToken must be a base64 token.");
}
long now = clock.millis();
pages.values().removeIf(page -> now - page.lastRead() >= PAGE_IDLE_MILLIS);
List<String> selected;
if (token == null) {
selected = group.members();
} else {
Page page = pages.get(token);
if (page == null || !page.groupArn().equals(group.arn())
|| !page.generation().equals(group.generation()) || !page.filter().equals(filter)) {
throw bad("NextToken does not match this group and filter, or has expired.");
}
pages.remove(token);
pages.put(token, new Page(page.groupArn(), page.generation(), page.filter(), page.remaining(), now));
selected = page.remaining().stream().filter(group.members()::contains).toList();
}
List<Map<String, String>> identifiers = selected.stream().limit(maximum)
.map(arn -> Map.of("ResourceArn", arn, "ResourceType", RESOURCE_TYPE)).toList();
Map<String, Object> response = new LinkedHashMap<>();
response.put("ResourceIdentifiers", identifiers);
response.put("Resources", identifiers.stream().map(identifier -> Map.of("Identifier", identifier)).toList());
if (selected.size() > maximum) {
if (pages.size() >= MAX_PAGES) {
pages.remove(pages.keySet().iterator().next());
}
String next = Base64.getEncoder().encodeToString(
UUID.randomUUID().toString().getBytes(StandardCharsets.US_ASCII));
pages.put(next, new Page(group.arn(), group.generation(), filter,
List.copyOf(selected.subList(maximum, selected.size())), now));
response.put("NextToken", next);
}
return response;
}
private ResourceGroup selected(String region, String account, JsonNode request) {
if (request.has("Group") && request.has("GroupName")) {
throw bad("Specify Group or GroupName, not both.");
}
String selected = text(request, request.has("Group") ? "Group" : "GroupName", true);
String name = selected;
if (selected.startsWith("arn:")) {
if (!AwsArnUtils.isArn(selected)) {
throw bad("Invalid group ARN.");
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(selected);
if (!arn.partition().equals(AwsRegions.partitionFor(region)) || !arn.service().equals("resource-groups")
|| !arn.region().equals(region) || !arn.accountId().equals(account)
|| !arn.resource().startsWith("group/")) {
throw bad("The group ARN must belong to the calling account and region.");
}
name = arn.resource().substring("group/".length());
}
validName(name);
ResourceGroup group = groups.getForAccount(account, key(region, name))
.orElseThrow(() -> new AwsException("NotFoundException", "The resource group was not found.", 404));
String expected = AwsArnUtils.Arn.of("resource-groups", region, account, "group/" + name).toString();
if (!group.arn().equals(expected)) {
throw new AwsException("InternalServerErrorException", "Stored group identity is inconsistent.", 500);
}
return group;
}
private static List<Map<String, Object>> configuration(JsonNode configuration) {
if (configuration == null || !configuration.isArray() || configuration.size() != 2) {
throw bad("A capacity reservation pool Configuration is required.");
}
Set<String> types = new LinkedHashSet<>();
for (JsonNode item : configuration) {
requireObject(item);
String type = text(item, "Type", true);
if (!types.add(type)) {
throw bad("Configuration types must be distinct.");
}
JsonNode parameters = item.get("Parameters");
if (POOL_TYPE.equals(type)) {
if (parameters != null && (!parameters.isArray() || !parameters.isEmpty())) {
throw bad("CapacityReservationPool does not accept these parameters.");
}
} else if (GENERIC_TYPE.equals(type)) {
if (parameters == null || !parameters.isArray() || parameters.size() != 1) {
throw bad("The allowed-resource-types parameter is required.");
}
JsonNode parameter = parameters.get(0);
requireObject(parameter);
JsonNode values = parameter.get("Values");
if (!"allowed-resource-types".equals(text(parameter, "Name", true))
|| values == null || !values.isArray() || values.size() != 1
|| !values.get(0).isTextual() || !RESOURCE_TYPE.equals(values.get(0).asText())) {
throw bad("Only CapacityReservation resources are supported.");
}
} else {
throw bad("Only capacity reservation pool configurations are supported.");
}
}
return List.of(Map.of("Type", POOL_TYPE), Map.of("Type", GENERIC_TYPE, "Parameters",
List.of(Map.of("Name", "allowed-resource-types", "Values", List.of(RESOURCE_TYPE)))));
}
private static List<String> filter(JsonNode filters) {
if (filters == null || filters.isArray() && filters.isEmpty()) {
return List.of();
}
if (!filters.isArray() || filters.size() != 1) {
throw bad("Only one resource-type filter is supported.");
}
JsonNode filter = filters.get(0);
requireObject(filter);
JsonNode values = filter.get("Values");
if (!"resource-type".equals(text(filter, "Name", true)) || values == null
|| !values.isArray() || values.isEmpty() || values.size() > 5) {
throw bad("Invalid resource-type filter.");
}
for (JsonNode value : values) {
if (!value.isTextual() || !RESOURCE_TYPE.equals(value.asText())) {
throw bad("The resource type is not allowed by the group configuration.");
}
}
return List.of(RESOURCE_TYPE);
}
private static Map<String, String> tags(JsonNode tags) {
Map<String, String> result = new LinkedHashMap<>();
if (tags == null || tags.isNull()) {
return result;
}
if (!tags.isObject() || tags.size() > 50) {
throw bad("Tags must be an object containing at most 50 entries.");
}
tags.fields().forEachRemaining(entry -> {
result.put(entry.getKey(), entry.getValue().asText());
});
return result;
}
private static void validName(String name) {
if (name == null || !name.matches("[A-Za-z0-9_.-]{1,300}")) {
throw bad("Invalid resource group name.");
}
}
private static String key(String region, String name) {
return region + "::" + name;
}
private static String text(JsonNode object, String field, boolean required) {
JsonNode value = object.get(field);
if ((value == null || value.isNull()) && !required) {
return null;
}
if (value == null || !value.isTextual() || required && value.asText().isEmpty()) {
throw bad(field + " must be a string.");
}
return value.asText();
}
private static void requireObject(JsonNode request) {
if (request == null || !request.isObject()) {
throw bad("A JSON object is required.");
}
}
private static AwsException bad(String message) {
return new AwsException("BadRequestException", message, 400);
}
@Override
public synchronized void clear() {
groups.clear();
pages.clear();
}
}
@@ -0,0 +1,14 @@
package io.github.hectorvent.floci.services.resourcegroups.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.List;
import java.util.Map;
@RegisterForReflection
public record ResourceGroup(String name, String arn, String description, Map<String, String> tags,
List<Map<String, Object>> configuration, List<String> members, String generation) {
public ResourceGroup withMembers(List<String> updated) {
return new ResourceGroup(name, arn, description, tags, configuration, List.copyOf(updated), generation);
}
}