Skip to content

Implement CloudWatch alarm SNS payloads. #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lambda-events/src/custom_serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ where
feature = "cloudwatch_events",
feature = "code_commit",
feature = "cognito",
feature = "sns",
test
))]
pub(crate) fn deserialize_nullish_boolean<'de, D>(deserializer: D) -> Result<bool, D::Error>
Expand Down
80 changes: 79 additions & 1 deletion lambda-events/src/event/sns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::custom_serde::deserialize_lambda_map;
use crate::custom_serde::{deserialize_lambda_map, deserialize_nullish_boolean};

/// The `Event` notification event handled by Lambda
///
Expand Down Expand Up @@ -175,6 +175,78 @@ pub struct MessageAttribute {
pub value: String,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CloudWatchAlarmPayload {
pub alarm_name: String,
pub alarm_description: String,
#[serde(rename = "AWSAccountId")]
pub aws_account_id: String,
pub new_state_value: String,
pub new_state_reason: String,
pub state_change_time: String,
pub region: String,
pub alarm_arn: String,
pub old_state_value: String,
pub trigger: CloudWatchAlarmTrigger,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CloudWatchAlarmTrigger {
pub period: i64,
pub evaluation_periods: i64,
pub comparison_operator: String,
pub threshold: f64,
pub treat_missing_data: String,
pub evaluate_low_sample_count_percentile: String,
#[serde(default)]
pub metrics: Vec<CloudWatchMetricDataQuery>,
pub metric_name: Option<String>,
pub namespace: Option<String>,
pub statistic_type: Option<String>,
pub statistic: Option<String>,
pub unit: Option<String>,
#[serde(default)]
pub dimensions: Vec<CloudWatchDimension>,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CloudWatchMetricDataQuery {
pub id: String,
pub expression: Option<String>,
pub label: Option<String>,
pub metric_stat: Option<CloudWatchMetricStat>,
pub period: Option<i64>,
#[serde(default, deserialize_with = "deserialize_nullish_boolean")]
pub return_data: bool,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CloudWatchMetricStat {
pub metric: CloudWatchMetric,
pub period: i64,
pub stat: String,
pub unit: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct CloudWatchMetric {
#[serde(default)]
pub dimensions: Vec<CloudWatchDimension>,
pub metric_name: Option<String>,
pub namespace: Option<String>,
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct CloudWatchDimension {
pub name: String,
pub value: String,
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -209,6 +281,12 @@ mod test {
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);

let parsed: SnsEventObj<CloudWatchAlarmPayload> =
serde_json::from_slice(data).expect("failed to parse CloudWatch Alarm payload");

let record = parsed.records.first().unwrap();
assert_eq!("EXAMPLE", record.sns.message.alarm_name);
}

#[test]
Expand Down