@@ -3,7 +3,7 @@ use serde::de::DeserializeOwned;
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use std:: collections:: HashMap ;
5
5
6
- use crate :: custom_serde:: deserialize_lambda_map;
6
+ use crate :: custom_serde:: { deserialize_lambda_map, deserialize_nullish_boolean } ;
7
7
8
8
/// The `Event` notification event handled by Lambda
9
9
///
@@ -175,6 +175,78 @@ pub struct MessageAttribute {
175
175
pub value : String ,
176
176
}
177
177
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
+
178
250
#[ cfg( test) ]
179
251
mod test {
180
252
use super :: * ;
@@ -209,6 +281,12 @@ mod test {
209
281
let output: String = serde_json:: to_string ( & parsed) . unwrap ( ) ;
210
282
let reparsed: SnsEvent = serde_json:: from_slice ( output. as_bytes ( ) ) . unwrap ( ) ;
211
283
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) ;
212
290
}
213
291
214
292
#[ test]
0 commit comments