Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudwatch): preserve metric alarm state on definition updates (#6464)
Andre Kuraitf49376f538dd
diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.mdindex d887daded0..0bb885089b 100644--- a/docs/services/cloudwatch.md+++ b/docs/services/cloudwatch.md@@ -408,6 +408,13 @@ Composite alarms support boolean rules over alarm states. Request `AlarmTypes` caction lists are stored but not executed. Action suppression is not implemented and isrejected with `ValidationError`.+### Metric alarm updates++Updating a metric alarm preserves its current state, state reason, state timestamp and tags.+Tags supplied to `PutMetricAlarm` apply only when creating an alarm; use `TagResource` and+`UntagResource` to change existing tags. `SetAlarmState` rejects missing or unknown state+values before changing the alarm.+### SNS alarm actionsState transitions publish to same-account standard SNS topics in `AlarmActions`, `OKActions`, ordiff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.javaindex 008f5abcd2..2be55674d0 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsService.java@@ -382,13 +382,11 @@ public class CloudWatchMetricsService {if (!previous.getClass().equals(alarm.getClass())) {throw new AwsException("ValidationError", "An alarm of another type already has this name", 400);}- if (alarm instanceof CompositeAlarm) {- alarm.setStateValue(previous.getStateValue());- alarm.setStateReason(previous.getStateReason());- alarm.setStateReasonData(previous.getStateReasonData());- alarm.setStateUpdatedTimestamp(previous.getStateUpdatedTimestamp());- alarm.setTags(new HashMap<>(previous.getTags()));- }+ alarm.setStateValue(previous.getStateValue());+ alarm.setStateReason(previous.getStateReason());+ alarm.setStateReasonData(previous.getStateReasonData());+ alarm.setStateUpdatedTimestamp(previous.getStateUpdatedTimestamp());+ alarm.setTags(new HashMap<>(previous.getTags()));}if (alarm.getAlarmArn() == null) {alarm.setAlarmArn(regionResolver.buildArn("cloudwatch", region, "alarm:" + name));@@ -556,6 +554,11 @@ public class CloudWatchMetricsService {}public void setAlarmState(String alarmName, String stateValue, String stateReason, String stateReasonData, String region) {+ List<String> states = List.of("OK", "ALARM", "INSUFFICIENT_DATA");+ if (stateValue == null || !states.contains(stateValue)) {+ throw SmithyValidationErrors.validationError(stateValue == null ? SmithyValidationErrors.notNull("stateValue")+ : SmithyValidationErrors.notInEnum(stateValue, "stateValue", states));+ }List<AlarmSnsNotifier.Notification> notifications = List.of();synchronized (this) {String key = region + "::" + alarmName;diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.javaindex acc4b59eed..2fd9d942ff 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/metrics/CloudWatchMetricsServiceTest.java@@ -9,9 +9,14 @@ import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.NullSource;+import org.junit.jupiter.params.provider.ValueSource;import java.time.Instant;+import java.util.HashMap;import java.util.List;+import java.util.Map;import static org.junit.jupiter.api.Assertions.*;@@ -31,6 +36,52 @@ class CloudWatchMetricsServiceTest {);}+ @Test+ void metricDefinitionUpdatePreservesStateAndTags() {+ MetricAlarm original = new MetricAlarm();+ original.setAlarmName("updated");+ original.setTags(new HashMap<>(Map.of("team", "original")));+ service.putMetricAlarm(original, REGION);+ service.setAlarmState("updated", "ALARM", "manual", "{}", REGION);+ service.tagResource(original.getAlarmArn(), Map.of("owner", "retained"), REGION);+ service.untagResource(original.getAlarmArn(), List.of("team"), REGION);+ MetricAlarm replacement = new MetricAlarm();+ replacement.setAlarmName("updated");+ replacement.setThreshold(42.0);+ replacement.setTags(new HashMap<>(Map.of("ignored", "update")));+ service.putMetricAlarm(replacement, REGION);+ MetricAlarm result = service.describeAlarms(List.of("updated"), null, REGION).getFirst();+ assertEquals("ALARM", result.getStateValue());+ assertEquals("manual", result.getStateReason());+ assertEquals("{}", result.getStateReasonData());+ assertEquals(original.getStateUpdatedTimestamp(), result.getStateUpdatedTimestamp());+ assertEquals(42.0, result.getThreshold());+ assertEquals(Map.of("owner", "retained"), result.getTags());+ assertNotSame(original.getTags(), result.getTags());+ }++ @ParameterizedTest+ @NullSource+ @ValueSource(strings = {"", "alarm", "UNKNOWN"})+ void invalidManualStatePreservesAlarm(String value) {+ MetricAlarm alarm = new MetricAlarm();+ alarm.setAlarmName("manual");+ service.putMetricAlarm(alarm, REGION);+ service.setAlarmState("manual", "OK", "preserved", "{}", REGION);+ long timestamp = alarm.getStateUpdatedTimestamp();+ AwsException error = assertThrows(AwsException.class,+ () -> service.setAlarmState("manual", value, "rejected", "changed", REGION));+ assertEquals("ValidationError", error.getErrorCode());+ assertEquals(value == null+ ? "1 validation error detected: Value null at 'stateValue' failed to satisfy constraint: Member must not be null"+ : "1 validation error detected: Value '" + value + "' at 'stateValue' failed to satisfy constraint: "+ + "Member must satisfy enum value set: [OK, ALARM, INSUFFICIENT_DATA]", error.getMessage());+ assertEquals("OK", alarm.getStateValue());+ assertEquals("preserved", alarm.getStateReason());+ assertEquals("{}", alarm.getStateReasonData());+ assertEquals(timestamp, alarm.getStateUpdatedTimestamp());+ }+private MetricDatum datum(String name, double value) {MetricDatum d = new MetricDatum();d.setMetricName(name);