Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(apigateway): centralize /tags/{arn} routing in SharedTagsController (#330)
Kenshin Okinakad74affa0bcd1
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SharedTagsController.java b/src/main/java/io/github/hectorvent/floci/core/common/SharedTagsController.javanew file mode 100644index 0000000000..d5683c52ce--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/SharedTagsController.java@@ -0,0 +1,124 @@+package io.github.hectorvent.floci.core.common;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import jakarta.enterprise.inject.Instance;+import jakarta.inject.Inject;+import jakarta.ws.rs.Consumes;+import jakarta.ws.rs.DELETE;+import jakarta.ws.rs.GET;+import jakarta.ws.rs.PUT;+import jakarta.ws.rs.Path;+import jakarta.ws.rs.PathParam;+import jakarta.ws.rs.Produces;+import jakarta.ws.rs.QueryParam;+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.HashMap;+import java.util.List;+import java.util.Map;++/**+ * Dispatcher for AWS services that share the REST {@code /tags/{resourceArn}} path+ * (API Gateway, EventBridge Scheduler, EFS, ...).+ *+ * <p>AWS distinguishes these services by hostname, but floci serves every service on a+ * single port, so the path alone is ambiguous. This controller resolves the owning+ * service from the {@code service} segment of the request ARN+ * ({@code arn:aws:<service>:<region>:<account>:<resource>}) and dispatches to the+ * matching {@link TagHandler}.+ */+@Path("/tags")+@Produces(MediaType.APPLICATION_JSON)+public class SharedTagsController {++ private final Map<String, TagHandler> handlersByServiceKey;+ private final RegionResolver regionResolver;+ private final ObjectMapper objectMapper;++ @Inject+ public SharedTagsController(Instance<TagHandler> handlers,+ RegionResolver regionResolver,+ ObjectMapper objectMapper) {+ Map<String, TagHandler> map = new HashMap<>();+ for (TagHandler h : handlers) {+ String serviceKey = h.serviceKey();+ TagHandler existing = map.putIfAbsent(serviceKey, h);+ if (existing != null) {+ throw new IllegalStateException(+ "Duplicate TagHandler registration for service key '" + serviceKey+ + "': " + existing.getClass().getName()+ + " and " + h.getClass().getName());+ }+ }+ this.handlersByServiceKey = Map.copyOf(map);+ this.regionResolver = regionResolver;+ this.objectMapper = objectMapper;+ }++ @GET+ @Path("/{arn: .*}")+ public Response listTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {+ TagHandler handler = resolveHandler(arn);+ String region = regionResolver.resolveRegion(headers);+ Map<String, String> tags = handler.listTags(region, arn);+ ObjectNode root = objectMapper.createObjectNode();+ ObjectNode tagsNode = root.putObject("tags");+ tags.forEach(tagsNode::put);+ return Response.ok(root).build();+ }++ @PUT+ @Path("/{arn: .*}")+ @Consumes(MediaType.APPLICATION_JSON)+ public Response tagResource(@Context HttpHeaders headers,+ @PathParam("arn") String arn,+ String body) {+ TagHandler handler = resolveHandler(arn);+ String region = regionResolver.resolveRegion(headers);+ try {+ JsonNode node = objectMapper.readTree(body);+ Map<String, String> tags = new HashMap<>();+ node.path("tags").fields().forEachRemaining(e -> tags.put(e.getKey(), e.getValue().asText()));+ handler.tagResource(region, arn, tags);+ return Response.noContent().build();+ } catch (AwsException e) {+ throw e;+ } catch (Exception e) {+ throw new AwsException("BadRequestException", e.getMessage(), 400);+ }+ }++ @DELETE+ @Path("/{arn: .*}")+ public Response untagResource(@Context HttpHeaders headers,+ @PathParam("arn") String arn,+ @QueryParam("tagKeys") List<String> tagKeys) {+ TagHandler handler = resolveHandler(arn);+ String region = regionResolver.resolveRegion(headers);+ handler.untagResource(region, arn, tagKeys);+ return Response.noContent().build();+ }++ private TagHandler resolveHandler(String arn) {+ // arn:aws:<service>:<region>:<account>:<resource>+ String[] parts = arn.split(":", 6);+ if (parts.length < 6 || !"arn".equals(parts[0])) {+ throw new AwsException("BadRequestException",+ "Invalid resource ARN: " + arn, 400);+ }+ String serviceKey = parts[2];+ TagHandler handler = handlersByServiceKey.get(serviceKey);+ if (handler == null) {+ // Surface an unregistered service as an invalid-ARN error so floci's+ // internal routing isn't leaked to the client.+ throw new AwsException("BadRequestException",+ "Invalid resource ARN: " + arn, 400);+ }+ return handler;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/core/common/TagHandler.java b/src/main/java/io/github/hectorvent/floci/core/common/TagHandler.javanew file mode 100644index 0000000000..7c56c4825f--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/TagHandler.java@@ -0,0 +1,32 @@+package io.github.hectorvent.floci.core.common;++import java.util.List;+import java.util.Map;++/**+ * Per-service handler for the REST tag endpoints that share the {@code /tags/{resourceArn}}+ * path (API Gateway, EventBridge Scheduler, EFS, etc.).+ *+ * <p>A single {@code SharedTagsController} routes all {@code /tags/{arn}} requests and+ * dispatches to the implementation whose {@link #serviceKey()} matches the {@code service}+ * segment of the request ARN ({@code arn:aws:<service>:<region>:<account>:<resource>}).+ *+ * <p>Implementations are responsible for parsing their own ARN resource format and raising+ * {@link io.github.hectorvent.floci.core.common.AwsException} on invalid input.+ */+public interface TagHandler {++ /**+ * The ARN {@code service} segment this handler responds to (e.g. {@code "apigateway"},+ * {@code "scheduler"}, {@code "elasticfilesystem"}). The {@code SharedTagsController}+ * dispatcher extracts the third colon-separated component of the request ARN and looks+ * up the handler whose {@code serviceKey()} equals that value.+ */+ String serviceKey();++ Map<String, String> listTags(String region, String arn);++ void tagResource(String region, String arn, Map<String, String> tags);++ void untagResource(String region, String arn, List<String> tagKeys);+}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.javaindex 54756469ea..66f352e2c6 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayController.java@@ -1020,20 +1020,6 @@ public class ApiGatewayController {return Response.noContent().build();}- // ──────────────────────────── Tags ────────────────────────────-- @GET- @Path("/tags/{arn: .*}")- public Response getTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {- String region = regionResolver.resolveRegion(headers);- String apiId = apiIdFromArn(arn);- Map<String, String> tags = service.getTags(region, apiId);- ObjectNode root = objectMapper.createObjectNode();- ObjectNode tagsNode = root.putObject("tags");- tags.forEach(tagsNode::put);- return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();- }-// ──────────────────────────── Helpers ────────────────────────────private ObjectNode toApiNode(RestApi api) {@@ -1259,9 +1245,4 @@ public class ApiGatewayController {return node;}- private String apiIdFromArn(String arn) {- String[] parts = arn.split("/restapis/");- if (parts.length < 2) return arn;- return parts[1].split("/")[0];- }}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagHandler.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagHandler.javanew file mode 100644index 0000000000..235a7592ef--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagHandler.java@@ -0,0 +1,55 @@+package io.github.hectorvent.floci.services.apigateway;++import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.TagHandler;+import jakarta.enterprise.context.ApplicationScoped;+import jakarta.inject.Inject;++import java.util.List;+import java.util.Map;++/**+ * {@link TagHandler} implementation for API Gateway.+ *+ * <p>ARN format: {@code arn:aws:apigateway:<region>::/restapis/<apiId>}.+ * The {@code apiId} is the canonical identifier the underlying {@link ApiGatewayService}+ * uses for its tag store.+ */+@ApplicationScoped+public class ApiGatewayTagHandler implements TagHandler {++ private final ApiGatewayService service;++ @Inject+ public ApiGatewayTagHandler(ApiGatewayService service) {+ this.service = service;+ }++ @Override+ public String serviceKey() {+ return "apigateway";+ }++ @Override+ public Map<String, String> listTags(String region, String arn) {+ return service.getTags(region, apiIdFromArn(arn));+ }++ @Override+ public void tagResource(String region, String arn, Map<String, String> tags) {+ service.tagResource(region, apiIdFromArn(arn), tags);+ }++ @Override+ public void untagResource(String region, String arn, List<String> tagKeys) {+ service.untagResource(region, apiIdFromArn(arn), tagKeys);+ }++ private static String apiIdFromArn(String arn) {+ String[] parts = arn.split("/restapis/");+ if (parts.length < 2) {+ throw new AwsException("BadRequestException", "Invalid resource ARN: " + arn, 400);+ }+ return parts[1].split("/")[0];+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagsController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagsController.javadeleted file mode 100644index 90ce85d2e7..0000000000--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayTagsController.java+++ /dev/null@@ -1,90 +0,0 @@-package io.github.hectorvent.floci.services.apigateway;--import io.github.hectorvent.floci.core.common.AwsException;-import io.github.hectorvent.floci.core.common.RegionResolver;-import com.fasterxml.jackson.databind.JsonNode;-import com.fasterxml.jackson.databind.ObjectMapper;-import com.fasterxml.jackson.databind.node.ObjectNode;-import jakarta.inject.Inject;-import jakarta.ws.rs.*;-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.List;-import java.util.Map;--/**- * AWS API Gateway tag endpoints at /tags/{resourceArn}.- */-@Path("/tags")-@Produces(MediaType.APPLICATION_JSON)-public class ApiGatewayTagsController {-- private final ApiGatewayService service;- private final RegionResolver regionResolver;- private final ObjectMapper objectMapper;-- @Inject- public ApiGatewayTagsController(ApiGatewayService service, RegionResolver regionResolver,- ObjectMapper objectMapper) {- this.service = service;- this.regionResolver = regionResolver;- this.objectMapper = objectMapper;- }-- @GET- @Path("/{arn: .*}")- public Response getTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {- String region = regionResolver.resolveRegion(headers);- String apiId = apiIdFromArn(arn);- Map<String, String> tags = service.getTags(region, apiId);- ObjectNode root = objectMapper.createObjectNode();- ObjectNode tagsNode = root.putObject("tags");- tags.forEach(tagsNode::put);- return Response.ok(root).build();- }-- @PUT- @Path("/{arn: .*}")- public Response tagResource(@Context HttpHeaders headers,- @PathParam("arn") String arn,- String body) {- String region = regionResolver.resolveRegion(headers);- String apiId = apiIdFromArn(arn);- try {- JsonNode node = objectMapper.readTree(body);- Map<String, String> tags = new java.util.HashMap<>();- node.path("tags").fields().forEachRemaining(e -> tags.put(e.getKey(), e.getValue().asText()));- service.tagResource(region, apiId, tags);- return Response.noContent().build();- } catch (AwsException e) {- throw e;- } catch (Exception e) {- throw new AwsException("BadRequestException", e.getMessage(), 400);- }- }-- @DELETE- @Path("/{arn: .*}")- public Response untagResource(@Context HttpHeaders headers,- @PathParam("arn") String arn,- @QueryParam("tagKeys") List<String> tagKeys) {- String region = regionResolver.resolveRegion(headers);- String apiId = apiIdFromArn(arn);- service.untagResource(region, apiId, tagKeys);- return Response.noContent().build();- }-- /**- * Extracts apiId from ARN: arn:aws:apigateway:region::/restapis/{apiId}- */- private String apiIdFromArn(String arn) {- String[] parts = arn.split("/restapis/");- if (parts.length < 2) {- throw new AwsException("BadRequestException", "Invalid resource ARN: " + arn, 400);- }- return parts[1].split("/")[0];- }-}