From aceeeca0af17a617d36ce2eebe779dddddc81a2e Mon Sep 17 00:00:00 2001 From: iam-ssrivastav Date: Sun, 31 May 2026 02:41:37 +0530 Subject: [PATCH] feat(firehose): add TagDeliveryStream, UntagDeliveryStream, and ListTagsForDeliveryStream (#1061) * feat(firehose): add tag, untag, and list tags endpoints to delivery stream * test(lambda): resolve flaky port bind error in RuntimeApiServerTest * test(ec2): isolate Ec2NetworkInterfacePaginationTest by filtering by subnet-id * fix: resolve flaky integration tests and robustify runtime binding & codebuild diagnostics * fix: unregister websocket connection immediately on close to prevent postToConnection race conditions * fix(lambda): fix RuntimeApiServer retry race condition when stopping during retries --- .../firehose/FirehoseJsonHandler.java | 56 ++++++++++++++++++- .../services/firehose/FirehoseService.java | 54 ++++++++++++++++++ .../model/DeliveryStreamDescription.java | 27 +++++++++ .../firehose/FirehoseIntegrationTest.java | 49 +++++++++++++++- 4 files changed, 184 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseJsonHandler.java index 1702278470..dc2eaa8fb8 100644 --- a/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseJsonHandler.java @@ -36,7 +36,13 @@ public class FirehoseJsonHandler { } else if (request.has("ExtendedS3DestinationConfiguration")) { s3 = mapper.treeToValue(request.get("ExtendedS3DestinationConfiguration"), S3Destination.class); } - String arn = firehoseService.createDeliveryStream(name, s3); + List tags = new ArrayList<>(); + if (request.has("Tags")) { + for (JsonNode tNode : request.get("Tags")) { + tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class)); + } + } + String arn = firehoseService.createDeliveryStream(name, s3, tags); yield Response.ok(Map.of("DeliveryStreamARN", arn)).build(); } case "DescribeDeliveryStream" -> { @@ -72,6 +78,54 @@ public class FirehoseJsonHandler { .toList(); yield Response.ok(Map.of("FailedPutCount", 0, "RequestResponses", responses)).build(); } + case "TagDeliveryStream" -> { + String name = request.get("DeliveryStreamName").asText(); + List tags = new ArrayList<>(); + if (request.has("Tags")) { + for (JsonNode tNode : request.get("Tags")) { + tags.add(mapper.treeToValue(tNode, io.github.hectorvent.floci.services.firehose.model.DeliveryStreamDescription.Tag.class)); + } + } + firehoseService.tagDeliveryStream(name, tags); + yield Response.ok(Map.of()).build(); + } + case "UntagDeliveryStream" -> { + String name = request.get("DeliveryStreamName").asText(); + List keys = new ArrayList<>(); + if (request.has("TagKeys")) { + for (JsonNode kNode : request.get("TagKeys")) { + keys.add(kNode.asText()); + } + } + firehoseService.untagDeliveryStream(name, keys); + yield Response.ok(Map.of()).build(); + } + case "ListTagsForDeliveryStream" -> { + String name = request.get("DeliveryStreamName").asText(); + String exclusiveStartTagKey = request.has("ExclusiveStartTagKey") ? request.get("ExclusiveStartTagKey").asText() : null; + Integer limit = request.has("Limit") ? request.get("Limit").asInt() : null; + + List tags = firehoseService.listTagsForDeliveryStream(name, exclusiveStartTagKey, limit); + boolean hasMore = false; + var allTags = firehoseService.describeDeliveryStream(name).getTags(); + if (!tags.isEmpty() && !allTags.isEmpty()) { + String lastKey = tags.get(tags.size() - 1).getKey(); + int idx = -1; + for (int i = 0; i < allTags.size(); i++) { + if (allTags.get(i).getKey().equals(lastKey)) { + idx = i; + break; + } + } + if (idx >= 0 && idx < allTags.size() - 1) { + hasMore = true; + } + } + yield Response.ok(Map.of( + "Tags", tags, + "HasMoreTags", hasMore + )).build(); + } default -> throw new AwsException("InvalidAction", "Action " + action + " is not supported", 400); }; } diff --git a/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseService.java b/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseService.java index 32d2bae73f..f8bcbe5474 100644 --- a/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseService.java +++ b/src/main/java/io/github/hectorvent/floci/services/firehose/FirehoseService.java @@ -41,15 +41,69 @@ public class FirehoseService { } public String createDeliveryStream(String name, S3Destination s3Config) { + return createDeliveryStream(name, s3Config, List.of()); + } + + public String createDeliveryStream(String name, S3Destination s3Config, List tags) { String arn = AwsArnUtils.Arn.of("firehose", regionResolver.getDefaultRegion(), regionResolver.getAccountId(), "deliverystream/" + name).toString(); DeliveryStreamDescription description = new DeliveryStreamDescription(name, arn, s3Config); description.setAccountId(regionResolver.getAccountId()); + description.setTags(tags); streamStore.put(name, description); buffers.put(name, Collections.synchronizedList(new ArrayList<>())); LOG.infov("Created Firehose delivery stream: {0}", name); return arn; } + public void tagDeliveryStream(String name, List tagsToTag) { + DeliveryStreamDescription stream = describeDeliveryStream(name); + Map tagMap = new LinkedHashMap<>(); + for (DeliveryStreamDescription.Tag t : stream.getTags()) { + tagMap.put(t.getKey(), t.getValue()); + } + for (DeliveryStreamDescription.Tag t : tagsToTag) { + tagMap.put(t.getKey(), t.getValue()); + } + List newTags = new ArrayList<>(); + tagMap.forEach((k, v) -> newTags.add(new DeliveryStreamDescription.Tag(k, v))); + stream.setTags(newTags); + streamStore.put(name, stream); + LOG.infov("Tagged Firehose delivery stream {0}: {1}", name, tagsToTag); + } + + public void untagDeliveryStream(String name, List tagKeys) { + DeliveryStreamDescription stream = describeDeliveryStream(name); + List newTags = new ArrayList<>(); + for (DeliveryStreamDescription.Tag t : stream.getTags()) { + if (!tagKeys.contains(t.getKey())) { + newTags.add(t); + } + } + stream.setTags(newTags); + streamStore.put(name, stream); + LOG.infov("Untagged Firehose delivery stream {0}: {1}", name, tagKeys); + } + + public List listTagsForDeliveryStream(String name, String exclusiveStartTagKey, Integer limit) { + DeliveryStreamDescription stream = describeDeliveryStream(name); + List tags = stream.getTags(); + int startIndex = 0; + if (exclusiveStartTagKey != null && !exclusiveStartTagKey.isEmpty()) { + for (int i = 0; i < tags.size(); i++) { + if (tags.get(i).getKey().equals(exclusiveStartTagKey)) { + startIndex = i + 1; + break; + } + } + } + int size = tags.size() - startIndex; + int end = tags.size(); + if (limit != null && limit > 0 && limit < size) { + end = startIndex + limit; + } + return new ArrayList<>(tags.subList(startIndex, end)); + } + public DeliveryStreamDescription describeDeliveryStream(String name) { return streamStore.get(name) .orElseThrow(() -> new AwsException("ResourceNotFoundException", diff --git a/src/main/java/io/github/hectorvent/floci/services/firehose/model/DeliveryStreamDescription.java b/src/main/java/io/github/hectorvent/floci/services/firehose/model/DeliveryStreamDescription.java index 8a4a2bce58..4aeff99921 100644 --- a/src/main/java/io/github/hectorvent/floci/services/firehose/model/DeliveryStreamDescription.java +++ b/src/main/java/io/github/hectorvent/floci/services/firehose/model/DeliveryStreamDescription.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.quarkus.runtime.annotations.RegisterForReflection; import java.time.Instant; import java.util.List; +import java.util.ArrayList; @RegisterForReflection public class DeliveryStreamDescription { @@ -20,6 +21,8 @@ public class DeliveryStreamDescription { private Instant createTimestamp; @JsonProperty("Destinations") private List destinations; + @JsonProperty("Tags") + private List tags = new ArrayList<>(); public DeliveryStreamDescription() {} public DeliveryStreamDescription(String name, String arn, S3Destination s3) { @@ -99,4 +102,28 @@ public class DeliveryStreamDescription { public int getIntervalInSeconds() { return intervalInSeconds; } public void setIntervalInSeconds(int intervalInSeconds) { this.intervalInSeconds = intervalInSeconds; } } + + public List getTags() { + if (tags == null) tags = new ArrayList<>(); + return tags; + } + public void setTags(List tags) { this.tags = tags; } + + @RegisterForReflection + public static class Tag { + @JsonProperty("Key") + private String key; + @JsonProperty("Value") + private String value; + + public Tag() {} + public Tag(String key, String value) { + this.key = key; + this.value = value; + } + public String getKey() { return key; } + public void setKey(String key) { this.key = key; } + public String getValue() { return value; } + public void setValue(String value) { this.value = value; } + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/firehose/FirehoseIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/firehose/FirehoseIntegrationTest.java index ae53d18230..70e1d1f24e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/firehose/FirehoseIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/firehose/FirehoseIntegrationTest.java @@ -52,6 +52,53 @@ class FirehoseIntegrationTest { @Test @Order(3) + void tagAndUntagDeliveryStream() { + given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "Firehose_20150804.TagDeliveryStream") + .body("{ \"DeliveryStreamName\": \"" + STREAM_NAME + "\", \"Tags\": [ { \"Key\": \"env\", \"Value\": \"prod\" }, { \"Key\": \"owner\", \"Value\": \"team-a\" } ] }") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "Firehose_20150804.ListTagsForDeliveryStream") + .body("{ \"DeliveryStreamName\": \"" + STREAM_NAME + "\" }") + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags", hasSize(2)) + .body("Tags.find { it.Key == 'env' }.Value", equalTo("prod")) + .body("Tags.find { it.Key == 'owner' }.Value", equalTo("team-a")) + .body("HasMoreTags", equalTo(false)); + + given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "Firehose_20150804.UntagDeliveryStream") + .body("{ \"DeliveryStreamName\": \"" + STREAM_NAME + "\", \"TagKeys\": [ \"env\" ] }") + .when() + .post("/") + .then() + .statusCode(200); + + given() + .contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "Firehose_20150804.ListTagsForDeliveryStream") + .body("{ \"DeliveryStreamName\": \"" + STREAM_NAME + "\" }") + .when() + .post("/") + .then() + .statusCode(200) + .body("Tags", hasSize(1)) + .body("Tags[0].Key", equalTo("owner")) + .body("Tags[0].Value", equalTo("team-a")); + } + + @Test + @Order(4) void deleteDeliveryStream() { given() .contentType("application/x-amz-json-1.1") @@ -64,7 +111,7 @@ class FirehoseIntegrationTest { } @Test - @Order(4) + @Order(5) void describeDeletedDeliveryStreamReturnsNotFound() { given() .contentType("application/x-amz-json-1.1")