mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(transcribe): keep tags on create and serve the tag operations (#9700)
This commit is contained in:
1 parent
09a2cc0190
commit
10855b0023
10 files changed
+550
-16
No files matched your search
@@ -161,7 +161,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
|
||||
| [Comprehend](comprehend.md) | `POST /` + `X-Amz-Target: Comprehend_20171127.*` | JSON 1.1 | 5 |
|
||||
| [Rekognition](rekognition.md) | `POST /` + `X-Amz-Target: RekognitionService.*` | JSON 1.1 | 14 |
|
||||
| [Translate](translate.md) | `POST /` + `X-Amz-Target: AWSShineFrontendService_20170701.*` | JSON 1.1 | 3 |
|
||||
| [Transcribe](transcribe.md) | `POST /` + `X-Amz-Target: Transcribe.*` | JSON 1.1 | 8 |
|
||||
| [Transcribe](transcribe.md) | `POST /` + `X-Amz-Target: Transcribe.*` | JSON 1.1 | 11 |
|
||||
| [Pricing](pricing.md) | `POST /` + `X-Amz-Target: AWSPriceListService.*` | JSON 1.1 | 5 |
|
||||
| [Cost Explorer](ce.md) | `POST /` + `X-Amz-Target: AWSInsightsIndexService.*` | JSON 1.1 | 20 |
|
||||
| [Cost and Usage Reports](cur.md) | `POST /` + `X-Amz-Target: AWSOrigamiServiceGatewayService.*` | JSON 1.1 | 7 |
|
||||
|
||||
@@ -25,6 +25,12 @@ and CLI clients can exercise their Transcribe control-flow code locally.
|
||||
| `GetVocabulary` | Returns a stored vocabulary by `VocabularyName` |
|
||||
| `ListVocabularies` | Lists vocabularies with optional `StateEquals`, `NameContains`, and `MaxResults` filters |
|
||||
| `DeleteVocabulary` | Removes a stored vocabulary |
|
||||
| `TagResource` | Adds or overwrites tags on a transcription job or vocabulary ARN |
|
||||
| `UntagResource` | Removes tags by key from a transcription job or vocabulary ARN |
|
||||
| `ListTagsForResource` | Lists the tags of a transcription job or vocabulary ARN |
|
||||
|
||||
`StartTranscriptionJob` and `CreateVocabulary` accept `Tags`. A job returns its tags in
|
||||
`TranscriptionJob.Tags`.
|
||||
|
||||
`LanguageCode` defaults to `en-US` and `MediaFormat` defaults to `mp4` when
|
||||
they are omitted from `StartTranscriptionJob`.
|
||||
|
||||
@@ -74,7 +74,8 @@ import java.util.Optional;
|
||||
* delivery destinations of a delivery source and the tags of a log group, a destination, a delivery source, a
|
||||
* delivery destination or a delivery; Pricing Calculator, for the tags of a workload estimate; Amazon Managed Service
|
||||
* for Prometheus, for the tags of a workspace or a rule groups namespace; AWS Config, for the config rule,
|
||||
* configuration recorder or conformance pack a name names, whose id it minted); looked up lazily by
|
||||
* configuration recorder or conformance pack a name names, whose id it minted; Transcribe, for the tags of a
|
||||
* transcription job or a vocabulary); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
|
||||
@@ -2,8 +2,11 @@ package io.floci.aws.services.transcribe;
|
||||
|
||||
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.floci.aws.core.common.AwsErrorResponse;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.services.transcribe.model.TranscriptionJob;
|
||||
import io.floci.aws.services.transcribe.model.VocabularyInfo;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -11,6 +14,9 @@ import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -41,7 +47,8 @@ public class TranscribeJsonHandler {
|
||||
getStringField(request, "TranscriptionJobName"),
|
||||
getMediaFileUri(request),
|
||||
getStringField(request, "LanguageCode"),
|
||||
getStringField(request, "MediaFormat"));
|
||||
getStringField(request, "MediaFormat"),
|
||||
getTags(request));
|
||||
yield Response.ok(Map.of("TranscriptionJob", job)).build();
|
||||
}
|
||||
case "GetTranscriptionJob" -> {
|
||||
@@ -74,7 +81,8 @@ public class TranscribeJsonHandler {
|
||||
case "CreateVocabulary" -> {
|
||||
VocabularyInfo vocab = transcribeService.createVocabulary(
|
||||
getStringField(request, "VocabularyName"),
|
||||
getStringField(request, "LanguageCode"));
|
||||
getStringField(request, "LanguageCode"),
|
||||
getTags(request));
|
||||
yield Response.ok(vocab).build();
|
||||
}
|
||||
case "GetVocabulary" -> {
|
||||
@@ -103,6 +111,23 @@ public class TranscribeJsonHandler {
|
||||
getStringField(request, "VocabularyName"));
|
||||
yield Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
case "TagResource" -> {
|
||||
transcribeService.tagResource(getStringField(request, "ResourceArn"), getTags(request));
|
||||
yield Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
case "UntagResource" -> {
|
||||
transcribeService.untagResource(getStringField(request, "ResourceArn"), getTagKeys(request));
|
||||
yield Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
case "ListTagsForResource" -> {
|
||||
String resourceArn = getStringField(request, "ResourceArn");
|
||||
Map<String, String> tags = transcribeService.listTagsForResource(resourceArn);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("ResourceArn", resourceArn);
|
||||
ArrayNode tagList = root.putArray("Tags");
|
||||
tags.forEach((key, value) -> tagList.addObject().put("Key", key).put("Value", value));
|
||||
yield Response.ok(root).build();
|
||||
}
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnknownOperationException",
|
||||
"Unknown operation: Transcribe." + action))
|
||||
@@ -115,6 +140,49 @@ public class TranscribeJsonHandler {
|
||||
return (value != null && !value.isNull()) ? value.asText() : null;
|
||||
}
|
||||
|
||||
private Map<String, String> getTags(JsonNode request) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
JsonNode tagList = request == null ? null : request.get("Tags");
|
||||
if (tagList == null || tagList.isNull()) {
|
||||
return tags;
|
||||
}
|
||||
if (!tagList.isArray()) {
|
||||
throw new AwsException("BadRequestException", "Tags must be a list of Key and Value pairs.", 400);
|
||||
}
|
||||
for (int index = 0; index < tagList.size(); index++) {
|
||||
JsonNode tag = tagList.get(index);
|
||||
String path = "Tags." + (index + 1) + ".member.";
|
||||
if (!tag.path("Key").isTextual() || tag.path("Key").asText().isEmpty()) {
|
||||
throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNull(path + "Key"));
|
||||
}
|
||||
if (!tag.path("Value").isTextual()) {
|
||||
throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNull(path + "Value"));
|
||||
}
|
||||
tags.put(tag.get("Key").asText(), tag.get("Value").asText());
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private List<String> getTagKeys(JsonNode request) {
|
||||
List<String> tagKeys = new ArrayList<>();
|
||||
JsonNode keyList = request == null ? null : request.get("TagKeys");
|
||||
if (keyList == null || keyList.isNull()) {
|
||||
return tagKeys;
|
||||
}
|
||||
if (!keyList.isArray()) {
|
||||
throw new AwsException("BadRequestException", "TagKeys must be a list of tag keys.", 400);
|
||||
}
|
||||
for (int index = 0; index < keyList.size(); index++) {
|
||||
JsonNode key = keyList.get(index);
|
||||
if (!key.isTextual() || key.asText().isEmpty()) {
|
||||
throw SmithyValidationErrors.badRequest(
|
||||
SmithyValidationErrors.notNull("TagKeys." + (index + 1) + ".member"));
|
||||
}
|
||||
tagKeys.add(key.asText());
|
||||
}
|
||||
return tagKeys;
|
||||
}
|
||||
|
||||
private String getMediaFileUri(JsonNode request) {
|
||||
if (request == null) return null;
|
||||
JsonNode media = request.path("Media");
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package io.floci.aws.services.transcribe;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
@@ -16,6 +18,7 @@ import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -30,12 +33,20 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
@ApplicationScoped
|
||||
public class TranscribeService implements Resettable {
|
||||
|
||||
private static final String JOB_RESOURCE = "transcription-job";
|
||||
private static final String VOCABULARY_RESOURCE = "vocabulary";
|
||||
private static final int MAX_TAGS_PER_RESOURCE = 50;
|
||||
private static final int MAX_TAG_KEY_LENGTH = 128;
|
||||
private static final int MAX_TAG_VALUE_LENGTH = 256;
|
||||
private static final String RESERVED_TAG_PREFIX = "aws:";
|
||||
|
||||
private final StorageFactory storageFactory;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
// Transcription jobs are transient (async work deleted after processing); vocabularies are durable.
|
||||
private final ConcurrentHashMap<String, TranscriptionJob> transcriptionJobs = new ConcurrentHashMap<>();
|
||||
private AccountAwareStorageBackend<VocabularyInfo> vocabularies;
|
||||
private final Object tagLock = new Object();
|
||||
|
||||
@Inject
|
||||
public TranscribeService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
@@ -66,8 +77,15 @@ public class TranscribeService implements Resettable {
|
||||
|
||||
public TranscriptionJob startTranscriptionJob(String jobName, String mediaFileUri,
|
||||
String languageCode, String mediaFormat) {
|
||||
return startTranscriptionJob(jobName, mediaFileUri, languageCode, mediaFormat, Map.of());
|
||||
}
|
||||
|
||||
public TranscriptionJob startTranscriptionJob(String jobName, String mediaFileUri,
|
||||
String languageCode, String mediaFormat,
|
||||
Map<String, String> tags) {
|
||||
requireNonBlank(jobName, "TranscriptionJobName");
|
||||
requireNonBlank(mediaFileUri, "Media.MediaFileUri");
|
||||
validateTags(tags);
|
||||
String jobKey = jobKey(jobName);
|
||||
if (transcriptionJobs.containsKey(jobKey)) {
|
||||
throw new AwsException("ConflictException",
|
||||
@@ -83,7 +101,7 @@ public class TranscribeService implements Resettable {
|
||||
48000,
|
||||
new TranscriptionJob.Media(mediaFileUri),
|
||||
new TranscriptionJob.Transcript("s3://floci-transcribe-output/" + jobName + ".json"),
|
||||
now, now, now);
|
||||
now, now, now, toTagList(tags));
|
||||
|
||||
transcriptionJobs.put(jobKey, job);
|
||||
return job;
|
||||
@@ -131,17 +149,25 @@ public class TranscribeService implements Resettable {
|
||||
}
|
||||
|
||||
public VocabularyInfo createVocabulary(String vocabularyName, String languageCode) {
|
||||
return createVocabulary(vocabularyName, languageCode, Map.of());
|
||||
}
|
||||
|
||||
public VocabularyInfo createVocabulary(String vocabularyName, String languageCode,
|
||||
Map<String, String> tags) {
|
||||
requireNonBlank(vocabularyName, "VocabularyName");
|
||||
requireNonBlank(languageCode, "LanguageCode");
|
||||
validateTags(tags);
|
||||
if (getStoredVocabulary(vocabularyName).isPresent()) {
|
||||
throw new AwsException("ConflictException",
|
||||
"The requested vocabulary name already exists. Use a different vocabulary name.", 400);
|
||||
}
|
||||
|
||||
VocabularyInfo vocab = new VocabularyInfo(
|
||||
vocabularyName, languageCode, "READY", Instant.now().getEpochSecond());
|
||||
vocabularies.putForAccount(regionResolver.getAccountId(), vocabularyKey(vocabularyName), vocab);
|
||||
return vocab;
|
||||
VocabularyInfo vocab = new VocabularyInfo(vocabularyName, languageCode, "READY",
|
||||
Instant.now().getEpochSecond(), tags == null || tags.isEmpty() ? null : new LinkedHashMap<>(tags));
|
||||
synchronized (tagLock) {
|
||||
vocabularies.putForAccount(regionResolver.getAccountId(), vocabularyKey(vocabularyName), vocab);
|
||||
}
|
||||
return vocab.withTags(null);
|
||||
}
|
||||
|
||||
public VocabularyInfo getVocabulary(String vocabularyName) {
|
||||
@@ -152,7 +178,7 @@ public class TranscribeService implements Resettable {
|
||||
"The requested vocabulary couldn't be found. Check the vocabulary name and try your request again.",
|
||||
400);
|
||||
}
|
||||
return vocab;
|
||||
return vocab.withTags(null);
|
||||
}
|
||||
|
||||
public ListVocabulariesResult listVocabularies(String stateEquals, String nameContains,
|
||||
@@ -168,6 +194,7 @@ public class TranscribeService implements Resettable {
|
||||
.filter(v -> stateEquals == null || stateEquals.equals(v.vocabularyState()))
|
||||
.filter(v -> nameContains == null || v.vocabularyName().contains(nameContains))
|
||||
.sorted(Comparator.comparing(VocabularyInfo::vocabularyName))
|
||||
.map(vocabulary -> vocabulary.withTags(null))
|
||||
.toList();
|
||||
|
||||
int startIndex = vocabularyStartIndex(filtered, nextToken);
|
||||
@@ -186,7 +213,144 @@ public class TranscribeService implements Resettable {
|
||||
"The requested vocabulary couldn't be found. Check the vocabulary name and try your request again.",
|
||||
400);
|
||||
}
|
||||
vocabularies.deleteForAccount(regionResolver.getAccountId(), vocabularyKey(vocabularyName));
|
||||
synchronized (tagLock) {
|
||||
vocabularies.deleteForAccount(regionResolver.getAccountId(), vocabularyKey(vocabularyName));
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listTagsForResource(String resourceArn) {
|
||||
return readTags(resolveTaggable(resourceArn));
|
||||
}
|
||||
|
||||
public void tagResource(String resourceArn, Map<String, String> tags) {
|
||||
requireNonBlank(resourceArn, "ResourceArn");
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNull("Tags"));
|
||||
}
|
||||
validateTags(tags);
|
||||
synchronized (tagLock) {
|
||||
Taggable taggable = resolveTaggable(resourceArn);
|
||||
Map<String, String> merged = readTags(taggable);
|
||||
merged.putAll(tags);
|
||||
requireWithinTagLimit(merged);
|
||||
writeTags(taggable, merged);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagResource(String resourceArn, List<String> tagKeys) {
|
||||
requireNonBlank(resourceArn, "ResourceArn");
|
||||
if (tagKeys == null || tagKeys.isEmpty()) {
|
||||
throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNull("TagKeys"));
|
||||
}
|
||||
synchronized (tagLock) {
|
||||
Taggable taggable = resolveTaggable(resourceArn);
|
||||
Map<String, String> remaining = readTags(taggable);
|
||||
tagKeys.forEach(remaining::remove);
|
||||
writeTags(taggable, remaining);
|
||||
}
|
||||
}
|
||||
|
||||
/** The tags IAM reads for the resource an ARN names, in the ARN's own account and region. */
|
||||
Map<String, String> storedTags(String resourceArn) {
|
||||
return parseTaggable(resourceArn).map(this::readTags).orElse(Map.of());
|
||||
}
|
||||
|
||||
private Taggable resolveTaggable(String resourceArn) {
|
||||
requireNonBlank(resourceArn, "ResourceArn");
|
||||
Taggable taggable = parseTaggable(resourceArn).orElseThrow(() -> new AwsException("BadRequestException",
|
||||
"The specified resource ARN isn't valid. Check the ARN and try your request again.", 400));
|
||||
boolean inScope = regionResolver.getRegion().equals(taggable.region())
|
||||
&& regionResolver.getAccountId().equals(taggable.accountId());
|
||||
boolean exists = inScope && switch (taggable.type()) {
|
||||
case JOB_RESOURCE -> transcriptionJobs.containsKey(jobKey(taggable.name()));
|
||||
case VOCABULARY_RESOURCE -> getStoredVocabulary(taggable.name()).isPresent();
|
||||
default -> false;
|
||||
};
|
||||
if (!exists) {
|
||||
throw new AwsException("NotFoundException",
|
||||
"The requested resource couldn't be found. Check the resource ARN and try your request again.",
|
||||
400);
|
||||
}
|
||||
return taggable;
|
||||
}
|
||||
|
||||
private Optional<Taggable> parseTaggable(String resourceArn) {
|
||||
if (!AwsArnUtils.isArnFor(resourceArn, "transcribe")) {
|
||||
return Optional.empty();
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
|
||||
int separator = arn.resource().indexOf('/');
|
||||
if (separator <= 0 || separator == arn.resource().length() - 1
|
||||
|| !arn.partition().equals(AwsRegions.partitionFor(arn.region()))) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(new Taggable(arn.resource().substring(0, separator), arn.accountId(), arn.region(),
|
||||
arn.resource().substring(separator + 1)));
|
||||
}
|
||||
|
||||
private Map<String, String> readTags(Taggable taggable) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
String scopedName = taggable.region() + "/" + taggable.name();
|
||||
if (JOB_RESOURCE.equals(taggable.type())) {
|
||||
TranscriptionJob job = transcriptionJobs.get(taggable.accountId() + "/" + scopedName);
|
||||
if (job != null && job.tags() != null) {
|
||||
job.tags().forEach(tag -> tags.put(tag.key(), tag.value()));
|
||||
}
|
||||
} else if (VOCABULARY_RESOURCE.equals(taggable.type())) {
|
||||
vocabularies.getForAccount(taggable.accountId(), scopedName)
|
||||
.map(VocabularyInfo::tags)
|
||||
.ifPresent(tags::putAll);
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private void writeTags(Taggable taggable, Map<String, String> tags) {
|
||||
if (JOB_RESOURCE.equals(taggable.type())) {
|
||||
transcriptionJobs.computeIfPresent(jobKey(taggable.name()),
|
||||
(ignored, job) -> job.withTags(toTagList(tags)));
|
||||
return;
|
||||
}
|
||||
String key = vocabularyKey(taggable.name());
|
||||
vocabularies.getForAccount(regionResolver.getAccountId(), key).ifPresent(vocabulary ->
|
||||
vocabularies.putForAccount(regionResolver.getAccountId(), key,
|
||||
vocabulary.withTags(tags.isEmpty() ? null : tags)));
|
||||
}
|
||||
|
||||
private void validateTags(Map<String, String> tags) {
|
||||
if (tags == null) {
|
||||
return;
|
||||
}
|
||||
requireWithinTagLimit(tags);
|
||||
tags.forEach((key, value) -> {
|
||||
if (key.regionMatches(true, 0, RESERVED_TAG_PREFIX, 0, RESERVED_TAG_PREFIX.length())) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Tag keys can't begin with the reserved prefix '" + RESERVED_TAG_PREFIX + "'.", 400);
|
||||
}
|
||||
if (key.length() > MAX_TAG_KEY_LENGTH) {
|
||||
throw SmithyValidationErrors.badRequest(
|
||||
SmithyValidationErrors.lengthOutOfRange("Tags.Key", key, 1, MAX_TAG_KEY_LENGTH));
|
||||
}
|
||||
if (value.length() > MAX_TAG_VALUE_LENGTH) {
|
||||
throw SmithyValidationErrors.badRequest(
|
||||
SmithyValidationErrors.lengthOutOfRange("Tags.Value", value, 0, MAX_TAG_VALUE_LENGTH));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void requireWithinTagLimit(Map<String, String> tags) {
|
||||
if (tags.size() > MAX_TAGS_PER_RESOURCE) {
|
||||
throw new AwsException("LimitExceededException",
|
||||
"A resource can have at most " + MAX_TAGS_PER_RESOURCE + " tags.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private List<TranscriptionJob.Tag> toTagList(Map<String, String> tags) {
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return tags.entrySet().stream()
|
||||
.map(entry -> new TranscriptionJob.Tag(entry.getKey(), entry.getValue()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private String jobKey(String jobName) {
|
||||
@@ -261,6 +425,8 @@ public class TranscribeService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
private record Taggable(String type, String accountId, String region, String name) {}
|
||||
|
||||
public record ListTranscriptionJobsResult(
|
||||
List<TranscriptionJobSummary> summaries, String status, String nextToken) {}
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.floci.aws.services.transcribe;
|
||||
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks Transcribe of the resources a request names: the tags Transcribe holds on a transcription job or a
|
||||
* vocabulary, which an {@code aws:ResourceTag} condition reads.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class TranscribeStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final TranscribeService transcribe;
|
||||
|
||||
@Inject
|
||||
public TranscribeStoredResourceArns(TranscribeService transcribe) {
|
||||
this.transcribe = transcribe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "transcribe";
|
||||
}
|
||||
|
||||
/** Transcribe stores no resource a request names but by its ARN or its format's variables. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
return transcribe.storedTags(arn);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record TranscriptionJob(
|
||||
@@ -16,8 +18,20 @@ public record TranscriptionJob(
|
||||
@JsonProperty("Transcript") Transcript transcript,
|
||||
@JsonProperty("CreationTime") Long creationTime,
|
||||
@JsonProperty("StartTime") Long startTime,
|
||||
@JsonProperty("CompletionTime") Long completionTime
|
||||
@JsonProperty("CompletionTime") Long completionTime,
|
||||
@JsonProperty("Tags") List<Tag> tags
|
||||
) {
|
||||
public TranscriptionJob withTags(List<Tag> newTags) {
|
||||
return new TranscriptionJob(transcriptionJobName, transcriptionJobStatus, languageCode, mediaFormat,
|
||||
mediaSampleRateHertz, media, transcript, creationTime, startTime, completionTime, newTags);
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
public record Tag(
|
||||
@JsonProperty("Key") String key,
|
||||
@JsonProperty("Value") String value
|
||||
) {}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record Media(
|
||||
|
||||
@@ -4,11 +4,18 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public record VocabularyInfo(
|
||||
@JsonProperty("VocabularyName") String vocabularyName,
|
||||
@JsonProperty("LanguageCode") String languageCode,
|
||||
@JsonProperty("VocabularyState") String vocabularyState,
|
||||
@JsonProperty("LastModifiedTime") Long lastModifiedTime
|
||||
) {}
|
||||
@JsonProperty("LastModifiedTime") Long lastModifiedTime,
|
||||
@JsonProperty("Tags") Map<String, String> tags
|
||||
) {
|
||||
public VocabularyInfo withTags(Map<String, String> newTags) {
|
||||
return new VocabularyInfo(vocabularyName, languageCode, vocabularyState, lastModifiedTime, newTags);
|
||||
}
|
||||
}
|
||||
@@ -551,6 +551,106 @@ class TranscribeIntegrationTest {
|
||||
|
||||
// ── Unknown operation ────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void tagsGivenOnCreate_areReturnedAndCanBeChanged() {
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/tag-vocab";
|
||||
|
||||
call("StartTranscriptionJob", """
|
||||
{"TranscriptionJobName":"tag-job","LanguageCode":"en-US",
|
||||
"Media":{"MediaFileUri":"s3://tag-bucket/a.wav"},
|
||||
"Tags":[{"Key":"team","Value":"red"}]}""")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TranscriptionJob.Tags", hasSize(1))
|
||||
.body("TranscriptionJob.Tags[0].Key", equalTo("team"))
|
||||
.body("TranscriptionJob.Tags[0].Value", equalTo("red"));
|
||||
|
||||
call("CreateVocabulary", """
|
||||
{"VocabularyName":"tag-vocab","LanguageCode":"en-US","Phrases":["hello","world"],
|
||||
"Tags":[{"Key":"team","Value":"red"}]}""")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("$", not(hasKey("Tags")));
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + vocabularyArn + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ResourceArn", equalTo(vocabularyArn))
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("team"))
|
||||
.body("Tags[0].Value", equalTo("red"));
|
||||
|
||||
call("TagResource", "{\"ResourceArn\":\"" + vocabularyArn
|
||||
+ "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"dev\"}]}")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
call("UntagResource", "{\"ResourceArn\":\"" + vocabularyArn + "\",\"TagKeys\":[\"team\"]}")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + vocabularyArn + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("env"))
|
||||
.body("Tags[0].Value", equalTo("dev"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTagsForResource_unknownResource_returnsNotFound() {
|
||||
call("ListTagsForResource",
|
||||
"{\"ResourceArn\":\"arn:aws:transcribe:us-east-1:000000000000:vocabulary/no-such-vocab\"}")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagWithoutKey_returnsBadRequest() {
|
||||
call("CreateVocabulary", """
|
||||
{"VocabularyName":"tag-vocab-no-key","LanguageCode":"en-US","Tags":[{"Value":"red"}]}""")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
|
||||
call("GetVocabulary", "{\"VocabularyName\":\"tag-vocab-no-key\"}")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void untagResource_nonStringTagKey_returnsBadRequestAndKeepsTags() {
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/tag-vocab-null-key";
|
||||
|
||||
call("CreateVocabulary", """
|
||||
{"VocabularyName":"tag-vocab-null-key","LanguageCode":"en-US",
|
||||
"Tags":[{"Key":"null","Value":"kept"}]}""")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
call("UntagResource", "{\"ResourceArn\":\"" + vocabularyArn + "\",\"TagKeys\":[null]}")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
|
||||
call("ListTagsForResource", "{\"ResourceArn\":\"" + vocabularyArn + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("null"));
|
||||
}
|
||||
|
||||
private static Response call(String action, String body) {
|
||||
return given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "Transcribe." + action)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownAction_returnsUnknownOperationError() {
|
||||
given()
|
||||
|
||||
@@ -5,14 +5,18 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.transcribe.model.TranscriptionJob;
|
||||
import io.floci.aws.services.transcribe.model.VocabularyInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -88,7 +92,7 @@ class TranscribeServiceTest {
|
||||
@Test
|
||||
void legacyVocabularyIsVisibleOnlyInDefaultAccountAndRegion() {
|
||||
vocabularyStore.put("legacy-vocabulary",
|
||||
new VocabularyInfo("legacy-vocabulary", "en-US", "READY", 1L));
|
||||
new VocabularyInfo("legacy-vocabulary", "en-US", "READY", 1L, null));
|
||||
|
||||
assertEquals(1, service.listVocabularies(null, null, null, null).vocabularies().size());
|
||||
assertEquals("en-US", service.getVocabulary("legacy-vocabulary").languageCode());
|
||||
@@ -106,7 +110,7 @@ class TranscribeServiceTest {
|
||||
@Test
|
||||
void accountPrefixedLegacyVocabularyMigratesForNonDefaultAccount() {
|
||||
vocabularyStore.putForAccount("111111111111", "legacy-vocabulary",
|
||||
new VocabularyInfo("legacy-vocabulary", "en-US", "READY", 1L));
|
||||
new VocabularyInfo("legacy-vocabulary", "en-US", "READY", 1L, null));
|
||||
account.set("111111111111");
|
||||
region.set("eu-west-1");
|
||||
|
||||
@@ -119,6 +123,134 @@ class TranscribeServiceTest {
|
||||
assertThrows(AwsException.class, () -> service.getVocabulary("legacy-vocabulary"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsGivenOnCreateAreListedAndCanBeChanged() {
|
||||
String jobArn = "arn:aws:transcribe:us-east-1:000000000000:transcription-job/tagged-job";
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/tagged-vocabulary";
|
||||
|
||||
TranscriptionJob job = service.startTranscriptionJob("tagged-job", "s3://bucket/one.wav", "en-US", "wav",
|
||||
Map.of("team", "red"));
|
||||
service.createVocabulary("tagged-vocabulary", "en-US", Map.of("team", "red"));
|
||||
|
||||
assertEquals(List.of(new TranscriptionJob.Tag("team", "red")), job.tags());
|
||||
assertEquals(Map.of("team", "red"), service.listTagsForResource(jobArn));
|
||||
assertEquals(Map.of("team", "red"), service.listTagsForResource(vocabularyArn));
|
||||
|
||||
service.tagResource(jobArn, Map.of("env", "dev"));
|
||||
service.tagResource(vocabularyArn, Map.of("env", "dev"));
|
||||
service.untagResource(jobArn, List.of("team"));
|
||||
service.untagResource(vocabularyArn, List.of("team"));
|
||||
|
||||
assertEquals(Map.of("env", "dev"), service.listTagsForResource(jobArn));
|
||||
assertEquals(Map.of("env", "dev"), service.listTagsForResource(vocabularyArn));
|
||||
assertEquals(List.of(new TranscriptionJob.Tag("env", "dev")),
|
||||
service.getTranscriptionJob("tagged-job").tags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void vocabularyTagsAreKeptOnItsRecordAndLeftOutOfResponses() {
|
||||
VocabularyInfo created = service.createVocabulary("one-record", "en-US", Map.of("team", "red"));
|
||||
|
||||
assertNull(created.tags());
|
||||
assertNull(service.getVocabulary("one-record").tags());
|
||||
assertNull(service.listVocabularies(null, null, null, null).vocabularies().getFirst().tags());
|
||||
assertEquals(Map.of("team", "red"),
|
||||
vocabularyStore.getForAccount(DEFAULT_ACCOUNT, "us-east-1/one-record").orElseThrow().tags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletedVocabularyLosesItsTags() {
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/recreated";
|
||||
service.createVocabulary("recreated", "en-US", Map.of("team", "red"));
|
||||
service.deleteVocabulary("recreated");
|
||||
service.createVocabulary("recreated", "en-US");
|
||||
|
||||
assertTrue(service.listTagsForResource(vocabularyArn).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void taggingRejectsMissingAndMalformedResources() {
|
||||
AwsException missing = assertThrows(AwsException.class, () -> service.listTagsForResource(
|
||||
"arn:aws:transcribe:us-east-1:000000000000:vocabulary/missing"));
|
||||
AwsException otherRegion = assertThrows(AwsException.class, () -> service.listTagsForResource(
|
||||
"arn:aws:transcribe:eu-west-1:000000000000:vocabulary/missing"));
|
||||
AwsException malformed = assertThrows(AwsException.class, () -> service.listTagsForResource("not-an-arn"));
|
||||
AwsException noName = assertThrows(AwsException.class, () -> service.listTagsForResource(
|
||||
"arn:aws:transcribe:us-east-1:000000000000:vocabulary"));
|
||||
AwsException otherPartition = assertThrows(AwsException.class, () -> service.listTagsForResource(
|
||||
"arn:aws-cn:transcribe:us-east-1:000000000000:vocabulary/missing"));
|
||||
|
||||
assertEquals("NotFoundException", missing.getErrorCode());
|
||||
assertEquals("NotFoundException", otherRegion.getErrorCode());
|
||||
assertEquals("BadRequestException", malformed.getErrorCode());
|
||||
assertEquals("BadRequestException", noName.getErrorCode());
|
||||
assertEquals("BadRequestException", otherPartition.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsAreCappedInCountAndLength() {
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/capped";
|
||||
Map<String, String> fifty = new LinkedHashMap<>();
|
||||
for (int index = 0; index < 50; index++) {
|
||||
fifty.put("key-" + index, "value");
|
||||
}
|
||||
service.createVocabulary("capped", "en-US", fifty);
|
||||
|
||||
AwsException tooMany = assertThrows(AwsException.class,
|
||||
() -> service.tagResource(vocabularyArn, Map.of("one-more", "value")));
|
||||
AwsException longKey = assertThrows(AwsException.class,
|
||||
() -> service.tagResource(vocabularyArn, Map.of("k".repeat(129), "value")));
|
||||
AwsException longValue = assertThrows(AwsException.class,
|
||||
() -> service.tagResource(vocabularyArn, Map.of("key-0", "v".repeat(257))));
|
||||
|
||||
assertEquals("LimitExceededException", tooMany.getErrorCode());
|
||||
assertEquals("BadRequestException", longKey.getErrorCode());
|
||||
assertEquals("BadRequestException", longValue.getErrorCode());
|
||||
assertEquals(fifty, service.listTagsForResource(vocabularyArn));
|
||||
|
||||
service.tagResource(vocabularyArn, Map.of("key-0", "changed"));
|
||||
assertEquals("changed", service.listTagsForResource(vocabularyArn).get("key-0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagKeysWithTheReservedAwsPrefixAreRejected() {
|
||||
String vocabularyArn = "arn:aws:transcribe:us-east-1:000000000000:vocabulary/reserved";
|
||||
service.createVocabulary("reserved", "en-US");
|
||||
|
||||
AwsException onTag = assertThrows(AwsException.class,
|
||||
() -> service.tagResource(vocabularyArn, Map.of("aws:team", "speech")));
|
||||
AwsException onCreateVocabulary = assertThrows(AwsException.class,
|
||||
() -> service.createVocabulary("reserved-two", "en-US", Map.of("AWS:team", "speech")));
|
||||
AwsException onStartJob = assertThrows(AwsException.class, () -> service.startTranscriptionJob(
|
||||
"reserved-job", "s3://bucket/one.wav", "en-US", "wav", Map.of("aws:team", "speech")));
|
||||
|
||||
assertEquals("BadRequestException", onTag.getErrorCode());
|
||||
assertEquals("BadRequestException", onCreateVocabulary.getErrorCode());
|
||||
assertEquals("BadRequestException", onStartJob.getErrorCode());
|
||||
assertTrue(service.listTagsForResource(vocabularyArn).isEmpty());
|
||||
assertThrows(AwsException.class, () -> service.getVocabulary("reserved-two"));
|
||||
assertThrows(AwsException.class, () -> service.getTranscriptionJob("reserved-job"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsAreReadInTheAccountAndRegionOfTheArn() {
|
||||
account.set("111111111111");
|
||||
region.set("eu-west-1");
|
||||
service.startTranscriptionJob("iam-job", "s3://bucket/one.wav", "en-US", "wav", Map.of("team", "red"));
|
||||
service.createVocabulary("iam-vocabulary", "en-US", Map.of("team", "blue"));
|
||||
|
||||
account.set(DEFAULT_ACCOUNT);
|
||||
region.set(DEFAULT_REGION);
|
||||
|
||||
assertEquals(Map.of("team", "red"),
|
||||
service.storedTags("arn:aws:transcribe:eu-west-1:111111111111:transcription-job/iam-job"));
|
||||
assertEquals(Map.of("team", "blue"),
|
||||
service.storedTags("arn:aws:transcribe:eu-west-1:111111111111:vocabulary/iam-vocabulary"));
|
||||
assertTrue(service.storedTags("arn:aws:transcribe:us-east-1:000000000000:vocabulary/iam-vocabulary")
|
||||
.isEmpty());
|
||||
assertTrue(service.storedTags("not-an-arn").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullStorageFactoryUsesAnInMemoryVocabularyStore() {
|
||||
TranscribeService serviceWithoutFactory = new TranscribeService(null,
|
||||
|
||||
Reference in new issue
Block a user