mirror of
https://github.com/floci-io/floci.git
synced 2026-12-06 03:28:18 -05:00
feat(emr-serverless): tag, untag and list tags for applications (#4667)
EMR Serverless had no tagging API: no TagHandler was registered for the emr-serverless ARN service, so TagResource, UntagResource and ListTagsForResource could not be routed, and a tagged aws_emrserverless_application could not be planned. Register an emr-serverless handler with the shared /tags dispatcher, answering 200 as the model does, over the tags already stored on the application. Validate keys and values, the user guide's 50 tag limit and its reserved aws: prefix, refuse job run ARNs as not found, and synchronize the application mutators so a tag write cannot lose a concurrent update. Closes #4662
This commit is contained in:
1 parent
48214e04e0
commit
5aad012e78
8 files changed
+510
-6
No files matched your search
+116
-4
@@ -19,14 +19,25 @@ import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EmrServerlessService {
|
||||
|
||||
// The EMR Serverless user guide's tagging limitations: at most 50 user-created tags per resource
|
||||
// (the model's TagMap max of 200 only bounds a single request), and no "aws:" prefix, in any case,
|
||||
// on a key or a value. TagKey and TagValue share one character set in the API model.
|
||||
private static final int MAX_TAGS = 50;
|
||||
private static final String RESERVED_PREFIX = "aws:";
|
||||
private static final Pattern TAG_TEXT = Pattern.compile("[A-Za-z0-9 /_.:=+@-]*");
|
||||
private static final Pattern RESOURCE = Pattern.compile("/applications/([0-9a-zA-Z]+)(?:/jobruns/([0-9a-zA-Z]+))?");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final AccountAwareStorageBackend<Application> storage;
|
||||
|
||||
@@ -74,6 +85,9 @@ public class EmrServerlessService {
|
||||
app.setStateDetails("");
|
||||
app.setCreatedAt(now);
|
||||
app.setUpdatedAt(now);
|
||||
if (request.getTags() != null) {
|
||||
validateTags(request.getTags());
|
||||
}
|
||||
app.setTags(request.getTags());
|
||||
app.setArchitecture(request.getArchitecture());
|
||||
app.setInitialCapacity(request.getInitialCapacity());
|
||||
@@ -107,7 +121,7 @@ public class EmrServerlessService {
|
||||
);
|
||||
}
|
||||
|
||||
public Application updateApplication(String applicationId, UpdateApplicationRequest request) {
|
||||
public synchronized Application updateApplication(String applicationId, UpdateApplicationRequest request) {
|
||||
Application app = getApplication(applicationId);
|
||||
|
||||
String state = app.getState();
|
||||
@@ -169,7 +183,7 @@ public class EmrServerlessService {
|
||||
return app;
|
||||
}
|
||||
|
||||
public void deleteApplication(String applicationId) {
|
||||
public synchronized void deleteApplication(String applicationId) {
|
||||
Application app = getApplication(applicationId);
|
||||
String state = app.getState();
|
||||
if (!"CREATED".equals(state) && !"STOPPED".equals(state)) {
|
||||
@@ -178,7 +192,7 @@ public class EmrServerlessService {
|
||||
storage.delete(applicationId);
|
||||
}
|
||||
|
||||
public void startApplication(String applicationId) {
|
||||
public synchronized void startApplication(String applicationId) {
|
||||
Application app = getApplication(applicationId);
|
||||
String state = app.getState();
|
||||
if ("STARTED".equals(state) || "STARTING".equals(state)) {
|
||||
@@ -189,7 +203,7 @@ public class EmrServerlessService {
|
||||
storage.put(applicationId, app);
|
||||
}
|
||||
|
||||
public void stopApplication(String applicationId) {
|
||||
public synchronized void stopApplication(String applicationId) {
|
||||
Application app = getApplication(applicationId);
|
||||
String state = app.getState();
|
||||
if ("STOPPED".equals(state) || "STOPPING".equals(state)) {
|
||||
@@ -200,6 +214,104 @@ public class EmrServerlessService {
|
||||
storage.put(applicationId, app);
|
||||
}
|
||||
|
||||
// ---- Tags ---------------------------------------------------------------------------------
|
||||
|
||||
public synchronized Map<String, String> listTags(String arn) {
|
||||
Application app = applicationFor(arn);
|
||||
return app.getTags() != null ? new LinkedHashMap<>(app.getTags()) : new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
/** Adds the tags to the application's, replacing any with the same key. */
|
||||
public synchronized void tagResource(String arn, Map<String, String> tags) {
|
||||
Application app = applicationFor(arn);
|
||||
// An empty map is a valid TagMap (min 0) and changes nothing; the dispatcher already refuses a
|
||||
// missing one.
|
||||
if (tags == null) {
|
||||
throw new AwsException("ValidationException", "tags is required", 400);
|
||||
}
|
||||
validateTags(tags);
|
||||
Map<String, String> merged = app.getTags() != null ? new LinkedHashMap<>(app.getTags()) : new LinkedHashMap<>();
|
||||
merged.putAll(tags);
|
||||
if (merged.size() > MAX_TAGS) {
|
||||
throw new AwsException("ValidationException",
|
||||
"A resource can have at most " + MAX_TAGS + " tags.", 400);
|
||||
}
|
||||
app.setTags(merged);
|
||||
storage.put(app.getApplicationId(), app);
|
||||
}
|
||||
|
||||
public synchronized void untagResource(String arn, List<String> tagKeys) {
|
||||
Application app = applicationFor(arn);
|
||||
if (tagKeys == null || tagKeys.isEmpty()) {
|
||||
throw new AwsException("ValidationException", "tagKeys is required", 400);
|
||||
}
|
||||
tagKeys.forEach(EmrServerlessService::validateTagKey);
|
||||
if (app.getTags() == null || app.getTags().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<String, String> remaining = new LinkedHashMap<>(app.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
app.setTags(remaining);
|
||||
storage.put(app.getApplicationId(), app);
|
||||
}
|
||||
|
||||
/**
|
||||
* The application an EMR Serverless ARN names:
|
||||
* {@code arn:<partition>:emr-serverless:<region>:<account>:/applications/<id>}, the form
|
||||
* CreateApplication returns. A job run ARN ({@code .../jobruns/<id>}) is refused, as job runs are
|
||||
* not emulated.
|
||||
*/
|
||||
private Application applicationFor(String arn) {
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ValidationException", "Invalid resourceArn: " + arn, 400);
|
||||
}
|
||||
Matcher matcher = RESOURCE.matcher(parsed.resource());
|
||||
if (!"emr-serverless".equals(parsed.service()) || !matcher.matches()) {
|
||||
throw new AwsException("ValidationException", "Invalid resourceArn: " + arn, 400);
|
||||
}
|
||||
if (matcher.group(2) != null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Job run " + matcher.group(2) + " not found", 404);
|
||||
}
|
||||
return storage.get(matcher.group(1))
|
||||
.filter(app -> arn.equals(app.getArn()))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Resource " + arn + " not found", 404));
|
||||
}
|
||||
|
||||
/** At most 50 tags, each key and value within TagKey and TagValue and free of the reserved prefix. */
|
||||
private static void validateTags(Map<String, String> tags) {
|
||||
if (tags.size() > MAX_TAGS) {
|
||||
throw new AwsException("ValidationException",
|
||||
"A resource can have at most " + MAX_TAGS + " tags.", 400);
|
||||
}
|
||||
tags.forEach((key, value) -> {
|
||||
validateTagKey(key);
|
||||
if (hasReservedPrefix(key)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Tag keys may not begin with the reserved prefix aws: (" + key + ")", 400);
|
||||
}
|
||||
if (value == null || value.length() > 256 || !TAG_TEXT.matcher(value).matches()) {
|
||||
throw new AwsException("ValidationException", "Invalid value for tag " + key, 400);
|
||||
}
|
||||
if (hasReservedPrefix(value)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Tag values may not begin with the reserved prefix aws: (tag " + key + ")", 400);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void validateTagKey(String key) {
|
||||
if (key == null || key.isEmpty() || key.length() > 128 || !TAG_TEXT.matcher(key).matches()) {
|
||||
throw new AwsException("ValidationException", "Invalid tag key: " + key, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasReservedPrefix(String text) {
|
||||
return text.regionMatches(true, 0, RESERVED_PREFIX, 0, RESERVED_PREFIX.length());
|
||||
}
|
||||
|
||||
private String generateId() {
|
||||
return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||
}
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.TagHandler;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* EMR Serverless on the shared {@code /tags/{resourceArn}} path. The wire shape is the common one (a
|
||||
* lowercase {@code tags} map, a {@code tagKeys} query and {@code POST}), except that TagResource and
|
||||
* UntagResource answer 200 rather than 204, and payloads are validated strictly.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EmrServerlessTagHandler implements TagHandler {
|
||||
|
||||
private final EmrServerlessService service;
|
||||
|
||||
@Inject
|
||||
public EmrServerlessTagHandler(EmrServerlessService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serviceKey() {
|
||||
return "emr-serverless";
|
||||
}
|
||||
|
||||
/** Tag values must be JSON strings, as the model's TagMap declares; nothing is coerced. */
|
||||
@Override
|
||||
public boolean strictTagValidation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int tagResourceSuccessStatus() {
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int untagResourceSuccessStatus() {
|
||||
return 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> listTags(String region, String arn) {
|
||||
return service.listTags(arn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
service.tagResource(arn, tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void untagResource(String region, String arn, List<String> tagKeys) {
|
||||
service.untagResource(arn, tagKeys);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user