Skip to content

Commit bf4da44

Browse files
committed
Implement CloudWatch alarm SNS payloads.
Signed-off-by: David Calavera <[email protected]>
1 parent 387b07c commit bf4da44

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lambda-events/src/custom_serde/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ where
8080
feature = "cloudwatch_events",
8181
feature = "code_commit",
8282
feature = "cognito",
83+
feature = "sns"
8384
test
8485
))]
8586
pub(crate) fn deserialize_nullish_boolean<'de, D>(deserializer: D) -> Result<bool, D::Error>

lambda-events/src/event/sns/mod.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::de::DeserializeOwned;
33
use serde::{Deserialize, Serialize};
44
use std::collections::HashMap;
55

6-
use crate::custom_serde::deserialize_lambda_map;
6+
use crate::custom_serde::{deserialize_lambda_map, deserialize_nullish_boolean};
77

88
/// The `Event` notification event handled by Lambda
99
///
@@ -175,6 +175,78 @@ pub struct MessageAttribute {
175175
pub value: String,
176176
}
177177

178+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
179+
#[serde(rename_all = "PascalCase")]
180+
pub struct CloudWatchAlarmPayload {
181+
pub alarm_name: String,
182+
pub alarm_description: String,
183+
#[serde(rename = "AWSAccountId")]
184+
pub aws_account_id: String,
185+
pub new_state_value: String,
186+
pub new_state_reason: String,
187+
pub state_change_time: String,
188+
pub region: String,
189+
pub alarm_arn: String,
190+
pub old_state_value: String,
191+
pub trigger: CloudWatchAlarmTrigger,
192+
}
193+
194+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
195+
#[serde(rename_all = "PascalCase")]
196+
pub struct CloudWatchAlarmTrigger {
197+
pub period: i64,
198+
pub evaluation_periods: i64,
199+
pub comparison_operator: String,
200+
pub threshold: f64,
201+
pub treat_missing_data: String,
202+
pub evaluate_low_sample_count_percentile: String,
203+
#[serde(default)]
204+
pub metrics: Vec<CloudWatchMetricDataQuery>,
205+
pub metric_name: Option<String>,
206+
pub namespace: Option<String>,
207+
pub statistic_type: Option<String>,
208+
pub statistic: Option<String>,
209+
pub unit: Option<String>,
210+
#[serde(default)]
211+
pub dimensions: Vec<CloudWatchDimension>,
212+
}
213+
214+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
215+
#[serde(rename_all = "PascalCase")]
216+
pub struct CloudWatchMetricDataQuery {
217+
pub id: String,
218+
pub expression: Option<String>,
219+
pub label: Option<String>,
220+
pub metric_stat: Option<CloudWatchMetricStat>,
221+
pub period: Option<i64>,
222+
#[serde(default, deserialize_with = "deserialize_nullish_boolean")]
223+
pub return_data: bool,
224+
}
225+
226+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
227+
#[serde(rename_all = "PascalCase")]
228+
pub struct CloudWatchMetricStat {
229+
pub metric: CloudWatchMetric,
230+
pub period: i64,
231+
pub stat: String,
232+
pub unit: Option<String>,
233+
}
234+
235+
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
236+
#[serde(rename_all = "PascalCase")]
237+
pub struct CloudWatchMetric {
238+
#[serde(default)]
239+
pub dimensions: Vec<CloudWatchDimension>,
240+
pub metric_name: Option<String>,
241+
pub namespace: Option<String>,
242+
}
243+
244+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
245+
pub struct CloudWatchDimension {
246+
pub name: String,
247+
pub value: String,
248+
}
249+
178250
#[cfg(test)]
179251
mod test {
180252
use super::*;
@@ -209,6 +281,12 @@ mod test {
209281
let output: String = serde_json::to_string(&parsed).unwrap();
210282
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
211283
assert_eq!(parsed, reparsed);
284+
285+
let parsed: SnsEventObj<CloudWatchAlarmPayload> =
286+
serde_json::from_slice(data).expect("failed to parse CloudWatch Alarm payload");
287+
288+
let record = parsed.records.first().unwrap();
289+
assert_eq!("EXAMPLE", record.sns.message.alarm_name);
212290
}
213291

214292
#[test]

0 commit comments

Comments
 (0)