mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(elasticache): preserve and mutate resource tags (#6327)
* fix(elasticache): preserve and mutate resource tags * fix(elasticache): complete resource tag mutations * docs(elasticache): sync tag actions * docs(services): update ElastiCache action count * fix(elasticache): use AWS InvalidARN tag error code * fix(elasticache): align resource tag mutations with lifecycle and AWS errors
This commit is contained in:
1 parent
a815434e21
commit
d4f454a1cf
14 files changed
+684
-14
No files matched your search
+102
-2
@@ -93,6 +93,8 @@ public class ElastiCacheQueryHandler {
|
||||
case "ModifyCacheParameterGroup" -> handleModifyCacheParameterGroup(params);
|
||||
case "DescribeCacheParameters" -> handleDescribeCacheParameters(params);
|
||||
case "DeleteCacheParameterGroup" -> handleDeleteCacheParameterGroup(params);
|
||||
case "AddTagsToResource" -> handleAddTagsToResource(params, region);
|
||||
case "RemoveTagsFromResource" -> handleRemoveTagsFromResource(params, region);
|
||||
case "ListTagsForResource" -> handleListTagsForResource(params);
|
||||
default -> AwsQueryResponse.error("UnsupportedOperation",
|
||||
"Operation " + action + " is not supported.", AwsNamespaces.EC, 400);
|
||||
@@ -276,7 +278,8 @@ public class ElastiCacheQueryHandler {
|
||||
UserAuthentication auth = resolveUserAuthentication(params);
|
||||
AuthMode authMode = auth != null ? auth.mode() : AuthMode.NO_AUTH;
|
||||
List<String> passwords = auth != null ? auth.passwords() : List.of();
|
||||
ElastiCacheUser user = service.createUser(userId, userName, authMode, passwords, accessString, engine);
|
||||
ElastiCacheUser user = service.createUser(userId, userName, authMode, passwords, accessString, engine,
|
||||
parseTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateUser", AwsNamespaces.EC, userXml(user))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
|
||||
@@ -342,7 +345,8 @@ public class ElastiCacheQueryHandler {
|
||||
private Response handleCreateUserGroup(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
ElastiCacheUserGroup userGroup = service.createUserGroup(params.getFirst("UserGroupId"),
|
||||
params.getFirst("Engine"), extractMemberList(params, "UserIds.member."), region);
|
||||
params.getFirst("Engine"), extractMemberList(params, "UserIds.member."), region,
|
||||
parseTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateUserGroup", AwsNamespaces.EC,
|
||||
userGroupXml(userGroup))).build();
|
||||
} catch (AwsException e) {
|
||||
@@ -817,6 +821,20 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
arn[6] + " is not present", 404))
|
||||
.getTags();
|
||||
}
|
||||
if ("user".equals(arn[5])) {
|
||||
ElastiCacheUser user = service.getUser(arn[6]);
|
||||
if (user.getArn() != null && !user.getArn().equalsIgnoreCase(resourceName)) {
|
||||
throw new AwsException("UserNotFound", "User " + arn[6] + " not found.", 404);
|
||||
}
|
||||
tags = user.getTags();
|
||||
}
|
||||
if ("usergroup".equals(arn[5])) {
|
||||
ElastiCacheUserGroup userGroup = service.getUserGroup(arn[6]);
|
||||
if (userGroup.getArn() != null && !userGroup.getArn().equalsIgnoreCase(resourceName)) {
|
||||
throw new AwsException("UserGroupNotFound", "User group " + arn[6] + " not found.", 404);
|
||||
}
|
||||
tags = userGroup.getTags();
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("TagList");
|
||||
tags.forEach((key, value) -> xml.start("Tag")
|
||||
.elem("Key", key)
|
||||
@@ -829,6 +847,65 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleAddTagsToResource(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
String resourceArn = requiredResourceArn(params);
|
||||
Map<String, String> tags = parseMutationTags(params);
|
||||
Map<String, String> updated = isServerlessResource(resourceArn)
|
||||
? serverlessHandler.addTagsToResource(resourceArn, tags, region)
|
||||
: service.addTagsToResource(resourceArn, tags, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("AddTagsToResource", AwsNamespaces.EC,
|
||||
tagListXml(updated))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleRemoveTagsFromResource(MultivaluedMap<String, String> params, String region) {
|
||||
try {
|
||||
String resourceArn = requiredResourceArn(params);
|
||||
List<String> tagKeys = extractMemberList(params, "TagKeys.member.");
|
||||
if (tagKeys.isEmpty() || tagKeys.stream().anyMatch(key -> key == null || key.isBlank())) {
|
||||
throw new AwsException("InvalidParameterValue", "TagKeys must contain at least one key.", 400);
|
||||
}
|
||||
Map<String, String> tags = isServerlessResource(resourceArn)
|
||||
? serverlessHandler.removeTagsFromResource(resourceArn, tagKeys, region)
|
||||
: service.removeTagsFromResource(resourceArn, tagKeys, region);
|
||||
return Response.ok(AwsQueryResponse.envelope("RemoveTagsFromResource", AwsNamespaces.EC,
|
||||
tagListXml(tags))).build();
|
||||
} catch (AwsException e) {
|
||||
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
private static String tagListXml(Map<String, String> tags) {
|
||||
XmlBuilder xml = new XmlBuilder().start("TagList");
|
||||
tags.forEach((key, value) -> xml.start("Tag")
|
||||
.elem("Key", key)
|
||||
.elem("Value", value)
|
||||
.end("Tag"));
|
||||
return xml.end("TagList").build();
|
||||
}
|
||||
|
||||
private static String requiredResourceArn(MultivaluedMap<String, String> params) {
|
||||
String resourceArn = params.getFirst("ResourceName");
|
||||
if (resourceArn == null || resourceArn.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400);
|
||||
}
|
||||
return resourceArn;
|
||||
}
|
||||
|
||||
private static boolean isServerlessResource(String resourceArn) {
|
||||
try {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
|
||||
String resource = arn.resource();
|
||||
int separator = resource.indexOf(':');
|
||||
return separator > 0 && "serverlesscache".equals(resource.substring(0, separator));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDeleteCacheParameterGroup(MultivaluedMap<String, String> params) {
|
||||
try {
|
||||
service.deleteCacheParameterGroup(params.getFirst("CacheParameterGroupName"));
|
||||
@@ -877,6 +954,29 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
return parsePairs(params, List.of("Tags.Tag", "Tags.member", "Tag"), "Key", "Value");
|
||||
}
|
||||
|
||||
private static Map<String, String> parseMutationTags(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> tags = parseTags(params);
|
||||
boolean memberPresent = false;
|
||||
for (String prefix : List.of("Tags.Tag", "Tags.member", "Tag")) {
|
||||
for (int index = 1; ; index++) {
|
||||
String key = params.getFirst(prefix + "." + index + ".Key");
|
||||
String value = params.getFirst(prefix + "." + index + ".Value");
|
||||
if (key == null && value == null) {
|
||||
break;
|
||||
}
|
||||
memberPresent = true;
|
||||
if (key == null || key.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Tag key cannot be null or empty.", 400);
|
||||
}
|
||||
// The AWS model permits a null tag value, so only the key is required here.
|
||||
}
|
||||
}
|
||||
if (!memberPresent || tags.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "At least one tag is required.", 400);
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private static Map<String, String> parseParameterNameValues(MultivaluedMap<String, String> params) {
|
||||
return parsePairs(params,
|
||||
List.of("ParameterNameValues.ParameterNameValue", "ParameterNameValues.member"),
|
||||
|
||||
+8
@@ -64,6 +64,14 @@ public class ElastiCacheServerlessQueryHandler {
|
||||
return service.getServerlessCache(name).getTags();
|
||||
}
|
||||
|
||||
public Map<String, String> addTagsToResource(String resourceArn, Map<String, String> tags, String region) {
|
||||
return service.addTagsToResource(resourceArn, tags, region);
|
||||
}
|
||||
|
||||
public Map<String, String> removeTagsFromResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
return service.removeTagsFromResource(resourceArn, tagKeys, region);
|
||||
}
|
||||
|
||||
private static String cacheXml(ServerlessCache cache) {
|
||||
return new XmlBuilder().start("ServerlessCache").raw(cacheFields(cache)).end("ServerlessCache").build();
|
||||
}
|
||||
|
||||
+38
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
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.elasticache.ElastiCacheTagResourceArn.TagResourceRef;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.CacheCluster;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ServerlessCache;
|
||||
@@ -243,6 +244,32 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> addTagsToResource(String resourceArn, Map<String, String> tags, String region) {
|
||||
TagResourceRef ref = parseTagResourceArn(resourceArn, region);
|
||||
synchronized (lockFor(ref.id())) {
|
||||
ServerlessCache cache = requireCache(ref.id());
|
||||
requireResourceArn(cache.getArn(), ref.resourceArn(), ref.id());
|
||||
Map<String, String> updatedTags = cache.getTags();
|
||||
updatedTags.putAll(tags);
|
||||
cache.setTags(updatedTags);
|
||||
caches.put(key(ref.id()), cache);
|
||||
return cache.getTags();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> removeTagsFromResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
TagResourceRef ref = parseTagResourceArn(resourceArn, region);
|
||||
synchronized (lockFor(ref.id())) {
|
||||
ServerlessCache cache = requireCache(ref.id());
|
||||
requireResourceArn(cache.getArn(), ref.resourceArn(), ref.id());
|
||||
Map<String, String> updatedTags = cache.getTags();
|
||||
tagKeys.forEach(updatedTags::remove);
|
||||
cache.setTags(updatedTags);
|
||||
caches.put(key(ref.id()), cache);
|
||||
return cache.getTags();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -313,6 +340,17 @@ public class ElastiCacheServerlessService implements Resettable {
|
||||
"Serverless cache " + name + " not found.", 404));
|
||||
}
|
||||
|
||||
private TagResourceRef parseTagResourceArn(String resourceArn, String region) {
|
||||
return ElastiCacheTagResourceArn.parse(resourceArn, region, resolver, "serverlesscache");
|
||||
}
|
||||
|
||||
private static void requireResourceArn(String actualArn, String requestedArn, String id) {
|
||||
if (actualArn == null || !actualArn.equalsIgnoreCase(requestedArn)) {
|
||||
throw new AwsException("ServerlessCacheNotFoundFault",
|
||||
"Serverless cache " + id + " not found.", 404);
|
||||
}
|
||||
}
|
||||
|
||||
private String key(String name) {
|
||||
return resolver.getRegion() + "/" + name;
|
||||
}
|
||||
|
||||
+195
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Subnet;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheTagResourceArn.TagResourceRef;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ValkeyClusterFormation;
|
||||
@@ -1860,6 +1861,12 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
|
||||
List<String> passwords, String accessString, String engine) {
|
||||
return createUser(userId, userName, authMode, passwords, accessString, engine, Map.of());
|
||||
}
|
||||
|
||||
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
|
||||
List<String> passwords, String accessString, String engine,
|
||||
Map<String, String> tags) {
|
||||
if (users.get(userId).isPresent()) {
|
||||
throw new AwsException("UserAlreadyExists",
|
||||
"User " + userId + " already exists.", 400);
|
||||
@@ -1874,12 +1881,194 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
accessString != null ? accessString : "on ~* +@all",
|
||||
normalizedEngine, "active", Instant.now());
|
||||
user.setArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(), "user:" + userId));
|
||||
user.setTags(tags);
|
||||
|
||||
users.put(userId, user);
|
||||
LOG.infov("ElastiCache user {0} created with authMode={1}", userId, authMode);
|
||||
return user;
|
||||
}
|
||||
|
||||
public Map<String, String> addTagsToResource(String resourceArn, Map<String, String> tags,
|
||||
String region) {
|
||||
TagResourceRef ref = ElastiCacheTagResourceArn.parse(resourceArn, region, regionResolver);
|
||||
return switch (ref.type()) {
|
||||
case "user" -> updateUserTags(ref, tags);
|
||||
case "usergroup" -> updateUserGroupTags(ref, tags);
|
||||
case "replicationgroup" -> updateReplicationGroupTags(ref, tags);
|
||||
case "cluster" -> updateCacheClusterTags(ref, tags);
|
||||
case "subnetgroup" -> updateCacheSubnetGroupTags(ref, tags);
|
||||
case "parametergroup" -> updateCacheParameterGroupTags(ref, tags);
|
||||
default -> throw ElastiCacheTagResourceArn.invalid(resourceArn);
|
||||
};
|
||||
}
|
||||
|
||||
public Map<String, String> removeTagsFromResource(String resourceArn, List<String> tagKeys,
|
||||
String region) {
|
||||
TagResourceRef ref = ElastiCacheTagResourceArn.parse(resourceArn, region, regionResolver);
|
||||
return switch (ref.type()) {
|
||||
case "user" -> removeUserTags(ref, tagKeys);
|
||||
case "usergroup" -> removeUserGroupTags(ref, tagKeys);
|
||||
case "replicationgroup" -> removeReplicationGroupTags(ref, tagKeys);
|
||||
case "cluster" -> removeCacheClusterTags(ref, tagKeys);
|
||||
case "subnetgroup" -> removeCacheSubnetGroupTags(ref, tagKeys);
|
||||
case "parametergroup" -> removeCacheParameterGroupTags(ref, tagKeys);
|
||||
default -> throw ElastiCacheTagResourceArn.invalid(resourceArn);
|
||||
};
|
||||
}
|
||||
|
||||
private Map<String, String> updateUserTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (userGroupLock) {
|
||||
ElastiCacheUser user = getUser(ref.id());
|
||||
requireResourceArn(user.getArn(), ref.resourceArn(), "UserNotFound", ref.id());
|
||||
user.getTags().putAll(tags);
|
||||
users.put(ref.id(), user);
|
||||
return new LinkedHashMap<>(user.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeUserTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (userGroupLock) {
|
||||
ElastiCacheUser user = getUser(ref.id());
|
||||
requireResourceArn(user.getArn(), ref.resourceArn(), "UserNotFound", ref.id());
|
||||
tagKeys.forEach(user.getTags()::remove);
|
||||
users.put(ref.id(), user);
|
||||
return new LinkedHashMap<>(user.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> updateUserGroupTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (userGroupLock) {
|
||||
ElastiCacheUserGroup userGroup = getUserGroup(ref.id());
|
||||
requireResourceArn(userGroup.getArn(), ref.resourceArn(), "UserGroupNotFound", ref.id());
|
||||
userGroup.getTags().putAll(tags);
|
||||
userGroups.put(userGroup.getUserGroupId(), userGroup);
|
||||
return new LinkedHashMap<>(userGroup.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeUserGroupTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (userGroupLock) {
|
||||
ElastiCacheUserGroup userGroup = getUserGroup(ref.id());
|
||||
requireResourceArn(userGroup.getArn(), ref.resourceArn(), "UserGroupNotFound", ref.id());
|
||||
tagKeys.forEach(userGroup.getTags()::remove);
|
||||
userGroups.put(userGroup.getUserGroupId(), userGroup);
|
||||
return new LinkedHashMap<>(userGroup.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> updateReplicationGroupTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (lockFor("rg:" + ref.id())) {
|
||||
ReplicationGroup group = getReplicationGroup(ref.id());
|
||||
requireResourceArn(group.getArn(), ref.resourceArn(), "ReplicationGroupNotFoundFault", ref.id());
|
||||
group.getTags().putAll(tags);
|
||||
groups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeReplicationGroupTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (lockFor("rg:" + ref.id())) {
|
||||
ReplicationGroup group = getReplicationGroup(ref.id());
|
||||
requireResourceArn(group.getArn(), ref.resourceArn(), "ReplicationGroupNotFoundFault", ref.id());
|
||||
tagKeys.forEach(group.getTags()::remove);
|
||||
groups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> updateCacheClusterTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (lockFor("cc:" + ref.id())) {
|
||||
StorageBackend<String, CacheCluster> store = cacheClusters.get(ref.id()).isPresent()
|
||||
? cacheClusters : memcachedClusters;
|
||||
CacheCluster cluster = store.get(ref.id())
|
||||
.filter(value -> value.getServerlessCacheName() == null)
|
||||
.orElseThrow(() -> new AwsException("CacheClusterNotFound",
|
||||
"Cache cluster " + ref.id() + " not found.", 404));
|
||||
requireResourceArn(cluster.getArn(), ref.resourceArn(), "CacheClusterNotFound", ref.id());
|
||||
cluster.getTags().putAll(tags);
|
||||
store.put(ref.id(), cluster);
|
||||
return new LinkedHashMap<>(cluster.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeCacheClusterTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (lockFor("cc:" + ref.id())) {
|
||||
StorageBackend<String, CacheCluster> store = cacheClusters.get(ref.id()).isPresent()
|
||||
? cacheClusters : memcachedClusters;
|
||||
CacheCluster cluster = store.get(ref.id())
|
||||
.filter(value -> value.getServerlessCacheName() == null)
|
||||
.orElseThrow(() -> new AwsException("CacheClusterNotFound",
|
||||
"Cache cluster " + ref.id() + " not found.", 404));
|
||||
requireResourceArn(cluster.getArn(), ref.resourceArn(), "CacheClusterNotFound", ref.id());
|
||||
tagKeys.forEach(cluster.getTags()::remove);
|
||||
store.put(ref.id(), cluster);
|
||||
return new LinkedHashMap<>(cluster.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> updateCacheSubnetGroupTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (lockFor("sng:" + ref.id())) {
|
||||
CacheSubnetGroup group = subnetGroups.get(ref.id()).orElseThrow(() ->
|
||||
new AwsException("CacheSubnetGroupNotFoundFault", "Cache subnet group " + ref.id() + " not found.", 400));
|
||||
requireResourceArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(),
|
||||
"subnetgroup:" + ref.id()).toString(), ref.resourceArn(),
|
||||
"CacheSubnetGroupNotFoundFault", ref.id(), 400);
|
||||
group.getTags().putAll(tags);
|
||||
subnetGroups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeCacheSubnetGroupTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (lockFor("sng:" + ref.id())) {
|
||||
CacheSubnetGroup group = subnetGroups.get(ref.id()).orElseThrow(() ->
|
||||
new AwsException("CacheSubnetGroupNotFoundFault", "Cache subnet group " + ref.id() + " not found.", 400));
|
||||
requireResourceArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(),
|
||||
"subnetgroup:" + ref.id()).toString(), ref.resourceArn(),
|
||||
"CacheSubnetGroupNotFoundFault", ref.id(), 400);
|
||||
tagKeys.forEach(group.getTags()::remove);
|
||||
subnetGroups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> updateCacheParameterGroupTags(TagResourceRef ref, Map<String, String> tags) {
|
||||
synchronized (lockFor(ref.id())) {
|
||||
CacheParameterGroup group = parameterGroups.get(ref.id()).orElseThrow(() ->
|
||||
new AwsException("CacheParameterGroupNotFound", "Cache parameter group " + ref.id() + " not found.", 404));
|
||||
requireResourceArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(),
|
||||
"parametergroup:" + ref.id()).toString(), ref.resourceArn(),
|
||||
"CacheParameterGroupNotFound", ref.id());
|
||||
group.getTags().putAll(tags);
|
||||
parameterGroups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> removeCacheParameterGroupTags(TagResourceRef ref, List<String> tagKeys) {
|
||||
synchronized (lockFor(ref.id())) {
|
||||
CacheParameterGroup group = parameterGroups.get(ref.id()).orElseThrow(() ->
|
||||
new AwsException("CacheParameterGroupNotFound", "Cache parameter group " + ref.id() + " not found.", 404));
|
||||
requireResourceArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(),
|
||||
"parametergroup:" + ref.id()).toString(), ref.resourceArn(),
|
||||
"CacheParameterGroupNotFound", ref.id());
|
||||
tagKeys.forEach(group.getTags()::remove);
|
||||
parameterGroups.put(ref.id(), group);
|
||||
return new LinkedHashMap<>(group.getTags());
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireResourceArn(String actualArn, String requestedArn, String errorCode, String id) {
|
||||
requireResourceArn(actualArn, requestedArn, errorCode, id, 404);
|
||||
}
|
||||
|
||||
private static void requireResourceArn(String actualArn, String requestedArn, String errorCode, String id,
|
||||
int httpStatus) {
|
||||
if (actualArn == null || !actualArn.equalsIgnoreCase(requestedArn)) {
|
||||
throw new AwsException(errorCode, "Resource " + id + " not found.", httpStatus);
|
||||
}
|
||||
}
|
||||
|
||||
public ElastiCacheUser getUser(String userId) {
|
||||
return users.get(userId).orElseThrow(() ->
|
||||
new AwsException("UserNotFound", "User " + userId + " not found.", 404));
|
||||
@@ -1970,6 +2159,11 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
public ElastiCacheUserGroup createUserGroup(String userGroupId, String engine, List<String> userIds,
|
||||
String region) {
|
||||
return createUserGroup(userGroupId, engine, userIds, region, Map.of());
|
||||
}
|
||||
|
||||
public ElastiCacheUserGroup createUserGroup(String userGroupId, String engine, List<String> userIds,
|
||||
String region, Map<String, String> tags) {
|
||||
String id = normalizeUserGroupId(userGroupId);
|
||||
// Engine is required on AWS; a missing value defaults to redis as CreateUser does.
|
||||
String normalizedEngine = (engine == null || engine.isBlank()) ? "redis" : normalizeEngine(engine);
|
||||
@@ -1983,6 +2177,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
ElastiCacheUserGroup userGroup = new ElastiCacheUserGroup(
|
||||
id, normalizedEngine, members, "active", Instant.now());
|
||||
userGroup.setArn(regionResolver.buildArn("elasticache", region, "usergroup:" + id));
|
||||
userGroup.setTags(tags);
|
||||
userGroups.put(id, userGroup);
|
||||
LOG.infov("ElastiCache user group {0} created with {1} users", id, members.size());
|
||||
return userGroup;
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package io.github.hectorvent.floci.services.elasticache;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
|
||||
final class ElastiCacheTagResourceArn {
|
||||
|
||||
record TagResourceRef(String type, String id, String resourceArn) {}
|
||||
|
||||
private ElastiCacheTagResourceArn() {
|
||||
}
|
||||
|
||||
static TagResourceRef parse(String resourceArn, String region, RegionResolver resolver) {
|
||||
return parse(resourceArn, region, resolver, null);
|
||||
}
|
||||
|
||||
static TagResourceRef parse(String resourceArn, String region, RegionResolver resolver, String requiredType) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(resourceArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw invalid(resourceArn);
|
||||
}
|
||||
if (!"elasticache".equals(arn.service())
|
||||
|| !resolver.partitionForRegion(region).equals(arn.partition())) {
|
||||
throw invalid(resourceArn);
|
||||
}
|
||||
int separator = arn.resource().indexOf(':');
|
||||
if (separator <= 0 || separator == arn.resource().length() - 1) {
|
||||
throw invalid(resourceArn);
|
||||
}
|
||||
String type = arn.resource().substring(0, separator);
|
||||
if (requiredType != null && !requiredType.equals(type)) {
|
||||
throw invalid(resourceArn);
|
||||
}
|
||||
if (!region.equals(arn.region()) || !resolver.getAccountId().equals(arn.accountId())) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"The resource ARN does not belong to the requested Region or account: " + resourceArn, 400);
|
||||
}
|
||||
String id = arn.resource().substring(separator + 1);
|
||||
if (id.isBlank()) {
|
||||
throw invalid(resourceArn);
|
||||
}
|
||||
return new TagResourceRef(type, id, resourceArn);
|
||||
}
|
||||
|
||||
static AwsException invalid(String resourceArn) {
|
||||
return new AwsException("InvalidARN", "Invalid resource ARN: " + resourceArn, 400);
|
||||
}
|
||||
}
|
||||
+8
@@ -4,7 +4,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public class ElastiCacheUser {
|
||||
@@ -20,6 +22,7 @@ public class ElastiCacheUser {
|
||||
private Instant createdAt;
|
||||
// Minted once in the region of the CreateUser request; null for users persisted before it was stored.
|
||||
private String arn;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public ElastiCacheUser() {}
|
||||
|
||||
@@ -63,6 +66,11 @@ public class ElastiCacheUser {
|
||||
public String getArn() { return arn; }
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isEnabled() {
|
||||
return isAccessStringOn(accessString);
|
||||
|
||||
+6
@@ -18,6 +18,7 @@ public class ElastiCacheUserGroup {
|
||||
private Set<String> userIds = new LinkedHashSet<>();
|
||||
private Instant createdAt;
|
||||
private String arn;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
// Serverless caches attached to this group, by cache name, with the engine each runs.
|
||||
private Map<String, String> serverlessCacheEngines = new LinkedHashMap<>();
|
||||
|
||||
@@ -52,6 +53,11 @@ public class ElastiCacheUserGroup {
|
||||
public String getArn() { return arn; }
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
|
||||
public Map<String, String> getServerlessCacheEngines() { return serverlessCacheEngines; }
|
||||
public void setServerlessCacheEngines(Map<String, String> serverlessCacheEngines) {
|
||||
this.serverlessCacheEngines = serverlessCacheEngines != null ? serverlessCacheEngines : new LinkedHashMap<>();
|
||||
|
||||
Reference in new issue
Block a user