1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
use chrono::{DateTime, Utc};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::custom_serde::deserialize_lambda_map;
/// The `Event` notification event handled by Lambda
///
/// [https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SnsEvent {
pub records: Vec<SnsRecord>,
}
/// SnsRecord stores information about each record of a SNS event
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SnsRecord {
/// A string containing the event source.
pub event_source: String,
/// A string containing the event version.
pub event_version: String,
/// A string containing the event subscription ARN.
pub event_subscription_arn: String,
/// An SNS object representing the SNS message.
pub sns: SnsMessage,
}
/// SnsMessage stores information about each record of a SNS event
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct SnsMessage {
/// The type of SNS message. For a lambda event, this should always be **Notification**
#[serde(rename = "Type")]
pub sns_message_type: String,
/// A Universally Unique Identifier, unique for each message published. For a notification that Amazon SNS resends during a retry, the message ID of the original message is used.
pub message_id: String,
/// The Amazon Resource Name (ARN) for the topic that this message was published to.
pub topic_arn: String,
/// The Subject parameter specified when the notification was published to the topic.
///
/// The SNS Developer Guide states: *This is an optional parameter. If no Subject was specified, then this name-value pair does not appear in this JSON document.*
///
/// Preliminary tests show this appears in the lambda event JSON as `Subject: null`, marking as Option with need to test additional scenarios
#[serde(default)]
pub subject: Option<String>,
/// The time (UTC) when the notification was published.
pub timestamp: DateTime<Utc>,
/// Version of the Amazon SNS signature used.
pub signature_version: String,
/// Base64-encoded SHA1withRSA signature of the Message, MessageId, Subject (if present), Type, Timestamp, and TopicArn values.
pub signature: String,
/// The URL to the certificate that was used to sign the message.
#[serde(alias = "SigningCertURL")]
pub signing_cert_url: String,
/// A URL that you can use to unsubscribe the endpoint from this topic. If you visit this URL, Amazon SNS unsubscribes the endpoint and stops sending notifications to this endpoint.
#[serde(alias = "UnsubscribeURL")]
pub unsubscribe_url: String,
/// The Message value specified when the notification was published to the topic.
pub message: String,
/// This is a HashMap of defined attributes for a message. Additional details can be found in the [SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html)
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
pub message_attributes: HashMap<String, MessageAttribute>,
}
/// An alternate `Event` notification event to use alongside `SnsRecordObj<T>` and `SnsMessageObj<T>` if you want to deserialize an object inside your SNS messages rather than getting an `Option<String>` message
///
/// [https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub struct SnsEventObj<T: Serialize> {
pub records: Vec<SnsRecordObj<T>>,
}
/// Alternative to `SnsRecord`, used alongside `SnsEventObj<T>` and `SnsMessageObj<T>` when deserializing nested objects from within SNS messages)
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub struct SnsRecordObj<T: Serialize> {
/// A string containing the event source.
pub event_source: String,
/// A string containing the event version.
pub event_version: String,
/// A string containing the event subscription ARN.
pub event_subscription_arn: String,
/// An SNS object representing the SNS message.
pub sns: SnsMessageObj<T>,
}
/// Alternate version of `SnsMessage` to use in conjunction with `SnsEventObj<T>` and `SnsRecordObj<T>` for deserializing the message into a struct of type `T`
#[serde_with::serde_as]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "PascalCase")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub struct SnsMessageObj<T: Serialize> {
/// The type of SNS message. For a lambda event, this should always be **Notification**
#[serde(rename = "Type")]
pub sns_message_type: String,
/// A Universally Unique Identifier, unique for each message published. For a notification that Amazon SNS resends during a retry, the message ID of the original message is used.
pub message_id: String,
/// The Amazon Resource Name (ARN) for the topic that this message was published to.
pub topic_arn: String,
/// The Subject parameter specified when the notification was published to the topic.
///
/// The SNS Developer Guide states: *This is an optional parameter. If no Subject was specified, then this name-value pair does not appear in this JSON document.*
///
/// Preliminary tests show this appears in the lambda event JSON as `Subject: null`, marking as Option with need to test additional scenarios
#[serde(default)]
pub subject: Option<String>,
/// The time (UTC) when the notification was published.
pub timestamp: DateTime<Utc>,
/// Version of the Amazon SNS signature used.
pub signature_version: String,
/// Base64-encoded SHA1withRSA signature of the Message, MessageId, Subject (if present), Type, Timestamp, and TopicArn values.
pub signature: String,
/// The URL to the certificate that was used to sign the message.
#[serde(alias = "SigningCertURL")]
pub signing_cert_url: String,
/// A URL that you can use to unsubscribe the endpoint from this topic. If you visit this URL, Amazon SNS unsubscribes the endpoint and stops sending notifications to this endpoint.
#[serde(alias = "UnsubscribeURL")]
pub unsubscribe_url: String,
/// Deserialized into a `T` from nested JSON inside the SNS message string. `T` must implement the `Deserialize` or `DeserializeOwned` trait.
#[serde_as(as = "serde_with::json::JsonString")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub message: T,
/// This is a HashMap of defined attributes for a message. Additional details can be found in the [SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html)
#[serde(deserialize_with = "deserialize_lambda_map")]
#[serde(default)]
pub message_attributes: HashMap<String, MessageAttribute>,
}
/// Structured metadata items (such as timestamps, geospatial data, signatures, and identifiers) about the message.
///
/// Message attributes are optional and separate from—but are sent together with—the message body. The receiver can use this information to decide how to handle the message without having to process the message body first.
///
/// Additional details can be found in the [SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html)
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct MessageAttribute {
/// The data type of the attribute. Per the [SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html), lambda notifications, this will only be **String** or **Binary**.
#[serde(rename = "Type")]
pub data_type: String,
/// The user-specified message attribute value.
#[serde(rename = "Value")]
pub value: String,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[cfg(feature = "sns")]
fn my_example_sns_event() {
let data = include_bytes!("../../fixtures/example-sns-event.json");
let parsed: SnsEvent = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "sns")]
fn my_example_sns_event_pascal_case() {
let data = include_bytes!("../../fixtures/example-sns-event-pascal-case.json");
let parsed: SnsEvent = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "sns")]
fn my_example_sns_event_cloudwatch_single_metric() {
let data = include_bytes!("../../fixtures/example-cloudwatch-alarm-sns-payload-single-metric.json");
let parsed: SnsEvent = serde_json::from_slice(data).unwrap();
assert_eq!(1, parsed.records.len());
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "sns")]
fn my_example_sns_event_cloudwatch_multiple_metrics() {
let data = include_bytes!("../../fixtures/example-cloudwatch-alarm-sns-payload-multiple-metrics.json");
let parsed: SnsEvent = serde_json::from_slice(data).unwrap();
assert_eq!(2, parsed.records.len());
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEvent = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "sns")]
fn my_example_sns_obj_event() {
let data = include_bytes!("../../fixtures/example-sns-event-obj.json");
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
struct CustStruct {
foo: String,
bar: i32,
}
let parsed: SnsEventObj<CustStruct> = serde_json::from_slice(data).unwrap();
println!("{:?}", parsed);
assert_eq!(parsed.records[0].sns.message.foo, "Hello world!");
assert_eq!(parsed.records[0].sns.message.bar, 123);
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: SnsEventObj<CustStruct> = serde_json::from_slice(output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
}