feat(cloudwatch): provision AWS::CloudWatch::Dashboard (#3231)

* feat(cloudwatch): provision AWS::CloudWatch::Dashboard

The dashboard name is the physical id, generated when the template gives none
and kept across updates. An update with the same name puts the body again and
drives the tags to the template's set, since PutDashboard leaves an existing
dashboard's tags alone. A changed name creates the new dashboard and leaves
the displaced one to the ReplacementCleanup record, which deletes it once the
update commits or restores it on rollback. The body may be written as a JSON
object in the template and is stored as the string the API takes.

The CloudWatch docs now list the dashboard and tag actions the handlers
already serve.

* fix(cloudwatch): roll back in-place dashboard updates and replace a dashboard whose explicit name is dropped

An in-place update keeps the body and tags it replaces on the resource, or
the fact that the dashboard did not exist, and a failed stack update puts
them back in full. The name mode is recorded as the Logs provisioner does, so
a template that drops an explicit DashboardName gets a generated name and a
replacement, as on AWS, while an unnamed dashboard keeps its generated name.
The Tags list is checked against the schema's cap of 50 on every provision,
since the tag operations an update reconciles through do not enforce it. The
integration test reads stack events with XmlParser.

* fix(cloudwatch): keep the dashboard rollback snapshot until the restore succeeds

A restore that throws left no snapshot behind, so the next rollback attempt
reported success without restoring anything. The snapshot is now spent only
after the body and tags are back.

* fix(cloudwatch): unwind a dashboard update that failed after it changed the body

An in-place update puts the body and then reconciles the tags. When the tag
calls failed, the exception left the dashboard carrying the new body while
CloudFormationService put the resource the stack held before the attempt back
in its place. That object never carried the snapshot taken by the attempt, so
the walker marked it restored and skipped rollbackUpdate: the stack reported a
completed rollback over a dashboard that still had the update's body, or a half
reconciled set of tags.

The mutating calls now unwind themselves through rollbackUpdate, the way
IotDomainConfigurationCfnProvisioner unwinds a replacement, and report the
resource restored. An unwind that cannot put the body back keeps the snapshot
and records the failure on the resource, so the stack ends in
UPDATE_ROLLBACK_FAILED rather than claiming the prior dashboard is intact, and
the original failure is still what the stack reports.

Tests: anInPlaceUpdateThatFailedOnItsTagsPutsTheBodyBack,
anUnwoundInPlaceUpdateIsMarkedRestored,
anUnwindThatCannotRestoreIsReportedAsARollbackFailure.

* fix(cloudwatch): record a dashboard unwind failure before joining it to the update failure

The unwind repeats the tag call the update just made, so a service that
keeps failing hands the unwind the very exception that interrupted the
update. Throwable.addSuppressed rejects a throwable suppressed under
itself, and that IllegalArgumentException escaped the unwind before the
rollback failure was recorded: the dispatcher reported "Self-suppression
not permitted" as the resource failure, no failure attribute reached the
stored resource, and the stack ended UPDATE_ROLLBACK_COMPLETE over a
dashboard whose tags were never put back.

The failure attribute is now written first and the two exceptions are
joined only when they differ, as PipesCfnProvisioner does. Two stack-level
tests drive the whole path through CloudFormationService with a spied
CloudWatchDashboardsService, the way CloudFormationPipesCleanupIntegrationTest
does: a one-off tag failure ends in UPDATE_ROLLBACK_COMPLETE with the prior
body and tags live, a persistent one in UPDATE_ROLLBACK_FAILED naming the
dashboard and carrying the unwind's reason.

Tests: anUnwindThatFailsWithTheUpdatesOwnExceptionIsStillRecorded,
anUpdateThatFailedOnItsTagsPutsTheDashboardBackAndTheStackRollsBackCleanly,
aFailedRestoreLeavesTheStackInUpdateRollbackFailedNamingTheDashboard.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-09-09 22:37:45 -05:00
1 parent 6eca8540a8
commit 460aa0718e
9 files changed
+1464 -1

No files matched your search

@@ -0,0 +1,208 @@
package com.floci.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
import software.amazon.awssdk.services.cloudformation.model.Output;
import software.amazon.awssdk.services.cloudformation.model.Parameter;
import software.amazon.awssdk.services.cloudformation.model.Stack;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.GetDashboardResponse;
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
import software.amazon.awssdk.services.cloudwatch.model.Tag;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Drives an {@code AWS::CloudWatch::Dashboard} through the CloudFormation SDK and reads it back
* through the CloudWatch SDK: {@code Ref} is the name, a body or tag change updates in place, a
* name change replaces the dashboard and removes the displaced one, and the stack delete removes it.
*/
@DisplayName("CloudFormation AWS::CloudWatch::Dashboard")
class CloudFormationCloudWatchDashboardTest {
private static CloudFormationClient cloudFormation;
private static CloudWatchClient cloudWatch;
private static String stackName;
private static String dashboardName;
@BeforeAll
static void setup() {
cloudFormation = TestFixtures.cloudFormationClient();
cloudWatch = TestFixtures.cloudWatchClient();
stackName = TestFixtures.uniqueName("compat-cfn-dashboard");
dashboardName = TestFixtures.uniqueName("compat-dashboard");
}
@AfterAll
static void cleanup() {
if (cloudFormation != null) {
try {
cloudFormation.deleteStack(r -> r.stackName(stackName));
waitForDeleted(stackName, 60);
} catch (Exception e) {
System.err.println("CloudFormation dashboard cleanup skipped: " + e.getMessage());
}
cloudFormation.close();
}
if (cloudWatch != null) {
cloudWatch.close();
}
}
@Test
void dashboardFollowsTheTemplateThroughCreateUpdateReplacementAndDelete() throws InterruptedException {
cloudFormation.createStack(r -> r
.stackName(stackName)
.templateBody(template())
.parameters(parameters(dashboardName, "Requests", "platform")));
assertThat(waitForTerminal(stackName, 60)).isEqualTo("CREATE_COMPLETE");
assertThat(output("DashboardRef")).as("Ref is the dashboard name").isEqualTo(dashboardName);
GetDashboardResponse created = get(dashboardName);
assertThat(created.dashboardBody())
.isEqualTo("{\"widgets\":[{\"type\":\"text\",\"properties\":{\"markdown\":\"Requests\"}}]}");
assertThat(created.dashboardArn()).endsWith(":dashboard/" + dashboardName);
assertThat(tags(created.dashboardArn())).containsExactlyEntriesOf(Map.of("team", "platform"));
cloudFormation.updateStack(r -> r
.stackName(stackName)
.templateBody(template())
.parameters(parameters(dashboardName, "Errors", "data")));
assertThat(waitForTerminal(stackName, 60)).isEqualTo("UPDATE_COMPLETE");
assertThat(output("DashboardRef")).as("a body or tag change updates in place").isEqualTo(dashboardName);
assertThat(get(dashboardName).dashboardBody()).contains("\"Errors\"");
assertThat(tags(created.dashboardArn())).containsExactlyEntriesOf(Map.of("team", "data"));
String renamed = dashboardName + "-v2";
cloudFormation.updateStack(r -> r
.stackName(stackName)
.templateBody(template())
.parameters(parameters(renamed, "Errors", "data")));
assertThat(waitForTerminal(stackName, 60)).isEqualTo("UPDATE_COMPLETE");
assertThat(output("DashboardRef")).as("a name change replaces the dashboard").isEqualTo(renamed);
assertThat(get(renamed).dashboardBody()).contains("\"Errors\"");
assertDashboardMissing(dashboardName, "the displaced dashboard is deleted once the update commits");
cloudFormation.deleteStack(r -> r.stackName(stackName));
waitForDeleted(stackName, 60);
assertDashboardMissing(renamed, "the stack delete removes the dashboard");
}
/**
* GetDashboard answers ResourceNotFound with HTTP 404 for a dashboard that does not exist. The
* code is asserted rather than the exception class: the SDK resolves the class from the JSON
* error type, and the code is what both the Query and JSON wire forms carry.
*/
private static void assertDashboardMissing(String name, String because) {
assertThatThrownBy(() -> get(name))
.as(because)
.isInstanceOfSatisfying(CloudWatchException.class, e -> {
assertThat(e.statusCode()).isEqualTo(404);
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("ResourceNotFound");
});
}
private static GetDashboardResponse get(String name) {
return cloudWatch.getDashboard(r -> r.dashboardName(name));
}
private static Map<String, String> tags(String arn) {
return cloudWatch.listTagsForResource(r -> r.resourceARN(arn)).tags().stream()
.collect(Collectors.toMap(Tag::key, Tag::value));
}
private static String template() {
return """
{
"Parameters": {
"Name": {"Type": "String"},
"Title": {"Type": "String"},
"Team": {"Type": "String"}
},
"Resources": {
"Dashboard": {
"Type": "AWS::CloudWatch::Dashboard",
"Properties": {
"DashboardName": {"Ref": "Name"},
"DashboardBody": {"Fn::Join": ["", [
"{\\"widgets\\":[{\\"type\\":\\"text\\",\\"properties\\":{\\"markdown\\":\\"",
{"Ref": "Title"},
"\\"}}]}"
]]},
"Tags": [{"Key": "team", "Value": {"Ref": "Team"}}]
}
}
},
"Outputs": {
"DashboardRef": {"Value": {"Ref": "Dashboard"}}
}
}
""";
}
private static List<Parameter> parameters(String name, String title, String team) {
return List.of(
Parameter.builder().parameterKey("Name").parameterValue(name).build(),
Parameter.builder().parameterKey("Title").parameterValue(title).build(),
Parameter.builder().parameterKey("Team").parameterValue(team).build());
}
private static String output(String key) {
Stack stack = cloudFormation.describeStacks(
DescribeStacksRequest.builder().stackName(stackName).build()).stacks().get(0);
return stack.outputs().stream()
.filter(o -> key.equals(o.outputKey()))
.map(Output::outputValue)
.findFirst()
.orElseThrow(() -> new AssertionError("stack " + stackName + " has no output " + key));
}
private static String waitForTerminal(String name, int maxSeconds) throws InterruptedException {
long deadline = System.currentTimeMillis() + maxSeconds * 1000L;
while (System.currentTimeMillis() < deadline) {
List<Stack> stacks = cloudFormation.describeStacks(
DescribeStacksRequest.builder().stackName(name).build()).stacks();
if (!stacks.isEmpty()) {
String status = stacks.get(0).stackStatusAsString();
if (!status.endsWith("_IN_PROGRESS")) {
return status;
}
}
Thread.sleep(500);
}
throw new AssertionError(
"Stack " + name + " did not reach a terminal state within " + maxSeconds + "s");
}
private static void waitForDeleted(String name, int maxSeconds) throws InterruptedException {
long deadline = System.currentTimeMillis() + maxSeconds * 1000L;
while (System.currentTimeMillis() < deadline) {
try {
List<Stack> stacks = cloudFormation.describeStacks(
DescribeStacksRequest.builder().stackName(name).build()).stacks();
if (stacks.isEmpty()
|| "DELETE_COMPLETE".equals(stacks.get(0).stackStatusAsString())) {
return;
}
} catch (CloudFormationException e) {
if (e.getMessage() != null && e.getMessage().contains("does not exist")) {
return;
}
throw e;
}
Thread.sleep(500);
}
throw new AssertionError(
"Stack " + name + " was not deleted within " + maxSeconds + "s");
}
}