diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java index 79cca6bdda..68c8ea4f47 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisioner.java @@ -10,6 +10,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -128,12 +129,33 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner { // Queue object, so build it here from region + accountId + queueName. Without this, // Fn::GetAtt [Queue, Arn] references resolve to an empty string. String queueArn = AwsArnUtils.Arn.of("sqs", ctx.region(), ctx.accountId(), queueName).toString(); + reconcileTags(queueUrl, ctx.resolveTags(props, "Tags"), ctx.region()); r.setPhysicalId(queueUrl); r.getAttributes().put("Arn", queueArn); r.getAttributes().put("QueueName", queueName); r.getAttributes().put("QueueUrl", queueUrl); } + /** + * Drives the queue's tags to the template's desired set, which is what CloudFormation does on + * update: a key the template drops is untagged, and a template with no {@code Tags} leaves the + * queue untagged. SQS has no replace-tags call, so the removal has to be computed, the same + * shape {@code AcmCfnProvisioner} uses. + * + *

Serves the create path too, where {@code listQueueTags} is empty and this is a plain tag. + * One path for both keeps a newly created queue and an updated one in the same state for the + * same template. + */ + private void reconcileTags(String queueUrl, Map desired, String region) { + List stale = ProvisionContext.staleTagKeys(sqsService.listQueueTags(queueUrl, region), desired); + if (!stale.isEmpty()) { + sqsService.untagQueue(queueUrl, stale, region); + } + if (!desired.isEmpty()) { + sqsService.tagQueue(queueUrl, desired, region); + } + } + private void provisionQueuePolicy(StackResource r, ProvisionContext ctx) { // A policy has no backing entity, so its id only has to stay put across updates. r.setPhysicalId(ctx.isUpdate() diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java index 8e9c4d3ca7..89132f1213 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/SqsCfnProvisionerTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -318,4 +320,63 @@ class SqsCfnProvisionerTest { provisioner.delete("AWS::SQS::QueuePolicy", "queue-policy-abc", "us-east-1"); verifyNoInteractions(sqs); } + + @Test + void queueTagsFromTheTemplateReachTheQueue() { + // The registry schema declares Tags on AWS::SQS::Queue, but the provisioner never read the + // property, so a tagged template produced an untagged queue and nothing reported it. + when(sqs.createQueue(eq("the-queue"), any(), eq("us-east-1"))) + .thenReturn(new Queue("the-queue", "http://q/the-queue")); + when(sqs.listQueueTags("http://q/the-queue", "us-east-1")).thenReturn(Map.of()); + StackResource r = resource("AWS::SQS::Queue", "MyQueue"); + ObjectNode props = mapper.createObjectNode().put("QueueName", "the-queue"); + props.putArray("Tags") + .add(mapper.createObjectNode().put("Key", "env").put("Value", "prod")) + .add(mapper.createObjectNode().put("Key", "team").put("Value", "platform")); + + provisioner.provision(r, props, ctx()); + + verify(sqs).tagQueue("http://q/the-queue", Map.of("env", "prod", "team", "platform"), + "us-east-1"); + verify(sqs, never()).untagQueue(anyString(), any(), anyString()); + } + + @Test + void updateUntagsOnlyTheKeyTheTemplateDropped() { + // CloudFormation drives tags to the template's desired state, so a dropped key is untagged. + // Calling tagQueue alone would leave "owner" on the queue forever: SQS has no replace-tags + // call, which is why this goes through staleTagKeys the way AcmCfnProvisioner does. + when(sqs.listQueueTags("http://q/orders", "us-east-1")) + .thenReturn(new HashMap<>(Map.of("env", "prod", "owner", "alice"))); + StackResource r = resource("AWS::SQS::Queue", "Orders"); + r.setAttributes(new HashMap<>(Map.of("QueueName", "orders"))); + ObjectNode props = mapper.createObjectNode().put("QueueName", "orders"); + props.putArray("Tags") + .add(mapper.createObjectNode().put("Key", "env").put("Value", "staging")); + + provisioner.provision(r, props, updateCtx("http://q/orders")); + + verify(sqs).untagQueue("http://q/orders", List.of("owner"), "us-east-1"); + verify(sqs).tagQueue("http://q/orders", Map.of("env", "staging"), "us-east-1"); + } + + @Test + void updateWithTheTagsPropertyGoneUntagsTheQueueCompletely() { + // Dropping Tags altogether is the edge the reconcile docstring promises but nothing pinned. + // It holds only because resolveTags answers an empty map rather than null: were that to + // change, every key would silently survive an update that asked for none. + when(sqs.listQueueTags("http://q/orders", "us-east-1")) + .thenReturn(new HashMap<>(Map.of("env", "prod", "owner", "alice"))); + StackResource r = resource("AWS::SQS::Queue", "Orders"); + r.setAttributes(new HashMap<>(Map.of("QueueName", "orders"))); + ObjectNode props = mapper.createObjectNode().put("QueueName", "orders"); + + provisioner.provision(r, props, updateCtx("http://q/orders")); + + // Order is not part of the contract: staleTagKeys walks a HashMap's key set. + verify(sqs).untagQueue(eq("http://q/orders"), + argThat(keys -> keys.size() == 2 && keys.containsAll(List.of("env", "owner"))), + eq("us-east-1")); + verify(sqs, never()).tagQueue(anyString(), any(), anyString()); + } }