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

@@ -61,6 +61,14 @@ public final class CfnRollback {
*/
public static final String EVENT_INVOKE_CONFIG_SNAPSHOT_ATTR = "__FlociEventInvokeConfigSnapshot";
/**
* Holds the body and tags a dashboard carried before an in-place update changed them, or the
* fact that it did not exist, so a failed stack update can put it back. Written by
* {@code CloudWatchDashboardCfnProvisioner} before its first mutating call and spent by its
* {@code rollbackUpdate}.
*/
public static final String DASHBOARD_UPDATE_SNAPSHOT_ATTR = "__FlociDashboardUpdateSnapshot";
/**
* Holds the pipe a rename displaced: the name it still lives under, the region that addresses
* it, how many times deleting it has been attempted, and when the replacement was created.
@@ -0,0 +1,250 @@
package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.cloudwatch.dashboards.CloudWatchDashboardsService;
import io.github.hectorvent.floci.services.cloudwatch.dashboards.model.Dashboard;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Provisions {@code AWS::CloudWatch::Dashboard}. Alarms belong to {@link CloudWatchCfnProvisioner};
* this class takes only the dashboards service so the test fixture can wire it from that service.
*
* <p>The dashboard name is the physical id and {@code Ref}, generated when the template gives none
* and kept across updates. It is create-only: an update that keeps it puts the body again and
* drives the tags to the template's set, since {@code PutDashboard} leaves an existing dashboard's
* tags alone, and keeps the body and tags it replaces on the resource so a failed stack update can
* put them back. An update that changes it, or drops an explicit name for a generated one, creates
* the new dashboard and leaves the displaced one to the {@link ReplacementCleanup} record, deleted
* once the update commits or put back on rollback.
*
* <p>The schema declares no read-only properties, so there is nothing for {@code Fn::GetAtt}.
*/
@ApplicationScoped
public class CloudWatchDashboardCfnProvisioner implements CfnResourceProvisioner {
private static final String TYPE = "AWS::CloudWatch::Dashboard";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final int DASHBOARD_NAME_MAX_LENGTH = 255;
/** The registry schema's {@code maxItems} for Tags, which CloudFormation checks before the handler runs. */
private static final int MAX_TAGS = 50;
/**
* Records whether the dashboard's name came from the template or was generated, so a later
* update can tell an explicit name being dropped, which replaces the dashboard on AWS, from
* an unnamed dashboard keeping the name it was given. Read back off the stored attributes.
*/
private static final String NAME_MODE_ATTR = "FlociDashboardNameMode";
private static final String NAME_MODE_EXPLICIT = "explicit";
private static final String NAME_MODE_GENERATED = "generated";
private final CloudWatchDashboardsService dashboardsService;
@Inject
public CloudWatchDashboardCfnProvisioner(CloudWatchDashboardsService dashboardsService) {
this.dashboardsService = dashboardsService;
}
@Override
public Set<String> resourceTypes() {
return Set.of(TYPE);
}
@Override
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
// A snapshot describes the update in flight; one an earlier update left behind is stale.
r.getAttributes().remove(CfnRollback.DASHBOARD_UPDATE_SNAPSHOT_ATTR);
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
String explicitName = ctx.resolveOptional(props, "DashboardName");
boolean hasExplicitName = explicitName != null && !explicitName.isBlank();
if (hasExplicitName && explicitName.length() > DASHBOARD_NAME_MAX_LENGTH) {
throw new AwsException("ValidationError",
TYPE + " DashboardName must be at most " + DASHBOARD_NAME_MAX_LENGTH + " characters", 400);
}
// The body is a string on the wire; a template may still write it as a JSON object.
String body = props == null ? null : ctx.engine().resolveJsonAttribute(props.path("DashboardBody"));
if (body == null || body.isBlank()) {
throw new AwsException("ValidationError", TYPE + " requires DashboardBody", 400);
}
Map<String, String> tags = ctx.resolveTags(props, "Tags");
if (tags.size() > MAX_TAGS) {
throw new AwsException("ValidationError",
TYPE + " Tags must hold at most " + MAX_TAGS + " entries, got " + tags.size(), 400);
}
String name = physicalName(r, ctx, explicitName, hasExplicitName);
if (ctx.reusesPriorEntity(name)) {
snapshotBeforeUpdate(r, name, ctx.region());
}
try {
// PutDashboard creates or replaces the body in full, and applies the tags on create only.
Dashboard dashboard = dashboardsService.putDashboard(name, body, tags, ctx.region());
if (ctx.reusesPriorEntity(name)) {
reconcileTags(dashboard.getDashboardArn(), tags, ctx.region());
}
} catch (RuntimeException failure) {
unwind(r, name, failure);
throw failure;
}
r.setPhysicalId(name);
r.getAttributes().put(NAME_MODE_ATTR, hasExplicitName ? NAME_MODE_EXPLICIT : NAME_MODE_GENERATED);
ReplacementCleanup.record(r, ctx, attributesBefore);
}
/**
* A provision that changed the dashboard and then failed undoes itself. CloudFormationService
* puts the resource the stack held before the attempt back in its place, and that object never
* carried the snapshot taken here, so it marks the resource restored and the rollback walker
* skips {@link #rollbackUpdate}: without this the stack reports a completed rollback while the
* new body, or a half-reconciled set of tags, is still live. The stack must report the original
* failure, so an unwind that cannot restore is attached to it and recorded as a rollback
* failure, which ends the stack in UPDATE_ROLLBACK_FAILED rather than claiming the prior
* dashboard is intact. The unwind repeats the tag call the update just made, so it can come
* back carrying the very exception that interrupted the update; a throwable cannot be
* suppressed under itself, and the failure is recorded before the two are joined so that
* record never depends on it.
*/
private void unwind(StackResource r, String name, RuntimeException failure) {
try {
rollbackUpdate(r);
r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR, "true");
} catch (RuntimeException unwindFailure) {
r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
"Could not roll back the update of dashboard " + name + ": " + unwindFailure.getMessage());
if (unwindFailure != failure) {
failure.addSuppressed(unwindFailure);
}
}
}
/**
* The template's name when it gives one; otherwise the generated name the dashboard already
* has, unless the previous name was explicit, since dropping an explicit name is a replacement
* on AWS rather than the old name living on; and a fresh generated name failing both.
*/
private static String physicalName(StackResource r, ProvisionContext ctx, String explicitName,
boolean hasExplicitName) {
if (hasExplicitName) {
return explicitName;
}
boolean explicitNameRemoved = ctx.isUpdate()
&& NAME_MODE_EXPLICIT.equals(r.getAttributes().get(NAME_MODE_ATTR));
if (ctx.isUpdate() && !explicitNameRemoved) {
return ctx.priorPhysicalId();
}
return ctx.generatePhysicalName(r.getLogicalId(), DASHBOARD_NAME_MAX_LENGTH, false);
}
private void reconcileTags(String dashboardArn, Map<String, String> desired, String region) {
Map<String, String> current = dashboardsService.listTagsForResource(dashboardArn, region);
List<String> stale = ProvisionContext.staleTagKeys(current, desired);
if (!stale.isEmpty()) {
dashboardsService.untagResource(dashboardArn, stale, region);
}
if (!desired.isEmpty()) {
dashboardsService.tagResource(dashboardArn, desired, region);
}
}
/**
* Keeps the body and tags the dashboard has before an in-place update changes them, so
* {@link #rollbackUpdate} can put them back. A dashboard that is already gone is recorded as
* absent, so a rollback removes the one the update created in its place.
*/
private void snapshotBeforeUpdate(StackResource r, String name, String region) {
ObjectNode snapshot = MAPPER.createObjectNode();
snapshot.put("region", region);
snapshot.put("name", name);
try {
Dashboard current = dashboardsService.getDashboard(name, region);
snapshot.put("body", current.getDashboardBody());
ObjectNode tags = snapshot.putObject("tags");
current.getTags().forEach(tags::put);
} catch (AwsException e) {
if (!"ResourceNotFound".equals(e.getErrorCode())) {
throw e;
}
snapshot.put("absent", true);
}
r.getAttributes().put(CfnRollback.DASHBOARD_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
}
@Override
public void delete(String resourceType, String physicalId, String region) {
if (physicalId == null || physicalId.isBlank()) {
return;
}
// ResourceNotFound is CloudWatch's own code for a dashboard that is already gone.
CfnDeletes.safeDelete("CloudWatch dashboard", physicalId,
() -> dashboardsService.deleteDashboards(List.of(physicalId), region),
"ResourceNotFound");
}
@Override
public boolean hasReplacementUpdate(StackResource resource) {
return ReplacementCleanup.hasReplacement(resource);
}
@Override
public String updateCleanupPhysicalId(StackResource resource) {
return ReplacementCleanup.cleanupPhysicalId(resource);
}
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
return ReplacementCleanup.complete(resource, this::delete);
}
@Override
public void clearUpdate(StackResource resource) {
resource.getAttributes().remove(CfnRollback.DASHBOARD_UPDATE_SNAPSHOT_ATTR);
ReplacementCleanup.clear(resource);
}
/**
* A replacement is undone through the cleanup record and an in-place update from the snapshot
* taken before it: the body is put back and the tags driven to the set the snapshot holds.
* With neither on the resource the provision failed before it changed anything, since every
* mutation is preceded by the snapshot or followed by the record, so there is nothing to undo.
*/
@Override
public boolean rollbackUpdate(StackResource resource) {
if (ReplacementCleanup.rollback(resource, this::delete)) {
return true;
}
// The snapshot is spent only once the restore succeeded: a restore that throws leaves it in
// place for the next attempt instead of reporting a rollback that never happened.
String raw = resource.getAttributes().get(CfnRollback.DASHBOARD_UPDATE_SNAPSHOT_ATTR);
if (raw == null) {
return true;
}
JsonNode snapshot;
try {
snapshot = MAPPER.readTree(raw);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Could not read the dashboard update snapshot for "
+ resource.getLogicalId(), e);
}
String region = snapshot.path("region").asText();
String name = snapshot.path("name").asText();
if (snapshot.path("absent").asBoolean(false)) {
delete(TYPE, name, region);
} else {
Map<String, String> tags = new LinkedHashMap<>();
snapshot.path("tags").fields().forEachRemaining(tag -> tags.put(tag.getKey(), tag.getValue().asText()));
Dashboard restored = dashboardsService.putDashboard(name, snapshot.path("body").asText(), tags, region);
reconcileTags(restored.getDashboardArn(), tags, region);
}
resource.getAttributes().remove(CfnRollback.DASHBOARD_UPDATE_SNAPSHOT_ATTR);
return true;
}
}