Skip to content

Commit dc88a4c

Browse files
author
Eugene Cheung
authored
feat(MultipleAlarmActionStrategy): add method to get flattened actions (#423)
--- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 0d79e94 commit dc88a4c

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

API.md

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/common/alarm/action/MultipleAlarmActionStrategy.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ export function multipleActions(...actions: IAlarmActionStrategy[]) {
77
return new MultipleAlarmActionStrategy(actions);
88
}
99

10+
export function isMultipleAlarmActionStrategy(
11+
obj?: any
12+
): obj is MultipleAlarmActionStrategy {
13+
return !!(obj && obj instanceof MultipleAlarmActionStrategy);
14+
}
15+
1016
/**
1117
* Alarm action strategy that combines multiple actions in the same order as they were given.
1218
*/
@@ -20,4 +26,26 @@ export class MultipleAlarmActionStrategy implements IAlarmActionStrategy {
2026
addAlarmActions(props: AlarmActionStrategyProps): void {
2127
this.actions.forEach((action) => action.addAlarmActions(props));
2228
}
29+
30+
/**
31+
* Returns list of alarm actions where any nested instances of MultipleAlarmActionStrategy
32+
* are flattened.
33+
*
34+
* @returns flattened list of alarm actions.
35+
*/
36+
flattenedAlarmActions(): IAlarmActionStrategy[] {
37+
return this._flattenedAlarmActions(...this.actions);
38+
}
39+
40+
private _flattenedAlarmActions(
41+
...actions: IAlarmActionStrategy[]
42+
): IAlarmActionStrategy[] {
43+
return actions.flatMap((action) => {
44+
if (isMultipleAlarmActionStrategy(action)) {
45+
return this._flattenedAlarmActions(action);
46+
}
47+
48+
return [action];
49+
});
50+
}
2351
}

test/common/alarm/action/MultipleAlarmActionStrategy.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { Template } from "aws-cdk-lib/assertions";
33
import { Alarm, Metric } from "aws-cdk-lib/aws-cloudwatch";
44
import { Topic } from "aws-cdk-lib/aws-sns";
55

6-
import { multipleActions, SnsAlarmActionStrategy } from "../../../../lib";
6+
import {
7+
isMultipleAlarmActionStrategy,
8+
multipleActions,
9+
SnsAlarmActionStrategy,
10+
} from "../../../../lib";
711

812
test("snapshot test: multiple actions", () => {
913
const stack = new Stack();
@@ -15,12 +19,24 @@ test("snapshot test: multiple actions", () => {
1519
threshold: 0,
1620
metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }),
1721
});
18-
const action = multipleActions(
19-
new SnsAlarmActionStrategy({ onAlarmTopic: topic1 }),
20-
new SnsAlarmActionStrategy({ onAlarmTopic: topic2 }),
21-
new SnsAlarmActionStrategy({ onAlarmTopic: topic3 })
22-
);
22+
23+
const action1 = new SnsAlarmActionStrategy({ onAlarmTopic: topic1 });
24+
const action2 = new SnsAlarmActionStrategy({ onAlarmTopic: topic2 });
25+
const action3 = new SnsAlarmActionStrategy({ onAlarmTopic: topic3 });
26+
27+
const action = multipleActions(action1, action2, action3);
2328
action.addAlarmActions({ alarm, action });
2429

2530
expect(Template.fromStack(stack)).toMatchSnapshot();
31+
expect(action.flattenedAlarmActions()).toEqual([action1, action2, action3]);
32+
});
33+
34+
test("isMultipleAlarmActionStrategy", () => {
35+
const stack = new Stack();
36+
const topic1 = new Topic(stack, "DummyTopic1");
37+
const snsAction = new SnsAlarmActionStrategy({ onAlarmTopic: topic1 });
38+
const multipleAction = multipleActions(snsAction);
39+
40+
expect(isMultipleAlarmActionStrategy(multipleAction)).toBe(true);
41+
expect(isMultipleAlarmActionStrategy(snsAction)).toBe(false);
2642
});

0 commit comments

Comments
 (0)