diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationStackTagsUpdateTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationStackTagsUpdateTest.java new file mode 100644 index 0000000000..1f587ceeac --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationStackTagsUpdateTest.java @@ -0,0 +1,104 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.ChangeSetType; +import software.amazon.awssdk.services.cloudformation.model.Stack; +import software.amazon.awssdk.services.cloudformation.model.Tag; + +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; + +class CloudFormationStackTagsUpdateTest { + + private static final String TEMPLATE = """ + {"Resources":{"Queue":{"Type":"AWS::SQS::Queue"}}} + """; + + private static CloudFormationClient cloudFormation; + private static String stackName; + + @BeforeAll + static void setup() { + cloudFormation = TestFixtures.cloudFormationClient(); + stackName = TestFixtures.uniqueName("cfn-stack-tags-sdk"); + } + + @AfterAll + static void cleanup() { + if (cloudFormation != null) { + try { + cloudFormation.deleteStack(request -> request.stackName(stackName)); + } finally { + cloudFormation.close(); + } + } + } + + @Test + void sdkUpdatesStackTags() throws InterruptedException { + cloudFormation.createStack(request -> request.stackName(stackName) + .templateBody(TEMPLATE) + .tags(tag("Env", "before"), tag("Owner", "original"))); + assertThat(awaitStatus("CREATE_COMPLETE")).isEqualTo("CREATE_COMPLETE"); + assertThat(tags()).containsExactlyInAnyOrderEntriesOf( + Map.of("Env", "before", "Owner", "original")); + + cloudFormation.updateStack(request -> request.stackName(stackName) + .usePreviousTemplate(true) + .tags(tag("Env", "after"), tag("Team", "new"))); + assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE"); + assertThat(tags()).containsExactlyInAnyOrderEntriesOf( + Map.of("Env", "after", "Team", "new")); + + cloudFormation.createChangeSet(request -> request.stackName(stackName) + .changeSetName("tags-only") + .changeSetType(ChangeSetType.UPDATE) + .usePreviousTemplate(true) + .tags(tag("Env", "final"))); + assertThat(tags()).containsExactlyInAnyOrderEntriesOf( + Map.of("Env", "after", "Team", "new")); + cloudFormation.executeChangeSet(request -> request.stackName(stackName) + .changeSetName("tags-only")); + assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE"); + assertThat(tags()).containsExactlyInAnyOrderEntriesOf(Map.of("Env", "final")); + + cloudFormation.updateStack(request -> request.stackName(stackName) + .usePreviousTemplate(true)); + assertThat(awaitStatus("UPDATE_COMPLETE")).isEqualTo("UPDATE_COMPLETE"); + assertThat(tags()).containsExactlyInAnyOrderEntriesOf(Map.of("Env", "final")); + } + + private static Tag tag(String key, String value) { + return Tag.builder().key(key).value(value).build(); + } + + private static Map tags() { + return cloudFormation.describeStacks(request -> request.stackName(stackName)) + .stacks().get(0).tags().stream() + .collect(Collectors.toMap(Tag::key, Tag::value)); + } + + private static String awaitStatus(String expected) throws InterruptedException { + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + while (System.nanoTime() < deadline) { + Stack stack = cloudFormation.describeStacks(request -> request.stackName(stackName)) + .stacks().get(0); + String status = stack.stackStatusAsString(); + if (expected.equals(status)) { + return status; + } + if (status.endsWith("_FAILED") || status.contains("ROLLBACK")) { + throw new AssertionError(stackName + " reached " + status + ": " + + stack.stackStatusReason()); + } + Thread.sleep(100); + } + throw new AssertionError(stackName + " did not reach " + expected + " within 30 seconds"); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java index 2a4c2c0c18..e424678c35 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationQueryHandler.java @@ -150,13 +150,14 @@ public class CloudFormationQueryHandler { Map parameters = extractParameters(params, cfnService.currentParameters(stackName, region)); List capabilities = extractList(params, "Capabilities.member."); + Map tags = extractTags(params); String changeSetName = "update-" + UUID.randomUUID().toString().substring(0, 8); ChangeSet cs = usePreviousTemplate ? cfnService.createChangeSetFromPreviousTemplate(stackName, changeSetName, - parameters, capabilities, Map.of(), region) + parameters, capabilities, tags, region) : cfnService.createChangeSet(stackName, changeSetName, - "UPDATE", templateBody, templateUrl, parameters, capabilities, Map.of(), region); + "UPDATE", templateBody, templateUrl, parameters, capabilities, tags, region); awaitExecution(cfnService.executeChangeSet(stackName, cs.getChangeSetName(), region)); Stack stack = cfnService.describeStacks(stackName, region).get(0); @@ -723,7 +724,9 @@ public class CloudFormationQueryHandler { result.put(key, value != null ? value : ""); i++; } - return result; + boolean supplied = params.keySet().stream().anyMatch(name -> + "Tags".equals(name) || "Tags.member".equals(name) || name.startsWith("Tags.member.")); + return supplied ? result : null; } private List extractList(MultivaluedMap params, String prefix) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java index 2a5f097c4d..bcfeba4334 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java @@ -441,6 +441,7 @@ public class CloudFormationService implements ResourceProvider { cs.setTemplateBody(resolvedTemplate); cs.setParameters(parameters); cs.setCapabilities(capabilities); + cs.setTags(tags); if (samTransformFailureReason != null) { cs.setStatus("FAILED"); cs.setExecutionStatus("UNAVAILABLE"); @@ -940,7 +941,7 @@ public class CloudFormationService implements ResourceProvider { Map params = cs.getParameters() != null ? cs.getParameters() : Map.of(); return submitOperation(() -> RequestScopes.runAs(accountId, region, () -> { - executeTemplate(stack, templateBody, params, isCreate, region, accountId); + executeTemplate(stack, templateBody, params, cs.getTags(), isCreate, region, accountId); String status = stack.getStatus(); cs.setExecutionStatus(status != null && (status.contains("ROLLBACK") || status.endsWith("_FAILED")) ? "EXECUTE_FAILED" : "EXECUTE_COMPLETE"); @@ -1404,6 +1405,7 @@ public class CloudFormationService implements ResourceProvider { } private void executeTemplate(Stack stack, String templateBody, Map params, + Map tags, boolean isCreate, String region, String accountId) { StackUpdateSnapshot previousState = snapshotForUpdate(stack); boolean updateCommitted = false; @@ -1681,6 +1683,9 @@ public class CloudFormationService implements ResourceProvider { if (!isCreate) { updateCommitted = true; + if (tags != null) { + stack.replaceTags(tags); + } if (hasReplacementUpdates(stack) || hasRemovedOrConditionFalseResources(stack, resources, conditions)) { stack.setStatus("UPDATE_COMPLETE_CLEANUP_IN_PROGRESS"); stack.setLastUpdatedTime(now()); @@ -2803,7 +2808,7 @@ public class CloudFormationService implements ResourceProvider { childParams.put(e.getKey(), engine.resolve(e.getValue()))); } - executeTemplate(childStack, childTemplate, childParams, childCreate, region, accountId); + executeTemplate(childStack, childTemplate, childParams, null, childCreate, region, accountId); resource.setPhysicalId(childStack.getStackId()); resource.getAttributes().put("Arn", childStack.getStackId()); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/ChangeSet.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/ChangeSet.java index 1220bb392b..75a29d95a1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/ChangeSet.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/ChangeSet.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.quarkus.runtime.annotations.RegisterForReflection; import java.time.Instant; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -20,6 +21,7 @@ public class ChangeSet { private String templateBody; private Map parameters; private List capabilities; + private Map tags; private String changeSetType; // CREATE or UPDATE private Instant creationTime = Instant.now(); @@ -43,6 +45,8 @@ public class ChangeSet { public void setParameters(Map parameters) { this.parameters = parameters; } public List getCapabilities() { return capabilities; } public void setCapabilities(List capabilities) { this.capabilities = capabilities; } + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags == null ? null : new LinkedHashMap<>(tags); } public String getChangeSetType() { return changeSetType; } public void setChangeSetType(String changeSetType) { this.changeSetType = changeSetType; } public Instant getCreationTime() { return creationTime; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java index 681b29828a..b8c2b0517a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/model/Stack.java @@ -158,6 +158,7 @@ public class Stack { public void replaceOutputs(Map source) { replace(outputs, source); } public void replaceExports(Map source) { replace(exports, source); } public void replaceOutputExportNames(Map source) { replace(outputExportNames, source); } + public void replaceTags(Map source) { replace(tags, source); } private static void replace(Map target, Map source) { synchronized (target) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUpdateStackTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUpdateStackTagsIntegrationTest.java new file mode 100644 index 0000000000..9421443ef8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationUpdateStackTagsIntegrationTest.java @@ -0,0 +1,158 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import io.github.hectorvent.floci.core.common.XmlParser; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@QuarkusTest +class CloudFormationUpdateStackTagsIntegrationTest { + + @Test + void tagsOnlyUpdatesWorkForDirectRequestsAndChangeSets() { + String stackName = "cfn-tags-only-" + UUID.randomUUID().toString().substring(0, 8); + String template = """ + {"Resources":{"Queue":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}}}} + """.formatted(stackName); + try { + withTags(request("CreateStack", stackName).formParam("TemplateBody", template), + "before", "original") + .when().post("/").then().statusCode(200); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + + withTags(request("UpdateStack", stackName).formParam("TemplateBody", template), + "after", "new") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName)); + + withTags(request("CreateChangeSet", stackName) + .formParam("TemplateBody", template) + .formParam("ChangeSetName", "tags-only") + .formParam("ChangeSetType", "UPDATE"), "final", "latest") + .when().post("/").then().statusCode(200); + assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName)); + executeChangeSet(stackName, "tags-only"); + assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName)); + + String failingTemplate = """ + {"Resources":{ + "Queue":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}}, + "ZFail":{"Type":"AWS::CloudFormation::Stack","Properties":{}} + }} + """.formatted(stackName); + withTags(request("UpdateStack", stackName).formParam("TemplateBody", failingTemplate), + "failed", "attempt") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName)); + } finally { + request("DeleteStack", stackName).when().post("/").then().statusCode(200); + CfnStackWaits.awaitStackDeleted(stackName); + } + } + + @Test + void suppliedTagsReplaceStackTagsOnlyWhenTheUpdateExecutes() { + String stackName = "cfn-update-tags-" + UUID.randomUUID().toString().substring(0, 8); + String template = """ + {"Parameters":{"Timeout":{"Type":"Number","Default":"30"}}, + "Resources":{"Queue":{"Type":"AWS::SQS::Queue","Properties":{ + "QueueName":"%s","VisibilityTimeout":{"Ref":"Timeout"}}}}} + """.formatted(stackName); + try { + withTags(request("CreateStack", stackName).formParam("TemplateBody", template), + "before", "original") + .when().post("/").then().statusCode(200); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "before", "Owner", "original"), tags(stackName)); + + withTags(updateRequest("UpdateStack", stackName, template, "60"), "after", "new") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName)); + + updateRequest("UpdateStack", stackName, template, "70") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName)); + + withTags(updateRequest("CreateChangeSet", stackName, template, "80") + .formParam("ChangeSetName", "tag-update") + .formParam("ChangeSetType", "UPDATE"), "final", "latest") + .when().post("/").then().statusCode(200); + assertEquals(Map.of("Env", "after", "Team", "new"), tags(stackName)); + executeChangeSet(stackName, "tag-update"); + assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName)); + + updateRequest("CreateChangeSet", stackName, template, "90") + .formParam("ChangeSetName", "untagged-update") + .formParam("ChangeSetType", "UPDATE") + .when().post("/").then().statusCode(200); + executeChangeSet(stackName, "untagged-update"); + assertEquals(Map.of("Env", "final", "Team", "latest"), tags(stackName)); + + updateRequest("UpdateStack", stackName, template, "100") + .formParam("Tags", "") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of(), tags(stackName)); + + withTags(updateRequest("UpdateStack", stackName, template, "110"), "reset", "again") + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + assertEquals(Map.of("Env", "reset", "Team", "again"), tags(stackName)); + + updateRequest("CreateChangeSet", stackName, template, "120") + .formParam("ChangeSetName", "clear-tags") + .formParam("ChangeSetType", "UPDATE") + .formParam("Tags.member", "") + .when().post("/").then().statusCode(200); + assertEquals(Map.of("Env", "reset", "Team", "again"), tags(stackName)); + executeChangeSet(stackName, "clear-tags"); + assertEquals(Map.of(), tags(stackName)); + } finally { + request("DeleteStack", stackName).when().post("/").then().statusCode(200); + CfnStackWaits.awaitStackDeleted(stackName); + } + } + + private static RequestSpecification request(String action, String stackName) { + return given().contentType("application/x-www-form-urlencoded") + .formParam("Action", action).formParam("StackName", stackName); + } + + private static RequestSpecification updateRequest(String action, String stackName, + String template, String timeout) { + return request(action, stackName).formParam("TemplateBody", template) + .formParam("Parameters.member.1.ParameterKey", "Timeout") + .formParam("Parameters.member.1.ParameterValue", timeout); + } + + private static RequestSpecification withTags(RequestSpecification request, + String env, String secondValue) { + String secondKey = "before".equals(env) ? "Owner" : "Team"; + return request.formParam("Tags.member.1.Key", "Env") + .formParam("Tags.member.1.Value", env) + .formParam("Tags.member.2.Key", secondKey) + .formParam("Tags.member.2.Value", secondValue); + } + + private static Map tags(String stackName) { + String xml = request("DescribeStacks", stackName).when().post("/") + .then().statusCode(200).extract().asString(); + return XmlParser.extractPairs(xml, "member", "Key", "Value"); + } + + private static void executeChangeSet(String stackName, String changeSetName) { + request("ExecuteChangeSet", stackName).formParam("ChangeSetName", changeSetName) + .when().post("/").then().statusCode(200); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status()); + } +}