diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/SqsCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/SqsCfnProvisioner.java index 57d032608f..1aa9a76bf4 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/SqsCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/SqsCfnProvisioner.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsArnUtils; -import io.floci.aws.core.common.AwsException; import io.floci.aws.services.cloudformation.model.StackResource; import io.floci.aws.services.sqs.SqsService; import io.floci.aws.services.sqs.model.Queue; @@ -140,12 +139,21 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner { // The physical id is the queue URL, so ctx.stablePhysicalName does not fit: the prior name // comes from the QueueName attribute recorded at create time, as SnsCfnProvisioner reads // the topic name beside its ARN. Keeping it means an unnamed queue survives an update - // instead of being orphaned with its messages. FifoQueue is createOnly like QueueName, so a - // prior name whose .fifo suffix no longer matches the flag is a replacing update and gets a - // fresh name. + // instead of being orphaned with its messages. The engine's change decides whether the + // update replaces the queue, as it does for the change set's Replacement: QueueName and + // FifoQueue are create-only in the registry. A replacing update of an unnamed queue gets a + // fresh generated name; a named one cannot be replaced under the same name. String priorName = r.getAttributes() != null ? r.getAttributes().get("QueueName") : null; + // SQS cannot change a queue's mode in place and a FIFO queue's name always ends in .fifo, so + // the prior name tells the live mode; the engine reads an added FifoQueue as no change, which + // would apply the update in place and leave the mode as it was. + boolean replaces = ctx.isUpdate() && priorName != null && !priorName.isBlank() + && (ctx.changes("AWS::SQS::Queue", props).requiresReplacement() || priorName.endsWith(".fifo") != fifo); + if (replaces && queueName != null && queueName.equals(priorName)) { + throw CfnExceptions.customNamedReplacement(queueName); + } if (queueName == null || queueName.isBlank()) { - if (priorName != null && !priorName.isBlank() && priorName.endsWith(".fifo") == fifo) { + if (priorName != null && !priorName.isBlank() && !replaces) { queueName = priorName; } else { // Like real CloudFormation, generated names of FIFO queues must end in .fifo @@ -183,15 +191,6 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner { // registry schema's update handler, rather than a second create. The prior physical id is // the queue URL SetQueueAttributes addresses. A replacing update derives a different name // and still creates. - // FifoQueue is createOnly and encoded in the name. An explicitly named queue keeps its name - // across updates, so a changed FifoQueue would need a replacement under the same name, - // which CloudFormation refuses for a custom-named resource: the SQS registry schema says a - // named queue cannot take an update that requires replacement. Fail it, as the other - // provisioners do, instead of reconciling everything but the mode. - if (ctx.isUpdate() && queueName.equals(priorName) && priorName.endsWith(".fifo") != fifo) { - throw new AwsException("ValidationError", - "Updating FifoQueue requires resource replacement, which is not supported.", 400); - } String queueUrl; if (ctx.isUpdate() && queueName.equals(priorName)) { attrs.remove("FifoQueue"); diff --git a/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java index 91c6d72d98..d73b58ee25 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/CfnReplacementIntegrationTest.java @@ -103,6 +103,16 @@ class CfnReplacementIntegrationTest { CLUSTER + "\"CacheNodeType\": \"cache.t3.micro\", \"CacheSubnetGroupName\": {\"Ref\": \"SubnetGroup\"}", "True", true, "repl-sng-b"); + private static final String SQS_TYPE = "AWS::SQS::Queue"; + private static final String QUEUE_NAME_IF = + "{\"Fn::If\": [\"Named\", \"repl-q-if\", {\"Ref\": \"AWS::NoValue\"}]}"; + + /** A queue named by {@code Fn::If} on {@code Named}, which is {@code Fn::Equals} of {@code equalsArgs}. */ + private static String namedByCondition(String equalsArgs) { + return subjectStack("", "", SQS_TYPE, "\"QueueName\": " + QUEUE_NAME_IF).replace("\"Resources\"", + "\"Conditions\": {\"Named\": {\"Fn::Equals\": " + equalsArgs + "}}, \"Resources\""); + } + static Stream rows() { String app = appConfigApplication("replacement-orders"); return Stream.of( @@ -134,6 +144,12 @@ class CfnReplacementIntegrationTest { + " \"HostedZoneConfig\": {\"Comment\": \"second\"}", "False", false, null), SUBNET_GROUP_REPLACED, + createOnlyRow("SQS queue explicit QueueName removed", "", "", SQS_TYPE, + "\"QueueName\": \"repl-q-removed\"", "\"VisibilityTimeout\": 30", "True", false, null), + new Row("SQS queue QueueName dropped by Fn::If choosing AWS::NoValue", + namedByCondition("[\"x\", \"x\"]"), + namedByCondition("[\"x\", \"y\"]"), + "Subject", "True", true, null), new Row("deployed parameter renamed, same value", locationStack(stringParameter("NameA", HOSTED), "", REF_A, "d"), locationStack(stringParameter("NameB", HOSTED), "", REF_B, "e"),