Skip to content

Commit 80cbc3b

Browse files
committed
feat(aws-cloudwatch): CompositeAlarm
1 parent 3ed0c0e commit 80cbc3b

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { IResource, Resource } from '@aws-cdk/core';
2+
import { IAlarmAction } from './alarm-action';
3+
4+
/**
5+
* Interface for Alarm Rule.
6+
*/
7+
export interface IAlarmRule {
8+
9+
/**
10+
* serialized representation of Alarm Rule to be used when building the Composite Alarm resource.
11+
*/
12+
renderAlarmRule(): string;
13+
14+
}
15+
16+
/**
17+
* Represents a CloudWatch Alarm
18+
*/
19+
export interface IAlarm extends IAlarmRule, IResource {
20+
/**
21+
* Alarm ARN (i.e. arn:aws:cloudwatch:<region>:<account-id>:alarm:Foo)
22+
*
23+
* @attribute
24+
*/
25+
readonly alarmArn: string;
26+
27+
/**
28+
* Name of the alarm
29+
*
30+
* @attribute
31+
*/
32+
readonly alarmName: string;
33+
}
34+
35+
/**
36+
* The base class for Alarm and CompositeAlarm resources.
37+
*/
38+
export abstract class AlarmBase extends Resource implements IAlarm {
39+
40+
/**
41+
* @attribute
42+
*/
43+
public abstract readonly alarmArn: string;
44+
public abstract readonly alarmName: string;
45+
46+
protected alarmActionArns?: string[];
47+
protected insufficientDataActionArns?: string[];
48+
protected okActionArns?: string[];
49+
50+
/**
51+
* AlarmRule indicating ALARM state for Alarm.
52+
*/
53+
public renderAlarmRule(): string {
54+
return `ALARM(${this.alarmArn})`;
55+
}
56+
57+
/**
58+
* Trigger this action if the alarm fires
59+
*
60+
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
61+
*/
62+
public addAlarmAction(...actions: IAlarmAction[]) {
63+
if (this.alarmActionArns === undefined) {
64+
this.alarmActionArns = [];
65+
}
66+
67+
this.alarmActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn));
68+
}
69+
70+
/**
71+
* Trigger this action if there is insufficient data to evaluate the alarm
72+
*
73+
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
74+
*/
75+
public addInsufficientDataAction(...actions: IAlarmAction[]) {
76+
if (this.insufficientDataActionArns === undefined) {
77+
this.insufficientDataActionArns = [];
78+
}
79+
80+
this.insufficientDataActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn));
81+
}
82+
83+
/**
84+
* Trigger this action if the alarm returns from breaching state into ok state
85+
*
86+
* Typically the ARN of an SNS topic or ARN of an AutoScaling policy.
87+
*/
88+
public addOkAction(...actions: IAlarmAction[]) {
89+
if (this.okActionArns === undefined) {
90+
this.okActionArns = [];
91+
}
92+
93+
this.okActionArns.push(...actions.map(a => a.bind(this, this).alarmActionArn));
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { IAlarm, IAlarmRule } from './alarm-base';
2+
3+
/**
4+
* Enumeration indicates state of Alarm used in building Alarm Rule.
5+
*/
6+
export enum AlarmState {
7+
8+
/**
9+
* State indicates resource is in ALARM
10+
*/
11+
ALARM = 'ALARM',
12+
13+
/**
14+
* State indicates resource is not in ALARM
15+
*/
16+
OK = 'OK',
17+
18+
/**
19+
* State indicates there is not enough data to determine is resource is in ALARM
20+
*/
21+
INSUFFICIENT_DATA = 'INSUFFICIENT_DATA',
22+
23+
}
24+
25+
/**
26+
* Enumeration of supported Composite Alarms operators.
27+
*/
28+
enum Operator {
29+
30+
AND = 'AND',
31+
OR = 'OR',
32+
NOT = 'NOT',
33+
34+
}
35+
36+
/**
37+
* Class with static functions to build AlarmRule for Composite Alarms.
38+
*/
39+
export class AlarmRule {
40+
41+
/**
42+
* function to join all provided AlarmRules with AND operator.
43+
*
44+
* @param operands IAlarmRules to be joined with AND operator.
45+
*/
46+
public static allOf(...operands: IAlarmRule[]): IAlarmRule {
47+
return this.concat(Operator.AND, ...operands);
48+
}
49+
50+
/**
51+
* function to join all provided AlarmRules with OR operator.
52+
*
53+
* @param operands IAlarmRules to be joined with OR operator.
54+
*/
55+
public static anyOf(...operands: IAlarmRule[]): IAlarmRule {
56+
return this.concat(Operator.OR, ...operands);
57+
}
58+
59+
/**
60+
* function to wrap provided AlarmRule in NOT operator.
61+
*
62+
* @param operand IAlarmRule to be wrapped in NOT operator.
63+
*/
64+
public static not(operand: IAlarmRule): IAlarmRule {
65+
// tslint:disable-next-line:new-parens
66+
return new class implements IAlarmRule {
67+
public renderAlarmRule(): string {
68+
return `(NOT (${operand.renderAlarmRule()}))`;
69+
}
70+
};
71+
}
72+
73+
/**
74+
* function to build TRUE/FALSE intent for Rule Expression.
75+
*
76+
* @param value boolean value to be used in rule expression.
77+
*/
78+
public static fromBoolean(value: boolean): IAlarmRule {
79+
// tslint:disable-next-line:new-parens
80+
return new class implements IAlarmRule {
81+
public renderAlarmRule(): string {
82+
return `${String(value).toUpperCase()}`;
83+
}
84+
};
85+
}
86+
87+
/**
88+
* function to build Rule Expression for given IAlarm and AlarmState.
89+
*
90+
* @param alarm IAlarm to be used in Rule Expression.
91+
* @param alarmState AlarmState to be used in Rule Expression.
92+
*/
93+
public static fromAlarm(alarm: IAlarm, alarmState: AlarmState): IAlarmRule {
94+
// tslint:disable-next-line:new-parens
95+
return new class implements IAlarmRule {
96+
public renderAlarmRule(): string {
97+
return `${alarmState}(${alarm.alarmArn})`;
98+
}
99+
};
100+
}
101+
102+
/**
103+
* function to build Rule Expression for given Alarm Rule string.
104+
*
105+
* @param alarmRule string to be used in Rule Expression.
106+
*/
107+
public static fromString(alarmRule: string): IAlarmRule {
108+
// tslint:disable-next-line:new-parens
109+
return new class implements IAlarmRule {
110+
public renderAlarmRule(): string {
111+
return alarmRule;
112+
}
113+
};
114+
}
115+
116+
private static concat(operator: Operator, ...operands: IAlarmRule[]): IAlarmRule {
117+
// tslint:disable-next-line:new-parens
118+
return new class implements IAlarmRule {
119+
public renderAlarmRule(): string {
120+
const expression = operands
121+
.map(operand => `${operand.renderAlarmRule()}`)
122+
.join(` ${operator} `);
123+
return `(${expression})`;
124+
}
125+
};
126+
}
127+
}

0 commit comments

Comments
 (0)