mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(cloudformation): provision ElastiCache serverless caches (#5423)
* feat(cloudformation): provision ElastiCache serverless caches Add the AWS::ElastiCache::ServerlessCache provisioner on top of the serverless cache API, with dotted Endpoint.* and ReaderEndpoint.* Fn::GetAtt attributes, in-place modification, rejected immutable changes and replacement on rename. * fix(cloudformation): reject a malformed NoPasswordRequired on ElastiCache users * fix(cloudformation): restore serverless cache settings on a failed update and reset removed ones * refactor(elasticache): inject ElastiCacheService once into the serverless service * fix(cloudformation): replace a serverless cache on its create-only properties Register AWS::ElastiCache::ServerlessCache in create-only-properties.json so a change to ServerlessCacheName, SubnetIds, KmsKeyId or SnapshotArnsToRestore is reported as a replacement instead of being applied in place or ignored. Share one Docker availability probe across the ElastiCache integration tests and validate the tag count in one place in ElastiCacheServerlessService. * fix(cloudformation): reject custom-named serverless cache replacement on create-only properties * fix(cloudformation): simplify serverless create-only update check and share replacement error
This commit is contained in:
1 parent
367ae0b2db
commit
b8e628356c
21 files changed
+1065
-89
No files matched your search
+21
@@ -0,0 +1,21 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/**
|
||||
* Exceptions shared by extracted CloudFormation resource provisioners.
|
||||
*/
|
||||
public final class CfnExceptions {
|
||||
|
||||
private CfnExceptions() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when an update requires replacing a custom-named resource that kept its name.
|
||||
*/
|
||||
public static AwsException customNamedReplacement(String name) {
|
||||
return new AwsException("ValidationError",
|
||||
"CloudFormation cannot update a stack when a custom-named resource requires replacing. "
|
||||
+ "Rename " + name + " and update the stack again.", 400);
|
||||
}
|
||||
}
|
||||
+7
@@ -200,6 +200,13 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String COGNITO_UPDATE_SNAPSHOT_ATTR = "__FlociCognitoUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the settings and tags a serverless cache had before an in-place update changed them,
|
||||
* so a failed stack update can put them back. Written by {@code ElastiCacheServerlessCfnProvisioner}
|
||||
* before its modify call and spent by its {@code rollbackUpdate}.
|
||||
*/
|
||||
public static final String SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR = "__FlociServerlessCacheUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the configuration an Auto Scaling group carried before an in-place update changed it,
|
||||
* so a failed stack update can restore it.
|
||||
|
||||
+1
-3
@@ -62,9 +62,7 @@ public class CognitoUserPoolUserToGroupAttachmentCfnProvisioner implements CfnRe
|
||||
if (ctx.reusesPriorEntity(physicalId)) {
|
||||
JsonNode prior = memberships(attributesBefore).get(physicalId);
|
||||
if (prior != null && !prior.equals(membership)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename "
|
||||
+ physicalId + " and update the stack again.", 400);
|
||||
throw CfnExceptions.customNamedReplacement(physicalId);
|
||||
}
|
||||
} else {
|
||||
String member = cognitoService.adminGetUser(userPoolId, username).getUsername();
|
||||
|
||||
+8
-1
@@ -433,7 +433,14 @@ public class ElastiCacheCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (passwords.isEmpty()) {
|
||||
passwords = ctx.resolveStringList(props, "Passwords");
|
||||
}
|
||||
boolean noPasswordRequired = Boolean.parseBoolean(ctx.resolveOptional(props, "NoPasswordRequired"));
|
||||
String noPasswordFlag = ctx.resolveOptional(props, "NoPasswordRequired");
|
||||
// parseBoolean reads every unrecognised value as false, which would hide a malformed template.
|
||||
if (noPasswordFlag != null && !noPasswordFlag.isBlank()
|
||||
&& !"true".equalsIgnoreCase(noPasswordFlag) && !"false".equalsIgnoreCase(noPasswordFlag)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"NoPasswordRequired must be true or false, got: " + noPasswordFlag, 400);
|
||||
}
|
||||
boolean noPasswordRequired = Boolean.parseBoolean(noPasswordFlag);
|
||||
return UserAuthentication.resolve(type, passwords, noPasswordRequired);
|
||||
}
|
||||
|
||||
|
||||
+309
@@ -0,0 +1,309 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
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.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheServerlessService;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.CacheUsageLimits;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.DataStorage;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache.EcpuPerSecond;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Provisions AWS::ElastiCache::ServerlessCache. Injects only ElastiCacheServerlessService. */
|
||||
@ApplicationScoped
|
||||
public class ElastiCacheServerlessCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String SERVERLESS_CACHE = "AWS::ElastiCache::ServerlessCache";
|
||||
private static final String CREATE_ONLY_ATTR = "__FlociCreateOnly";
|
||||
|
||||
private final ElastiCacheServerlessService serverlessService;
|
||||
|
||||
@Inject
|
||||
public ElastiCacheServerlessCfnProvisioner(ElastiCacheServerlessService serverlessService) {
|
||||
this.serverlessService = serverlessService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(SERVERLESS_CACHE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
|
||||
String name = ctx.resolveOptional(props, "ServerlessCacheName");
|
||||
String engine = ctx.resolveOptional(props, "Engine");
|
||||
if (name == null || name.isBlank() || engine == null || engine.isBlank()) {
|
||||
throw new AwsException("ValidationException", "ServerlessCacheName and Engine are required", 400);
|
||||
}
|
||||
// The service stores the name in lowercase, so the prior physical id only matches in that form.
|
||||
name = name.toLowerCase(Locale.ROOT);
|
||||
ServerlessCache cache;
|
||||
if (ctx.reusesPriorEntity(name)) {
|
||||
cache = modify(r, name, props, ctx);
|
||||
} else {
|
||||
cache = create(name, engine, props, ctx);
|
||||
}
|
||||
|
||||
r.setPhysicalId(cache.getServerlessCacheName());
|
||||
r.getAttributes().put("ARN", cache.getArn());
|
||||
r.getAttributes().put("Status", cache.getStatus());
|
||||
if (cache.getFullEngineVersion() != null) {
|
||||
r.getAttributes().put("FullEngineVersion", cache.getFullEngineVersion());
|
||||
}
|
||||
if (cache.getCreateTime() != null) {
|
||||
r.getAttributes().put("CreateTime", cache.getCreateTime().toString());
|
||||
}
|
||||
putEndpoint(r, "Endpoint", cache.getEndpoint());
|
||||
putEndpoint(r, "ReaderEndpoint", cache.getReaderEndpoint());
|
||||
r.getAttributes().put(CREATE_ONLY_ATTR, createOnlyRecord(props, ctx).toString());
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
private ServerlessCache create(String name, String engine, JsonNode props, ProvisionContext ctx) {
|
||||
List<String> subnetIds = ctx.resolveStringList(props, "SubnetIds");
|
||||
List<String> securityGroupIds = ctx.resolveStringList(props, "SecurityGroupIds");
|
||||
List<String> snapshotArns = ctx.resolveStringList(props, "SnapshotArnsToRestore");
|
||||
return serverlessService.createServerlessCache(new ElastiCacheServerlessService.CreateServerlessCacheRequest(
|
||||
name,
|
||||
engine,
|
||||
ctx.resolveOptional(props, "MajorEngineVersion"),
|
||||
ctx.resolveOptional(props, "Description"),
|
||||
subnetIds.isEmpty() ? null : subnetIds,
|
||||
securityGroupIds.isEmpty() ? null : securityGroupIds,
|
||||
ctx.resolveOptional(props, "UserGroupId"),
|
||||
ctx.resolveOptional(props, "KmsKeyId"),
|
||||
usageLimits(props, ctx),
|
||||
optionalInt(ctx.resolveOptional(props, "SnapshotRetentionLimit")),
|
||||
ctx.resolveOptional(props, "DailySnapshotTime"),
|
||||
snapshotArns.isEmpty() ? null : snapshotArns,
|
||||
ctx.resolveTags(props, "Tags")));
|
||||
}
|
||||
|
||||
private ServerlessCache modify(StackResource r, String name, JsonNode props, ProvisionContext ctx) {
|
||||
ServerlessCache existing = serverlessService.getServerlessCache(name);
|
||||
if (createOnlyPropertiesChanged(r, props, ctx)) {
|
||||
throw CfnExceptions.customNamedReplacement(name);
|
||||
}
|
||||
snapshotBeforeUpdate(r, existing);
|
||||
List<String> securityGroupIds = ctx.resolveStringList(props, "SecurityGroupIds");
|
||||
boolean hadSecurityGroups = existing.getSecurityGroupIds() != null && !existing.getSecurityGroupIds().isEmpty();
|
||||
// A null list means leave unchanged, so clearing the groups needs an explicit empty one.
|
||||
List<String> desiredSecurityGroups = !securityGroupIds.isEmpty() ? securityGroupIds
|
||||
: hadSecurityGroups ? List.of() : null;
|
||||
String description = ctx.resolveOptional(props, "Description");
|
||||
String userGroupId = ctx.resolveOptional(props, "UserGroupId");
|
||||
boolean removeUserGroup = userGroupId == null && existing.getUserGroupId() != null;
|
||||
try {
|
||||
serverlessService.modifyServerlessCache(new ElastiCacheServerlessService.ModifyServerlessCacheRequest(
|
||||
name,
|
||||
// A removed Description goes back to the empty one a new cache gets.
|
||||
description == null ? "" : description,
|
||||
ctx.resolveOptional(props, "Engine"),
|
||||
ctx.resolveOptional(props, "MajorEngineVersion"),
|
||||
desiredSecurityGroups,
|
||||
userGroupId,
|
||||
removeUserGroup ? Boolean.TRUE : null,
|
||||
usageLimits(props, ctx),
|
||||
optionalInt(ctx.resolveOptional(props, "SnapshotRetentionLimit")),
|
||||
ctx.resolveOptional(props, "DailySnapshotTime"),
|
||||
true));
|
||||
return serverlessService.replaceTags(name, ctx.resolveTags(props, "Tags"));
|
||||
} catch (RuntimeException failure) {
|
||||
// The stack skips rollbackUpdate for the resource whose own update failed, and the tag step
|
||||
// runs after the settings were saved, so the settings are put back here.
|
||||
try {
|
||||
restoreSnapshot(r);
|
||||
} catch (RuntimeException restoreFailure) {
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
/** Keeps what an in-place update changes, so a failed stack update can put it back. */
|
||||
private static void snapshotBeforeUpdate(StackResource r, ServerlessCache existing) {
|
||||
ObjectNode snapshot = MAPPER.createObjectNode();
|
||||
snapshot.put("name", existing.getServerlessCacheName());
|
||||
snapshot.put("description", existing.getDescription() == null ? "" : existing.getDescription());
|
||||
snapshot.set("securityGroupIds", MAPPER.valueToTree(
|
||||
existing.getSecurityGroupIds() == null ? List.of() : existing.getSecurityGroupIds()));
|
||||
if (existing.getUserGroupId() != null) {
|
||||
snapshot.put("userGroupId", existing.getUserGroupId());
|
||||
}
|
||||
if (existing.getCacheUsageLimits() != null) {
|
||||
snapshot.set("cacheUsageLimits", MAPPER.valueToTree(existing.getCacheUsageLimits()));
|
||||
}
|
||||
snapshot.put("snapshotRetentionLimit",
|
||||
existing.getSnapshotRetentionLimit() == null ? 0 : existing.getSnapshotRetentionLimit());
|
||||
if (existing.getDailySnapshotTime() != null) {
|
||||
snapshot.put("dailySnapshotTime", existing.getDailySnapshotTime());
|
||||
}
|
||||
ObjectNode tags = snapshot.putObject("tags");
|
||||
if (existing.getTags() != null) {
|
||||
existing.getTags().forEach(tags::put);
|
||||
}
|
||||
r.getAttributes().put(CfnRollback.SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
}
|
||||
|
||||
private void restoreSnapshot(StackResource r) {
|
||||
String raw = r.getAttributes().get(CfnRollback.SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR);
|
||||
if (raw == null) {
|
||||
return;
|
||||
}
|
||||
JsonNode snapshot;
|
||||
try {
|
||||
snapshot = MAPPER.readTree(raw);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Could not read the serverless cache update snapshot for "
|
||||
+ r.getLogicalId(), e);
|
||||
}
|
||||
String name = snapshot.path("name").asText();
|
||||
List<String> securityGroupIds = new ArrayList<>();
|
||||
snapshot.path("securityGroupIds").forEach(id -> securityGroupIds.add(id.asText()));
|
||||
String userGroupId = snapshot.hasNonNull("userGroupId") ? snapshot.get("userGroupId").asText() : null;
|
||||
CacheUsageLimits limits = snapshot.hasNonNull("cacheUsageLimits")
|
||||
? MAPPER.convertValue(snapshot.get("cacheUsageLimits"), CacheUsageLimits.class) : null;
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
snapshot.path("tags").fields().forEachRemaining(e -> tags.put(e.getKey(), e.getValue().asText()));
|
||||
serverlessService.modifyServerlessCache(new ElastiCacheServerlessService.ModifyServerlessCacheRequest(
|
||||
name,
|
||||
snapshot.path("description").asText(""),
|
||||
null,
|
||||
null,
|
||||
securityGroupIds,
|
||||
userGroupId,
|
||||
userGroupId == null ? Boolean.TRUE : null,
|
||||
limits,
|
||||
snapshot.path("snapshotRetentionLimit").asInt(0),
|
||||
snapshot.hasNonNull("dailySnapshotTime") ? snapshot.get("dailySnapshotTime").asText() : null,
|
||||
true));
|
||||
serverlessService.replaceTags(name, tags);
|
||||
r.getAttributes().remove(CfnRollback.SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR);
|
||||
}
|
||||
|
||||
private CacheUsageLimits usageLimits(JsonNode props, ProvisionContext ctx) {
|
||||
if (!props.hasNonNull("CacheUsageLimits")) {
|
||||
return null;
|
||||
}
|
||||
JsonNode limits = ctx.engine().resolveNode(props.get("CacheUsageLimits"));
|
||||
JsonNode storage = limits.path("DataStorage");
|
||||
JsonNode ecpu = limits.path("ECPUPerSecond");
|
||||
DataStorage dataStorage = storage.isObject()
|
||||
? new DataStorage(intOrNull(storage, "Minimum"), intOrNull(storage, "Maximum"),
|
||||
storage.hasNonNull("Unit") ? storage.get("Unit").asText() : null)
|
||||
: null;
|
||||
EcpuPerSecond ecpuPerSecond = ecpu.isObject()
|
||||
? new EcpuPerSecond(intOrNull(ecpu, "Minimum"), intOrNull(ecpu, "Maximum"))
|
||||
: null;
|
||||
return new CacheUsageLimits(dataStorage, ecpuPerSecond);
|
||||
}
|
||||
|
||||
private static Integer intOrNull(JsonNode node, String field) {
|
||||
return node.hasNonNull(field) ? Integer.valueOf(node.get(field).asInt()) : null;
|
||||
}
|
||||
|
||||
private static Integer optionalInt(String raw) {
|
||||
return raw == null || raw.isBlank() ? null : Integer.valueOf(raw);
|
||||
}
|
||||
|
||||
private void putEndpoint(StackResource r, String prefix, Endpoint endpoint) {
|
||||
if (endpoint == null) {
|
||||
return;
|
||||
}
|
||||
r.getAttributes().put(prefix + ".Address", endpoint.address());
|
||||
r.getAttributes().put(prefix + ".Port", String.valueOf(endpoint.port()));
|
||||
}
|
||||
|
||||
private static ObjectNode createOnlyRecord(JsonNode props, ProvisionContext ctx) {
|
||||
ObjectNode record = MAPPER.createObjectNode();
|
||||
if (props == null) {
|
||||
return record;
|
||||
}
|
||||
String kms = ctx.resolveOptional(props, "KmsKeyId");
|
||||
if (kms != null && !kms.isBlank()) {
|
||||
record.put("KmsKeyId", kms.trim());
|
||||
}
|
||||
String connectionType = ctx.resolveOptional(props, "ConnectionType");
|
||||
if (connectionType != null && !connectionType.isBlank()) {
|
||||
record.put("ConnectionType", connectionType.trim());
|
||||
}
|
||||
String networkType = ctx.resolveOptional(props, "NetworkType");
|
||||
if (networkType != null && !networkType.isBlank()) {
|
||||
record.put("NetworkType", networkType.trim());
|
||||
}
|
||||
List<String> subnets = ctx.resolveStringList(props, "SubnetIds");
|
||||
if (!subnets.isEmpty()) {
|
||||
ArrayNode arr = record.putArray("SubnetIds");
|
||||
subnets.stream().sorted().forEach(arr::add);
|
||||
}
|
||||
List<String> snapshots = ctx.resolveStringList(props, "SnapshotArnsToRestore");
|
||||
if (!snapshots.isEmpty()) {
|
||||
ArrayNode arr = record.putArray("SnapshotArnsToRestore");
|
||||
snapshots.forEach(arr::add);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
private static boolean createOnlyPropertiesChanged(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String recorded = r.getAttributes().getOrDefault(CREATE_ONLY_ATTR, "{}");
|
||||
return !recorded.equals(createOnlyRecord(props, ctx).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(CfnRollback.SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR);
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
if (ReplacementCleanup.rollback(resource, this::delete)) {
|
||||
return true;
|
||||
}
|
||||
if (resource.getAttributes().get(CfnRollback.SERVERLESS_CACHE_UPDATE_SNAPSHOT_ATTR) == null) {
|
||||
return false;
|
||||
}
|
||||
restoreSnapshot(resource);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
if (physicalId == null) {
|
||||
return;
|
||||
}
|
||||
CfnDeletes.safeDelete("ElastiCache serverless cache", physicalId,
|
||||
() -> serverlessService.deleteServerlessCache(physicalId), "ServerlessCacheNotFoundFault");
|
||||
}
|
||||
}
|
||||
+47
-19
@@ -32,18 +32,16 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
private final RegionResolver resolver;
|
||||
private final ElastiCacheService runtime;
|
||||
private final ElastiCacheMemcachedService memcached;
|
||||
private final ElastiCacheService groups;
|
||||
private final ConcurrentHashMap<String, Object> locks = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public ElastiCacheServerlessService(StorageFactory factory, RegionResolver resolver, ElastiCacheService runtime,
|
||||
ElastiCacheMemcachedService memcached, ElastiCacheService groups) {
|
||||
ElastiCacheMemcachedService memcached) {
|
||||
this.caches = factory.create("elasticache", "elasticache-serverless-caches.json",
|
||||
new TypeReference<Map<String, ServerlessCache>>() {});
|
||||
this.resolver = resolver;
|
||||
this.runtime = runtime;
|
||||
this.memcached = memcached;
|
||||
this.groups = groups;
|
||||
}
|
||||
|
||||
public record CreateServerlessCacheRequest(String serverlessCacheName, String engine, String majorEngineVersion,
|
||||
@@ -55,7 +53,19 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
public record ModifyServerlessCacheRequest(String serverlessCacheName, String description, String engine,
|
||||
String majorEngineVersion, List<String> securityGroupIds,
|
||||
String userGroupId, Boolean removeUserGroup, CacheUsageLimits cacheUsageLimits,
|
||||
Integer snapshotRetentionLimit, String dailySnapshotTime) {}
|
||||
Integer snapshotRetentionLimit, String dailySnapshotTime,
|
||||
boolean resetUnset) {
|
||||
|
||||
/** The API form: an omitted field leaves the stored value unchanged. */
|
||||
public ModifyServerlessCacheRequest(String serverlessCacheName, String description, String engine,
|
||||
String majorEngineVersion, List<String> securityGroupIds,
|
||||
String userGroupId, Boolean removeUserGroup,
|
||||
CacheUsageLimits cacheUsageLimits, Integer snapshotRetentionLimit,
|
||||
String dailySnapshotTime) {
|
||||
this(serverlessCacheName, description, engine, majorEngineVersion, securityGroupIds, userGroupId,
|
||||
removeUserGroup, cacheUsageLimits, snapshotRetentionLimit, dailySnapshotTime, false);
|
||||
}
|
||||
}
|
||||
|
||||
public ServerlessCache createServerlessCache(CreateServerlessCacheRequest request) {
|
||||
String name = normalizedName(request.serverlessCacheName());
|
||||
@@ -69,9 +79,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
if (request.snapshotArnsToRestore() != null && !request.snapshotArnsToRestore().isEmpty()) {
|
||||
throw invalid("Snapshot restore is not supported locally.");
|
||||
}
|
||||
if (request.tags() != null && request.tags().size() > 50) {
|
||||
throw new AwsException("TagQuotaPerResourceExceeded", "At most 50 tags are supported.", 400);
|
||||
}
|
||||
validateTagCount(request.tags());
|
||||
String rawGroup = request.userGroupId();
|
||||
String group = normalizedUserGroupId(rawGroup);
|
||||
String region = resolver.getRegion();
|
||||
@@ -85,7 +93,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
CacheCluster backing = null;
|
||||
try {
|
||||
if (group != null) {
|
||||
groups.attachServerlessCache(group, key(name), engine);
|
||||
runtime.attachServerlessCache(group, key(name), engine);
|
||||
attached = true;
|
||||
}
|
||||
String runtimeId = "serverless-" + UUID.randomUUID();
|
||||
@@ -131,7 +139,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
}
|
||||
if (attached) {
|
||||
try {
|
||||
groups.detachServerlessCache(group, key(name));
|
||||
runtime.detachServerlessCache(group, key(name));
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
exception.addSuppressed(cleanupFailure);
|
||||
}
|
||||
@@ -178,7 +186,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
validateGroup(group, previous.getEngine(), previous.getRegion());
|
||||
boolean changedGroup = !Objects.equals(group, previous.getUserGroupId());
|
||||
if (changedGroup && group != null) {
|
||||
groups.attachServerlessCache(group, key(name), previous.getEngine());
|
||||
runtime.attachServerlessCache(group, key(name), previous.getEngine());
|
||||
}
|
||||
try {
|
||||
if (changedGroup && !"memcached".equals(previous.getEngine())) {
|
||||
@@ -191,13 +199,15 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
if (request.securityGroupIds() != null) {
|
||||
updated.setSecurityGroupIds(request.securityGroupIds());
|
||||
}
|
||||
if (request.cacheUsageLimits() != null) {
|
||||
// CloudFormation sends the whole desired state, so an omitted field goes back to its create default.
|
||||
if (request.cacheUsageLimits() != null || request.resetUnset()) {
|
||||
updated.setCacheUsageLimits(request.cacheUsageLimits());
|
||||
}
|
||||
if (request.snapshotRetentionLimit() != null) {
|
||||
updated.setSnapshotRetentionLimit(request.snapshotRetentionLimit());
|
||||
if (request.snapshotRetentionLimit() != null || request.resetUnset()) {
|
||||
updated.setSnapshotRetentionLimit(
|
||||
request.snapshotRetentionLimit() == null ? 0 : request.snapshotRetentionLimit());
|
||||
}
|
||||
if (request.dailySnapshotTime() != null) {
|
||||
if (request.dailySnapshotTime() != null || request.resetUnset()) {
|
||||
updated.setDailySnapshotTime(request.dailySnapshotTime());
|
||||
}
|
||||
caches.put(key(name), updated);
|
||||
@@ -206,7 +216,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
try {
|
||||
runtime.updateServerlessUserGroup(previous.getBackingCacheClusterId(), previous.getAccountId(), previous.getUserGroupId());
|
||||
if (group != null) {
|
||||
groups.detachServerlessCache(group, key(name));
|
||||
runtime.detachServerlessCache(group, key(name));
|
||||
}
|
||||
} catch (RuntimeException rollbackFailure) {
|
||||
exception.addSuppressed(rollbackFailure);
|
||||
@@ -215,12 +225,30 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
throw exception;
|
||||
}
|
||||
if (changedGroup && previous.getUserGroupId() != null) {
|
||||
groups.detachServerlessCache(previous.getUserGroupId(), key(name));
|
||||
runtime.detachServerlessCache(previous.getUserGroupId(), key(name));
|
||||
}
|
||||
return current(updated);
|
||||
}
|
||||
}
|
||||
|
||||
/** Drives the tags to exactly the given set, the way CloudFormation reconciles a resource on update. */
|
||||
public ServerlessCache replaceTags(String name, Map<String, String> tags) {
|
||||
validateTagCount(tags);
|
||||
String normalized = normalizedName(name);
|
||||
synchronized (lockFor(normalized)) {
|
||||
ServerlessCache updated = copy(requireCache(normalized));
|
||||
updated.setTags(tags);
|
||||
caches.put(key(normalized), updated);
|
||||
return current(updated);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateTagCount(Map<String, String> tags) {
|
||||
if (tags != null && tags.size() > 50) {
|
||||
throw new AwsException("TagQuotaPerResourceExceeded", "At most 50 tags are supported.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public ServerlessCache deleteServerlessCache(String name) {
|
||||
return deleteServerlessCache(name, null);
|
||||
}
|
||||
@@ -242,7 +270,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
cache.getBackingCacheClusterId(), normalized);
|
||||
}
|
||||
if (cache.getUserGroupId() != null) {
|
||||
groups.detachServerlessCache(cache.getUserGroupId(), key(normalized));
|
||||
runtime.detachServerlessCache(cache.getUserGroupId(), key(normalized));
|
||||
}
|
||||
caches.delete(key(normalized));
|
||||
ServerlessCache deleted = copy(cache);
|
||||
@@ -300,7 +328,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
if ("memcached".equals(engine)) {
|
||||
throw new AwsException("InvalidParameterCombination", "Memcached does not support user groups.", 400);
|
||||
}
|
||||
groups.validateServerlessAssociation(group, engine);
|
||||
runtime.validateServerlessAssociation(group, engine);
|
||||
}
|
||||
|
||||
private static String normalizedName(String name) {
|
||||
@@ -375,7 +403,7 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
cache.getBackingCacheClusterId(), cache.getServerlessCacheName());
|
||||
}
|
||||
if (cache.getUserGroupId() != null) {
|
||||
groups.detachServerlessCache(cache.getUserGroupId(),
|
||||
runtime.detachServerlessCache(cache.getUserGroupId(),
|
||||
cache.getRegion() + "/" + cache.getServerlessCacheName());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user