refactor(ses): move the tag dispatch helpers into their domain services (#3090)

* refactor(ses): move the tag dispatch helpers into their domain services

* docs(ses): move the stranded tenant-ARN comment onto ResourceRef
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-09-05 15:41:46 -04:00
1 parent 3c6c005ff1
commit 15b401032d
7 files changed
+175 -111

No files matched your search

@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.ses.model.CloudWatchDimensionConfigur
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
import io.github.hectorvent.floci.services.ses.model.EventDestination;
import io.github.hectorvent.floci.services.ses.model.SuppressionOptions;
import io.github.hectorvent.floci.services.ses.model.Tag;
import io.github.hectorvent.floci.services.ses.model.VdmOptions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -17,6 +18,7 @@ import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -31,9 +33,9 @@ import java.util.regex.Pattern;
* <p>The boundary follows the cross-domain seams: option validation that reads OTHER domains stays
* in the facade (tracking options check a verified domain identity, delivery options check a
* dedicated IP pool), which validates first (or resolves existence through {@link #get}) and then
* mutates through {@link #save}. The facade also keeps the send-path reads (pause check, event
* publishing, effective suppression reasons), the ARN-dispatched tagging, and the tenant
* delete-guard around {@link #remove}.
* mutates through {@link #save}. The ARN-dispatched configuration-set tagging lives here; the
* facade keeps the send-path reads (pause check, event publishing, effective suppression reasons)
* and the tenant delete-guard around {@link #remove}.
*/
@ApplicationScoped
public class SesConfigurationSetService {
@@ -102,13 +104,42 @@ public class SesConfigurationSetService {
LOG.infov("Deleted SES configuration set: {0} in region {1}", name, region);
}
/** The ARN-dispatched tag operations, sharing the store behind {@code CreateConfigurationSet.Tags}. */
public List<Tag> listTags(String name, String region) {
return new ArrayList<>(requireForTags(name, region).getTags());
}
public void tag(String name, String region, List<Tag> newTags) {
ConfigurationSet cs = requireForTags(name, region);
cs.setTags(SesTags.merge(cs.getTags(), newTags));
configSetStore.put(configSetKey(region, name), cs);
LOG.infov("Tagged SES configuration set: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
public void untag(String name, String region, List<String> tagKeys) {
ConfigurationSet cs = requireForTags(name, region);
Set<String> toRemove = new HashSet<>(tagKeys);
// Copy-on-write: the stored list may be immutable, and unlocked readers iterate it.
List<Tag> remaining = new ArrayList<>(cs.getTags());
remaining.removeIf(t -> toRemove.contains(t.key()));
cs.setTags(remaining);
configSetStore.put(configSetKey(region, name), cs);
LOG.infov("Untagged SES configuration set: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
private ConfigurationSet requireForTags(String name, String region) {
return configSetStore.get(configSetKey(region, name))
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
}
/** Reads without throwing on absence (the name is still validated, as the key derivation always
* has), for the facade's send-path and tagging lookups. */
* has), for the facade's send-path lookups. */
public Optional<ConfigurationSet> find(String name, String region) {
return configSetStore.get(configSetKey(region, name));
}
/** Persists a configuration set the facade mutated (cross-domain option setters, tagging). */
/** Persists a configuration set the facade mutated (the cross-domain option setters). */
public void save(ConfigurationSet configSet, String region) {
configSetStore.put(configSetKey(region, configSet.getName()), configSet);
}
@@ -1483,13 +1483,13 @@ public class SesService {
ResourceRef ref = parseSesArn(arn);
requireCallerAccount(ref);
List<Tag> tags = switch (ref.type()) {
case "configuration-set" -> listConfigurationSetTags(ref.name(), region);
case "template" -> listEmailTemplateTags(ref.name(), region);
case "configuration-set" -> configSetService.listTags(ref.name(), region);
case "template" -> templateService.listTags(ref.name(), region);
case "identity" -> identityService.listTags(ref.name(), region);
case "contact-list" -> contactService.listTags(ref.name(), region);
case "custom-verification-email-template" -> cvetService.listTags(ref.name(), region);
case "dedicated-ip-pool" -> dedicatedIpService.listTags(ref.name(), region);
case "tenant" -> listTenantTags(ref.name(), region);
case "tenant" -> tenantService.listTags(ref.name(), region);
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
};
@@ -1514,13 +1514,13 @@ public class SesService {
List<Tag> tags = newTags == null ? List.of() : newTags;
SesTags.validate(tags);
switch (ref.type()) {
case "configuration-set" -> tagConfigurationSet(ref.name(), region, tags);
case "template" -> tagEmailTemplate(ref.name(), region, tags);
case "configuration-set" -> configSetService.tag(ref.name(), region, tags);
case "template" -> templateService.tag(ref.name(), region, tags);
case "identity" -> identityService.tag(ref.name(), region, tags);
case "contact-list" -> contactService.tag(ref.name(), region, tags);
case "custom-verification-email-template" -> cvetService.tag(ref.name(), region, tags);
case "dedicated-ip-pool" -> dedicatedIpService.tag(ref.name(), region, tags);
case "tenant" -> tagTenant(ref.name(), region, tags);
case "tenant" -> tenantService.tag(ref.name(), region, tags);
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
}
@@ -1541,109 +1541,24 @@ public class SesService {
throw new AwsException("BadRequestException", "Failed to untag resource", 400);
}
switch (ref.type()) {
case "configuration-set" -> untagConfigurationSet(ref.name(), region, tagKeys);
case "template" -> untagEmailTemplate(ref.name(), region, tagKeys);
case "configuration-set" -> configSetService.untag(ref.name(), region, tagKeys);
case "template" -> templateService.untag(ref.name(), region, tagKeys);
case "identity" -> identityService.untag(ref.name(), region, tagKeys);
case "contact-list" -> contactService.untag(ref.name(), region, tagKeys);
case "custom-verification-email-template" -> cvetService.untag(ref.name(), region, tagKeys);
case "dedicated-ip-pool" -> dedicatedIpService.untag(ref.name(), region, tagKeys);
case "tenant" -> untagTenant(ref.name(), region, tagKeys);
case "tenant" -> tenantService.untag(ref.name(), region, tagKeys);
default -> throw new AwsException("NotFoundException",
"Resource " + arn + " was not found.", 404);
}
}
// A tenant tag ARN carries two path segments (tenant/<name>/<tenantId>), so parseSesArn's
// first-slash split leaves "<name>/<tenantId>" as the resource name; the tenant domain owns
// that remainder's decomposition and the id-only resolution (see SesTenantService.TenantTagArn).
private List<Tag> listTenantTags(String resourceName, String region) {
Tenant tenant = tenantService.tenantForTagArn(resourceName, region);
// AWS returns a tenant's tags ordered by key (probe-confirmed).
return (tenant.tags() == null ? List.<Tag>of() : tenant.tags()).stream()
.sorted(Comparator.comparing(Tag::key, Comparator.nullsLast(Comparator.naturalOrder())))
.toList();
}
private void tagTenant(String resourceName, String region, List<Tag> newTags) {
tenantService.mutateTags(resourceName, region, tags -> SesTags.merge(tags, newTags));
LOG.infov("Tagged SES tenant <{0}> (region {1}, +{2} tags)",
resourceName, region, newTags.size());
}
private void untagTenant(String resourceName, String region, List<String> tagKeys) {
Set<String> toRemove = new HashSet<>(tagKeys);
tenantService.mutateTags(resourceName, region, tags -> {
List<Tag> remaining = new ArrayList<>(tags);
remaining.removeIf(t -> toRemove.contains(t.key()));
return remaining;
});
LOG.infov("Untagged SES tenant <{0}> (region {1}, -{2} keys)",
resourceName, region, tagKeys.size());
}
private List<Tag> listConfigurationSetTags(String name, String region) {
ConfigurationSet cs = configSetService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
return new ArrayList<>(cs.getTags());
}
private void tagConfigurationSet(String name, String region, List<Tag> newTags) {
ConfigurationSet cs = configSetService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
cs.setTags(SesTags.merge(cs.getTags(), newTags));
configSetService.save(cs, region);
LOG.infov("Tagged SES configuration set: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
private void untagConfigurationSet(String name, String region, List<String> tagKeys) {
ConfigurationSet cs = configSetService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No ConfigurationSet present with name: " + name, 404));
Set<String> toRemove = new HashSet<>(tagKeys);
// Copy-on-write: the stored list may be immutable, and unlocked readers iterate it.
List<Tag> remaining = new ArrayList<>(cs.getTags());
remaining.removeIf(t -> toRemove.contains(t.key()));
cs.setTags(remaining);
configSetService.save(cs, region);
LOG.infov("Untagged SES configuration set: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
private List<Tag> listEmailTemplateTags(String name, String region) {
EmailTemplate template = templateService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
return new ArrayList<>(template.getTags());
}
private void tagEmailTemplate(String name, String region, List<Tag> newTags) {
EmailTemplate template = templateService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
template.setTags(SesTags.merge(template.getTags(), newTags));
templateService.save(template, region);
LOG.infov("Tagged SES template: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
public void setIdentityTags(String identityValue, String region, List<Tag> tags) {
identityService.setTags(identityValue, region, tags);
}
private void untagEmailTemplate(String name, String region, List<String> tagKeys) {
EmailTemplate template = templateService.find(name, region)
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
Set<String> toRemove = new HashSet<>(tagKeys);
// Copy-on-write: the stored list may be immutable, and unlocked readers iterate it.
List<Tag> remaining = new ArrayList<>(template.getTags());
remaining.removeIf(t -> toRemove.contains(t.key()));
template.setTags(remaining);
templateService.save(template, region);
LOG.infov("Untagged SES template: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
// name is everything after the type's first slash and may itself contain one: a tenant ARN's
// <name>/<tenantId> remainder passes through whole, decomposed by the tenant domain.
private record ResourceRef(String account, String region, String type, String name) {}
/**
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
import io.github.hectorvent.floci.services.ses.model.Tag;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -18,18 +19,20 @@ import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Email templates (the {@code templateStore}), extracted from {@link SesService} as part of the
* store-based domain split. Reached through the {@code SesService} facade, which delegates the CRUD
* here; the facade's templated-send path reads templates back through {@link #getTemplate}, and its
* ARN-dispatched tagging reads/writes through {@link #find} / {@link #save}.
* here, along with the ARN-dispatched template tagging; the facade's templated-send path reads
* templates back through {@link #getTemplate}.
*
* <p>Template rendering is domain logic too: {@code TestRenderTemplate} (data parsing, variable
* substitution, and the MIME assembly) lives here, and the facade's templated-send paths call the
@@ -124,16 +127,16 @@ public class SesTemplateService {
}
/**
* Reads a template without throwing, so the facade's ARN-dispatched tagging can look one up by
* name and reuse this service's key derivation.
* Reads a template without throwing, for the facade's orchestration lookups (the tenant
* association check and the send-path reads).
*/
public Optional<EmailTemplate> find(String templateName, String region) {
return templateStore.get(templateKey(region, templateName));
}
/**
* Persists a template under its canonical key, so the facade's tagging can write the merged tags
* back without owning the key derivation.
* Persists a template under its canonical key, so facade orchestration that mutates a found
* template can write it back without owning the key derivation.
*/
public void save(EmailTemplate template, String region) {
templateStore.put(templateKey(region, template.getTemplateName()), template);
@@ -169,6 +172,35 @@ public class SesTemplateService {
return "template::" + region + "::" + templateName;
}
/** The ARN-dispatched tag operations, sharing the store behind {@code CreateEmailTemplate.Tags}. */
public List<Tag> listTags(String name, String region) {
return new ArrayList<>(requireForTags(name, region).getTags());
}
public void tag(String name, String region, List<Tag> newTags) {
EmailTemplate template = requireForTags(name, region);
template.setTags(SesTags.merge(template.getTags(), newTags));
templateStore.put(templateKey(region, name), template);
LOG.infov("Tagged SES template: {0} (region {1}, +{2} tags)", name, region, newTags.size());
}
public void untag(String name, String region, List<String> tagKeys) {
EmailTemplate template = requireForTags(name, region);
Set<String> toRemove = new HashSet<>(tagKeys);
// Copy-on-write: the stored list may be immutable, and unlocked readers iterate it.
List<Tag> remaining = new ArrayList<>(template.getTags());
remaining.removeIf(t -> toRemove.contains(t.key()));
template.setTags(remaining);
templateStore.put(templateKey(region, name), template);
LOG.infov("Untagged SES template: {0} (region {1}, -{2} keys)", name, region, tagKeys.size());
}
private EmailTemplate requireForTags(String name, String region) {
return templateStore.get(templateKey(region, name))
.orElseThrow(() -> new AwsException("NotFoundException",
"No Template present with name: " + name, 404));
}
// ──────────────────────── Rendering (TestRenderTemplate + send-path substitution) ────────────────────────
public String renderTestTemplate(String templateName, String templateDataRaw, String region) {
@@ -15,6 +15,7 @@ import org.jboss.logging.Logger;
import java.security.SecureRandom;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.HashSet;
@@ -201,20 +202,46 @@ public class SesTenantService {
}
}
/** Resolves a tenant from a tag ARN's resource remainder, for the facade's tag listing. */
public Tenant tenantForTagArn(String resourceRemainder, String region) {
/** The ARN-dispatched tag operations; {@code resourceRemainder} is the ARN's {@code <name>/<tenantId>} part. */
public List<Tag> listTags(String resourceRemainder, String region) {
Tenant tenant = tenantForTagArn(resourceRemainder, region);
// AWS returns a tenant's tags ordered by key (probe-confirmed).
return (tenant.tags() == null ? List.<Tag>of() : tenant.tags()).stream()
.sorted(Comparator.comparing(Tag::key, Comparator.nullsLast(Comparator.naturalOrder())))
.toList();
}
public void tag(String resourceRemainder, String region, List<Tag> newTags) {
mutateTags(resourceRemainder, region, tags -> SesTags.merge(tags, newTags));
LOG.infov("Tagged SES tenant <{0}> (region {1}, +{2} tags)",
resourceRemainder, region, newTags.size());
}
public void untag(String resourceRemainder, String region, List<String> tagKeys) {
Set<String> toRemove = new HashSet<>(tagKeys);
mutateTags(resourceRemainder, region, tags -> {
List<Tag> remaining = new ArrayList<>(tags);
remaining.removeIf(t -> toRemove.contains(t.key()));
return remaining;
});
LOG.infov("Untagged SES tenant <{0}> (region {1}, -{2} keys)",
resourceRemainder, region, tagKeys.size());
}
/** Resolves a tenant from a tag ARN's resource remainder; package-private for the unit tests. */
Tenant tenantForTagArn(String resourceRemainder, String region) {
TenantTagArn arn = TenantTagArn.parse(resourceRemainder);
return findByTenantId(arn.tenantId(), region)
.orElseThrow(() -> tagArnTenantNotFound(arn));
}
/**
* Applies a tag mutation for the facade's ARN-dispatched tagging. The tenant is re-resolved by
* Applies a tag mutation for the ARN-dispatched tagging above. The tenant is re-resolved by
* TenantId and mutated INSIDE the shared lock, so concurrent tag calls can't lose each other's
* merges, a concurrent suppression-attribute update isn't overwritten by a stale copy, and a
* delete/recreate between the caller's lookup and this write can't resurrect the old record.
*/
public void mutateTags(String resourceRemainder, String region, UnaryOperator<List<Tag>> mutation) {
void mutateTags(String resourceRemainder, String region, UnaryOperator<List<Tag>> mutation) {
TenantTagArn arn = TenantTagArn.parse(resourceRemainder);
synchronized (tenantMutationLock) {
Tenant current = findByTenantId(arn.tenantId(), region)