From 87fd2d500a437f9d857f9db15127a1ac7ef1a12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Hord=C4=9Bj=C4=8Duk?= Date: Mon, 28 Feb 2022 16:04:24 +0100 Subject: [PATCH] feat(actions): add feature to add multiple actions to an alarm (#29) Users will be able to use `multipleActions(action1, action2, etc)` to add multiple actions to a single alarm. --- API.md | 51 ++++++++++++++++++- .../alarm/MultipleAlarmActionStrategy.ts | 23 +++++++++ lib/common/alarm/index.ts | 1 + .../alarm/MultipleAlarmActionStrategy.test.ts | 26 ++++++++++ .../MultipleAlarmActionStrategy.test.ts.snap | 40 +++++++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/common/alarm/MultipleAlarmActionStrategy.ts create mode 100644 test/common/alarm/MultipleAlarmActionStrategy.test.ts create mode 100644 test/common/alarm/__snapshots__/MultipleAlarmActionStrategy.test.ts.snap diff --git a/API.md b/API.md index c49bebdb..b7bdddb5 100644 --- a/API.md +++ b/API.md @@ -35506,6 +35506,55 @@ MonitoringNamingStrategy.isAlarmFriendly(str: string) +### MultipleAlarmActionStrategy + +- *Implements:* IAlarmActionStrategy + +Alarm action strategy that combines multiple actions in the same order as they were given. + +#### Initializers + +```typescript +import { MultipleAlarmActionStrategy } from 'cdk-monitoring-constructs' + +new MultipleAlarmActionStrategy(actions: IAlarmActionStrategy[]) +``` + +| **Name** | **Type** | **Description** | +| --- | --- | --- | +| actions | IAlarmActionStrategy[] | *No description.* | + +--- + +##### `actions`Required + +- *Type:* IAlarmActionStrategy[] + +--- + +#### Methods + +| **Name** | **Description** | +| --- | --- | +| addAlarmActions | *No description.* | + +--- + +##### `addAlarmActions` + +```typescript +public addAlarmActions(props: AlarmActionStrategyProps): void +``` + +###### `props`Required + +- *Type:* AlarmActionStrategyProps + +--- + + + + ### NetworkLoadBalancerMetricFactory - *Implements:* ILoadBalancerMetricFactory @@ -40155,7 +40204,7 @@ public with(options: MathExpressionOptions): IMetric ### IAlarmActionStrategy -- *Implemented By:* NoopAlarmActionStrategy, SnsAlarmActionStrategy, IAlarmActionStrategy +- *Implemented By:* MultipleAlarmActionStrategy, NoopAlarmActionStrategy, SnsAlarmActionStrategy, IAlarmActionStrategy An object that appends actions to alarms. diff --git a/lib/common/alarm/MultipleAlarmActionStrategy.ts b/lib/common/alarm/MultipleAlarmActionStrategy.ts new file mode 100644 index 00000000..b3a4f09a --- /dev/null +++ b/lib/common/alarm/MultipleAlarmActionStrategy.ts @@ -0,0 +1,23 @@ +import { + AlarmActionStrategyProps, + IAlarmActionStrategy, +} from "./IAlarmActionStrategy"; + +export function multipleActions(...actions: IAlarmActionStrategy[]) { + return new MultipleAlarmActionStrategy(actions); +} + +/** + * Alarm action strategy that combines multiple actions in the same order as they were given. + */ +export class MultipleAlarmActionStrategy implements IAlarmActionStrategy { + protected readonly actions: IAlarmActionStrategy[]; + + constructor(actions: IAlarmActionStrategy[]) { + this.actions = actions; + } + + addAlarmActions(props: AlarmActionStrategyProps): void { + this.actions.forEach((action) => action.addAlarmActions(props)); + } +} diff --git a/lib/common/alarm/index.ts b/lib/common/alarm/index.ts index 3aabae64..552a8fbb 100644 --- a/lib/common/alarm/index.ts +++ b/lib/common/alarm/index.ts @@ -4,5 +4,6 @@ export * from "./CustomAlarmThreshold"; export * from "./IAlarmActionStrategy"; export * from "./IAlarmAnnotationStrategy"; export * from "./IAlarmDedupeStringProcessor"; +export * from "./MultipleAlarmActionStrategy"; export * from "./NoopAlarmActionStrategy"; export * from "./SnsAlarmActionStrategy"; diff --git a/test/common/alarm/MultipleAlarmActionStrategy.test.ts b/test/common/alarm/MultipleAlarmActionStrategy.test.ts new file mode 100644 index 00000000..6ae2bfd0 --- /dev/null +++ b/test/common/alarm/MultipleAlarmActionStrategy.test.ts @@ -0,0 +1,26 @@ +import { SynthUtils } from "@monocdk-experiment/assert"; +import { Stack } from "monocdk"; +import { Alarm, Metric } from "monocdk/aws-cloudwatch"; +import { Topic } from "monocdk/aws-sns"; + +import { multipleActions, SnsAlarmActionStrategy } from "../../../lib"; + +test("snapshot test: multiple actions", () => { + const stack = new Stack(); + const topic1 = new Topic(stack, "DummyTopic1"); + const topic2 = new Topic(stack, "DummyTopic2"); + const topic3 = new Topic(stack, "DummyTopic3"); + const alarm = new Alarm(stack, "DummyAlarm", { + evaluationPeriods: 1, + threshold: 0, + metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }), + }); + const action = multipleActions( + new SnsAlarmActionStrategy({ onAlarmTopic: topic1 }), + new SnsAlarmActionStrategy({ onAlarmTopic: topic2 }), + new SnsAlarmActionStrategy({ onAlarmTopic: topic3 }) + ); + action.addAlarmActions({ alarm, action }); + + expect(SynthUtils.toCloudFormation(stack)).toMatchSnapshot(); +}); diff --git a/test/common/alarm/__snapshots__/MultipleAlarmActionStrategy.test.ts.snap b/test/common/alarm/__snapshots__/MultipleAlarmActionStrategy.test.ts.snap new file mode 100644 index 00000000..dec61273 --- /dev/null +++ b/test/common/alarm/__snapshots__/MultipleAlarmActionStrategy.test.ts.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snapshot test: multiple actions 1`] = ` +Object { + "Resources": Object { + "DummyAlarm234203A9": Object { + "Properties": Object { + "AlarmActions": Array [ + Object { + "Ref": "DummyTopic18541D2D0", + }, + Object { + "Ref": "DummyTopic2F00C887A", + }, + Object { + "Ref": "DummyTopic389AA825B", + }, + ], + "ComparisonOperator": "GreaterThanOrEqualToThreshold", + "EvaluationPeriods": 1, + "MetricName": "Dummy", + "Namespace": "Dummy", + "Period": 300, + "Statistic": "Average", + "Threshold": 0, + }, + "Type": "AWS::CloudWatch::Alarm", + }, + "DummyTopic18541D2D0": Object { + "Type": "AWS::SNS::Topic", + }, + "DummyTopic2F00C887A": Object { + "Type": "AWS::SNS::Topic", + }, + "DummyTopic389AA825B": Object { + "Type": "AWS::SNS::Topic", + }, + }, +} +`;