Skip to content

Commit

Permalink
feat(actions): add feature to add multiple actions to an alarm (#29)
Browse files Browse the repository at this point in the history
Users will be able to use `multipleActions(action1, action2, etc)` to add multiple actions to a single alarm.
  • Loading branch information
voho authored Feb 28, 2022
1 parent 455c746 commit 87fd2d5
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
51 changes: 50 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -35506,6 +35506,55 @@ MonitoringNamingStrategy.isAlarmFriendly(str: string)



### MultipleAlarmActionStrategy <a name="MultipleAlarmActionStrategy" id="cdk-monitoring-constructs.MultipleAlarmActionStrategy"></a>

- *Implements:* <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>

Alarm action strategy that combines multiple actions in the same order as they were given.

#### Initializers <a name="Initializers" id="cdk-monitoring-constructs.MultipleAlarmActionStrategy.Initializer"></a>

```typescript
import { MultipleAlarmActionStrategy } from 'cdk-monitoring-constructs'

new MultipleAlarmActionStrategy(actions: IAlarmActionStrategy[])
```

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code><a href="#cdk-monitoring-constructs.MultipleAlarmActionStrategy.Initializer.parameter.actions">actions</a></code> | <code><a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>[]</code> | *No description.* |

---

##### `actions`<sup>Required</sup> <a name="actions" id="cdk-monitoring-constructs.MultipleAlarmActionStrategy.Initializer.parameter.actions"></a>

- *Type:* <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>[]

---

#### Methods <a name="Methods" id="Methods"></a>

| **Name** | **Description** |
| --- | --- |
| <code><a href="#cdk-monitoring-constructs.MultipleAlarmActionStrategy.addAlarmActions">addAlarmActions</a></code> | *No description.* |

---

##### `addAlarmActions` <a name="addAlarmActions" id="cdk-monitoring-constructs.MultipleAlarmActionStrategy.addAlarmActions"></a>

```typescript
public addAlarmActions(props: AlarmActionStrategyProps): void
```

###### `props`<sup>Required</sup> <a name="props" id="cdk-monitoring-constructs.MultipleAlarmActionStrategy.addAlarmActions.parameter.props"></a>

- *Type:* <a href="#cdk-monitoring-constructs.AlarmActionStrategyProps">AlarmActionStrategyProps</a>

---




### NetworkLoadBalancerMetricFactory <a name="NetworkLoadBalancerMetricFactory" id="cdk-monitoring-constructs.NetworkLoadBalancerMetricFactory"></a>

- *Implements:* <a href="#cdk-monitoring-constructs.ILoadBalancerMetricFactory">ILoadBalancerMetricFactory</a>
Expand Down Expand Up @@ -40155,7 +40204,7 @@ public with(options: MathExpressionOptions): IMetric

### IAlarmActionStrategy <a name="IAlarmActionStrategy" id="cdk-monitoring-constructs.IAlarmActionStrategy"></a>

- *Implemented By:* <a href="#cdk-monitoring-constructs.NoopAlarmActionStrategy">NoopAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.SnsAlarmActionStrategy">SnsAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>
- *Implemented By:* <a href="#cdk-monitoring-constructs.MultipleAlarmActionStrategy">MultipleAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.NoopAlarmActionStrategy">NoopAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.SnsAlarmActionStrategy">SnsAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>

An object that appends actions to alarms.

Expand Down
23 changes: 23 additions & 0 deletions lib/common/alarm/MultipleAlarmActionStrategy.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
1 change: 1 addition & 0 deletions lib/common/alarm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
26 changes: 26 additions & 0 deletions test/common/alarm/MultipleAlarmActionStrategy.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87fd2d5

Please sign in to comment.