Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(ses): split the dedicated-IP endpoints into SesDedicatedIpController (#3692)
Kenshin Okinakab8d78c8c9f26
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.javaindex 948b7693da..53e6968784 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java@@ -35,6 +35,7 @@ import io.github.hectorvent.floci.services.cloudfront.CloudFrontServingControlleimport io.github.hectorvent.floci.services.route53.Route53Controller;import io.github.hectorvent.floci.services.ses.SesContactController;import io.github.hectorvent.floci.services.ses.SesController;+import io.github.hectorvent.floci.services.ses.SesDedicatedIpController;import io.github.hectorvent.floci.services.ses.SesTemplateController;import io.github.hectorvent.floci.services.appsync.AppSyncController;import io.github.hectorvent.floci.services.rdsdata.RdsDataController;@@ -288,7 +289,7 @@ public class ResolvedServiceCatalog {protocols(ServiceProtocol.REST_JSON, ServiceProtocol.QUERY),Set.of(), Set.of("email", "ses", "sesv2"), Set.of(),Set.of(SesController.class, SesContactController.class,- SesTemplateController.class)),+ SesDedicatedIpController.class, SesTemplateController.class)),descriptor("es", "opensearch", config.services().opensearch().enabled(), true,"opensearch", storageMode(config.storage().services().opensearch().mode(), config.storage().mode()),config.storage().services().opensearch().flushIntervalMs(), null, ServiceProtocol.REST_JSON,diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.javaindex efba63cdcb..2942d18c1f 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java@@ -11,7 +11,6 @@ import io.github.hectorvent.floci.services.ses.model.BulkEmailEntry;import io.github.hectorvent.floci.services.ses.model.BulkEmailEntryResult;import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;import io.github.hectorvent.floci.services.ses.model.CustomVerificationEmailTemplate;-import io.github.hectorvent.floci.services.ses.model.DedicatedIpPool;import io.github.hectorvent.floci.services.ses.model.DeliveryOptions;import io.github.hectorvent.floci.services.ses.model.EmailTemplate;import io.github.hectorvent.floci.services.ses.model.EventDestination;@@ -1609,158 +1608,6 @@ public class SesController {}}- // ──────────────────────── Dedicated IP Pools ────────────────────────-- @POST- @Path("/dedicated-ip-pools")- public Response createDedicatedIpPool(@Context HttpHeaders headers, String body) {- String region = regionResolver.resolveRegion(headers);- try {- if (body == null || body.isBlank()) {- throw new AwsException("BadRequestException", "Request body is required.", 400);- }- JsonNode request = objectMapper.readTree(body);- requireJsonObject(request);- String poolName = readRequiredStringField(request, "PoolName");- JsonNode scalingNode = request.path("ScalingMode");- String scalingMode;- if (scalingNode.isMissingNode() || scalingNode.isNull()) {- scalingMode = null;- } else if (!scalingNode.isTextual()) {- throw new AwsException("BadRequestException",- "The ScalingMode parameter is invalid.", 400);- } else {- scalingMode = scalingNode.asText();- }- List<Tag> tags = parseTagsArray(request.path("Tags"));- sesService.createDedicatedIpPool(poolName, scalingMode, tags, region);- LOG.infov("SES V2 CreateDedicatedIpPool: {0}", poolName);- return Response.ok(objectMapper.createObjectNode()).build();- } catch (AwsException e) {- throw remapV1Exception(e);- } catch (com.fasterxml.jackson.core.JsonProcessingException e) {- throw new AwsException("BadRequestException", e.getMessage(), 400);- }- }-- @GET- @Path("/dedicated-ip-pools")- public Response listDedicatedIpPools(@Context HttpHeaders headers) {- String region = regionResolver.resolveRegion(headers);- ObjectNode result = objectMapper.createObjectNode();- ArrayNode pools = result.putArray("DedicatedIpPools");- sesService.listDedicatedIpPools(region).forEach(pools::add);- return Response.ok(result).build();- }-- @GET- @Path("/dedicated-ip-pools/{poolName}")- public Response getDedicatedIpPool(@Context HttpHeaders headers,- @PathParam("poolName") String poolName) {- String region = regionResolver.resolveRegion(headers);- DedicatedIpPool pool = sesService.getDedicatedIpPool(poolName, region);- ObjectNode result = objectMapper.createObjectNode();- // Built explicitly: the AWS DedicatedIpPool shape carries only PoolName and ScalingMode;- // the model's tags are exposed via ListTagsForResource, not here.- ObjectNode poolNode = result.putObject("DedicatedIpPool");- poolNode.put("PoolName", pool.getPoolName());- poolNode.put("ScalingMode", pool.getScalingMode());- return Response.ok(result).build();- }-- @DELETE- @Path("/dedicated-ip-pools/{poolName}")- public Response deleteDedicatedIpPool(@Context HttpHeaders headers,- @PathParam("poolName") String poolName) {- String region = regionResolver.resolveRegion(headers);- sesService.deleteDedicatedIpPool(poolName, region);- LOG.infov("SES V2 DeleteDedicatedIpPool: {0}", poolName);- return Response.ok(objectMapper.createObjectNode()).build();- }-- @PUT- @Path("/dedicated-ip-pools/{poolName}/scaling")- public Response putDedicatedIpPoolScalingAttributes(@Context HttpHeaders headers,- @PathParam("poolName") String poolName,- String body) {- String region = regionResolver.resolveRegion(headers);- try {- JsonNode request = readOptionBody(objectMapper, body);- String scalingMode = parseOptionString(request.path("ScalingMode"), "ScalingMode");- sesService.putDedicatedIpPoolScalingAttributes(poolName, scalingMode, region);- LOG.infov("SES V2 PutDedicatedIpPoolScalingAttributes on {0}", poolName);- return Response.ok(objectMapper.createObjectNode()).build();- } catch (AwsException e) {- throw remapV1Exception(e);- }- }-- // ──────────────────────── Dedicated IPs (IP-level) ────────────────────────-- @GET- @Path("/dedicated-ips")- public Response getDedicatedIps(@Context HttpHeaders headers) {- regionResolver.resolveRegion(headers);- // Floci does not model leased dedicated IPs, so the account has none.- ObjectNode result = objectMapper.createObjectNode();- result.putArray("DedicatedIps");- result.putNull("NextToken");- return Response.ok(result).build();- }-- @GET- @Path("/dedicated-ips/{ip}")- public Response getDedicatedIp(@Context HttpHeaders headers, @PathParam("ip") String ip) {- String region = regionResolver.resolveRegion(headers);- sesService.getDedicatedIp(ip, region);- return Response.ok(objectMapper.createObjectNode()).build();- }-- @PUT- @Path("/dedicated-ips/{ip}/pool")- public Response putDedicatedIpInPool(@Context HttpHeaders headers,- @PathParam("ip") String ip, String body) {- String region = regionResolver.resolveRegion(headers);- try {- JsonNode request = readOptionBody(objectMapper, body);- String destinationPoolName = parseOptionString(- request.path("DestinationPoolName"), "DestinationPoolName");- sesService.putDedicatedIpInPool(ip, destinationPoolName, region);- LOG.infov("SES V2 PutDedicatedIpInPool: {0}", ip);- return Response.ok(objectMapper.createObjectNode()).build();- } catch (AwsException e) {- throw remapV1Exception(e);- }- }-- @PUT- @Path("/dedicated-ips/{ip}/warmup")- public Response putDedicatedIpWarmupAttributes(@Context HttpHeaders headers,- @PathParam("ip") String ip, String body) {- String region = regionResolver.resolveRegion(headers);- try {- JsonNode request = readOptionBody(objectMapper, body);- sesService.putDedicatedIpWarmupAttributes(ip,- parseWarmupPercentage(request.path("WarmupPercentage")), region);- LOG.infov("SES V2 PutDedicatedIpWarmupAttributes: {0}", ip);- return Response.ok(objectMapper.createObjectNode()).build();- } catch (AwsException e) {- throw remapV1Exception(e);- }- }-- // JSON-layer concern only: a non-integer WarmupPercentage is a SerializationException,- // matching AWS. Required/range validation lives in the service.- private static Integer parseWarmupPercentage(JsonNode node) {- if (node.isMissingNode() || node.isNull()) {- return null;- }- if (!node.isInt()) {- throw new AwsException("SerializationException", null, 400);- }- return node.intValue();- }-// ──────────────────────────── Account ────────────────────────────@GETdiff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.javanew file mode 100644index 0000000000..9b81463f96--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesDedicatedIpController.java@@ -0,0 +1,208 @@+package io.github.hectorvent.floci.services.ses;++import com.fasterxml.jackson.core.JsonProcessingException;+import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ArrayNode;+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.services.ses.model.DedicatedIpPool;+import io.github.hectorvent.floci.services.ses.model.Tag;+import jakarta.inject.Inject;+import jakarta.ws.rs.Consumes;+import jakarta.ws.rs.DELETE;+import jakarta.ws.rs.GET;+import jakarta.ws.rs.POST;+import jakarta.ws.rs.PUT;+import jakarta.ws.rs.Path;+import jakarta.ws.rs.PathParam;+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 org.jboss.logging.Logger;++import java.util.List;++import static io.github.hectorvent.floci.services.ses.SesV2Json.parseOptionString;+import static io.github.hectorvent.floci.services.ses.SesV2Json.parseTagsArray;+import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody;+import static io.github.hectorvent.floci.services.ses.SesV2Json.readRequiredStringField;+import static io.github.hectorvent.floci.services.ses.SesV2Json.remapV1Exception;+import static io.github.hectorvent.floci.services.ses.SesV2Json.requireJsonObject;++/**+ * SES V2 dedicated IP pool and dedicated IP endpoints ({@code /v2/email/dedicated-ip-pools} and+ * {@code /v2/email/dedicated-ips}), split out of {@link SesController}. Every operation is a+ * single-domain call on {@link SesDedicatedIpService}, so this controller does not touch the+ * {@link SesService} facade; the account-level auto-warmup setting stays under {@code /account}.+ */+@Path("/v2/email")+@Produces(MediaType.APPLICATION_JSON)+@Consumes(MediaType.APPLICATION_JSON)+public class SesDedicatedIpController {++ private static final Logger LOG = Logger.getLogger(SesDedicatedIpController.class);++ private final SesDedicatedIpService dedicatedIpService;+ private final RegionResolver regionResolver;+ private final ObjectMapper objectMapper;++ @Inject+ public SesDedicatedIpController(SesDedicatedIpService dedicatedIpService,+ RegionResolver regionResolver, ObjectMapper objectMapper) {+ this.dedicatedIpService = dedicatedIpService;+ this.regionResolver = regionResolver;+ this.objectMapper = objectMapper;+ }++ @POST+ @Path("/dedicated-ip-pools")+ public Response createDedicatedIpPool(@Context HttpHeaders headers, String body) {+ String region = regionResolver.resolveRegion(headers);+ try {+ if (body == null || body.isBlank()) {+ throw new AwsException("BadRequestException", "Request body is required.", 400);+ }+ JsonNode request = objectMapper.readTree(body);+ requireJsonObject(request);+ String poolName = readRequiredStringField(request, "PoolName");+ JsonNode scalingNode = request.path("ScalingMode");+ String scalingMode;+ if (scalingNode.isMissingNode() || scalingNode.isNull()) {+ scalingMode = null;+ } else if (!scalingNode.isTextual()) {+ throw new AwsException("BadRequestException",+ "The ScalingMode parameter is invalid.", 400);+ } else {+ scalingMode = scalingNode.asText();+ }+ List<Tag> tags = parseTagsArray(request.path("Tags"));+ dedicatedIpService.createDedicatedIpPool(poolName, scalingMode, tags, region);+ LOG.infov("SES V2 CreateDedicatedIpPool: {0}", poolName);+ return Response.ok(objectMapper.createObjectNode()).build();+ } catch (AwsException e) {+ throw remapV1Exception(e);+ } catch (JsonProcessingException e) {+ throw new AwsException("BadRequestException", e.getMessage(), 400);+ }+ }++ @GET+ @Path("/dedicated-ip-pools")+ public Response listDedicatedIpPools(@Context HttpHeaders headers) {+ String region = regionResolver.resolveRegion(headers);+ ObjectNode result = objectMapper.createObjectNode();+ ArrayNode pools = result.putArray("DedicatedIpPools");+ dedicatedIpService.listDedicatedIpPools(region).forEach(pools::add);+ return Response.ok(result).build();+ }++ @GET+ @Path("/dedicated-ip-pools/{poolName}")+ public Response getDedicatedIpPool(@Context HttpHeaders headers,+ @PathParam("poolName") String poolName) {+ String region = regionResolver.resolveRegion(headers);+ DedicatedIpPool pool = dedicatedIpService.getDedicatedIpPool(poolName, region);+ ObjectNode result = objectMapper.createObjectNode();+ // Built explicitly: the AWS DedicatedIpPool shape carries only PoolName and ScalingMode;+ // the model's tags are exposed via ListTagsForResource, not here.+ ObjectNode poolNode = result.putObject("DedicatedIpPool");+ poolNode.put("PoolName", pool.getPoolName());+ poolNode.put("ScalingMode", pool.getScalingMode());+ return Response.ok(result).build();+ }++ @DELETE+ @Path("/dedicated-ip-pools/{poolName}")+ public Response deleteDedicatedIpPool(@Context HttpHeaders headers,+ @PathParam("poolName") String poolName) {+ String region = regionResolver.resolveRegion(headers);+ dedicatedIpService.deleteDedicatedIpPool(poolName, region);+ LOG.infov("SES V2 DeleteDedicatedIpPool: {0}", poolName);+ return Response.ok(objectMapper.createObjectNode()).build();+ }++ @PUT+ @Path("/dedicated-ip-pools/{poolName}/scaling")+ public Response putDedicatedIpPoolScalingAttributes(@Context HttpHeaders headers,+ @PathParam("poolName") String poolName,+ String body) {+ String region = regionResolver.resolveRegion(headers);+ try {+ JsonNode request = readOptionBody(objectMapper, body);+ String scalingMode = parseOptionString(request.path("ScalingMode"), "ScalingMode");+ dedicatedIpService.putDedicatedIpPoolScalingAttributes(poolName, scalingMode, region);+ LOG.infov("SES V2 PutDedicatedIpPoolScalingAttributes on {0}", poolName);+ return Response.ok(objectMapper.createObjectNode()).build();+ } catch (AwsException e) {+ throw remapV1Exception(e);+ }+ }++ @GET+ @Path("/dedicated-ips")+ public Response getDedicatedIps(@Context HttpHeaders headers) {+ regionResolver.resolveRegion(headers);+ // Floci does not model leased dedicated IPs, so the account has none.+ ObjectNode result = objectMapper.createObjectNode();+ result.putArray("DedicatedIps");+ result.putNull("NextToken");+ return Response.ok(result).build();+ }++ @GET+ @Path("/dedicated-ips/{ip}")+ public Response getDedicatedIp(@Context HttpHeaders headers, @PathParam("ip") String ip) {+ String region = regionResolver.resolveRegion(headers);+ dedicatedIpService.getDedicatedIp(ip, region);+ return Response.ok(objectMapper.createObjectNode()).build();+ }++ @PUT+ @Path("/dedicated-ips/{ip}/pool")+ public Response putDedicatedIpInPool(@Context HttpHeaders headers,+ @PathParam("ip") String ip, String body) {+ String region = regionResolver.resolveRegion(headers);+ try {+ JsonNode request = readOptionBody(objectMapper, body);+ String destinationPoolName = parseOptionString(+ request.path("DestinationPoolName"), "DestinationPoolName");+ dedicatedIpService.putDedicatedIpInPool(ip, destinationPoolName, region);+ LOG.infov("SES V2 PutDedicatedIpInPool: {0}", ip);+ return Response.ok(objectMapper.createObjectNode()).build();+ } catch (AwsException e) {+ throw remapV1Exception(e);+ }+ }++ @PUT+ @Path("/dedicated-ips/{ip}/warmup")+ public Response putDedicatedIpWarmupAttributes(@Context HttpHeaders headers,+ @PathParam("ip") String ip, String body) {+ String region = regionResolver.resolveRegion(headers);+ try {+ JsonNode request = readOptionBody(objectMapper, body);+ dedicatedIpService.putDedicatedIpWarmupAttributes(ip,+ parseWarmupPercentage(request.path("WarmupPercentage")), region);+ LOG.infov("SES V2 PutDedicatedIpWarmupAttributes: {0}", ip);+ return Response.ok(objectMapper.createObjectNode()).build();+ } catch (AwsException e) {+ throw remapV1Exception(e);+ }+ }++ // JSON-layer concern only: a non-integer WarmupPercentage is a SerializationException,+ // matching AWS. Required/range validation lives in the service.+ private static Integer parseWarmupPercentage(JsonNode node) {+ if (node.isMissingNode() || node.isNull()) {+ return null;+ }+ if (!node.isInt()) {+ throw new AwsException("SerializationException", null, 400);+ }+ return node.intValue();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.javaindex 9c8fdaf31e..a1f0859749 100644--- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java@@ -14,7 +14,6 @@ import io.github.hectorvent.floci.services.ses.model.CloudWatchDimensionConfigurimport io.github.hectorvent.floci.services.ses.model.ConfigurationSet;import io.github.hectorvent.floci.services.ses.model.Contact;import io.github.hectorvent.floci.services.ses.model.CustomVerificationEmailTemplate;-import io.github.hectorvent.floci.services.ses.model.DedicatedIpPool;import io.github.hectorvent.floci.services.ses.model.DeliveryOptions;import io.github.hectorvent.floci.services.ses.model.EmailTemplate;import io.github.hectorvent.floci.services.ses.model.EventDestination;@@ -79,7 +78,9 @@ public class SesService {// Account suppression attributes + the per-address suppression list (two stores) extracted to// SesSuppressionService. The facade delegates; its send filters read via it.private final SesSuppressionService suppressionService;- // Dedicated IP pools extracted to SesDedicatedIpService. The facade delegates.+ // Dedicated IP pools and IPs live in SesDedicatedIpService, which the v2 controller calls+ // directly; the facade only reaches it for the delivery-options pool probe and the+ // ARN-dispatched tagging.private final SesDedicatedIpService dedicatedIpService;// Contact lists and contacts (two stores) live in SesContactService, which the v2 controller// and the unsubscribe endpoint call directly; the facade only reaches it from the send-path@@ -888,7 +889,7 @@ public class SesService {public ConfigurationSet createConfigurationSet(ConfigurationSet configSet, String region) {return configSetService.createConfigurationSet(configSet, region,domain -> isVerifiedDomainIdentity(domain, region),- pool -> dedicatedIpPoolExists(pool, region));+ pool -> dedicatedIpService.dedicatedIpPoolExists(pool, region));}// The option operations live in the service; the facade only supplies the cross-domain probes@@ -901,7 +902,7 @@ public class SesService {public void setConfigurationSetDeliveryOptions(String configSetName, DeliveryOptions options, String region) {configSetService.setDeliveryOptions(configSetName, options, region,- pool -> dedicatedIpPoolExists(pool, region));+ pool -> dedicatedIpService.dedicatedIpPoolExists(pool, region));}public void setConfigurationSetReputationOptions(String configSetName, boolean metricsEnabled, String region) {@@ -1136,31 +1137,6 @@ public class SesService {throw new AwsException("NotFoundException", message, 404);}- // ──────────────────────── Dedicated IP Pools ────────────────────────-- // Storage lives in SesDedicatedIpService; the facade forwards.-- public DedicatedIpPool createDedicatedIpPool(String poolName, String scalingMode, List<Tag> tags,- String region) {- return dedicatedIpService.createDedicatedIpPool(poolName, scalingMode, tags, region);- }-- public DedicatedIpPool getDedicatedIpPool(String poolName, String region) {- return dedicatedIpService.getDedicatedIpPool(poolName, region);- }-- public boolean dedicatedIpPoolExists(String poolName, String region) {- return dedicatedIpService.dedicatedIpPoolExists(poolName, region);- }-- public List<String> listDedicatedIpPools(String region) {- return dedicatedIpService.listDedicatedIpPools(region);- }-- public void deleteDedicatedIpPool(String poolName, String region) {- dedicatedIpService.deleteDedicatedIpPool(poolName, region);- }-// ──────────────── Identity (sending authorization) policies ────────────────// One shared store behind the v1 (PutIdentityPolicy/GetIdentityPolicies/ListIdentityPolicies/// DeleteIdentityPolicy) and v2 (Create/Get/Update/DeleteEmailIdentityPolicy) APIs. Verified@@ -1214,25 +1190,6 @@ public class SesService {}-- // Dedicated IPs (IP-level) and pool scaling live in SesDedicatedIpService; the facade forwards.-- public void getDedicatedIp(String ip, String region) {- dedicatedIpService.getDedicatedIp(ip, region);- }-- public void putDedicatedIpInPool(String ip, String destinationPoolName, String region) {- dedicatedIpService.putDedicatedIpInPool(ip, destinationPoolName, region);- }-- public void putDedicatedIpWarmupAttributes(String ip, Integer warmupPercentage, String region) {- dedicatedIpService.putDedicatedIpWarmupAttributes(ip, warmupPercentage, region);- }-- public void putDedicatedIpPoolScalingAttributes(String poolName, String scalingMode, String region) {- dedicatedIpService.putDedicatedIpPoolScalingAttributes(poolName, scalingMode, region);- }-// Dedicated-IP auto-warmup is an account-level setting owned by SesAccountService.public boolean isAccountDedicatedIpAutoWarmupEnabled(String region) {