-
Notifications
You must be signed in to change notification settings - Fork 361
Eventbridge Event Processor #704
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
Changes from 3 commits
363dbcb
bce30ba
771e0a0
47aa639
dc4f4e9
1165ba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use chrono::{DateTime, Utc}; | ||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_with::serde_as; | ||
|
||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct EventBridgeEvent { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be an alias for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are having some problems creating this Alias due to the use of this setting in the "detail" field: #[serde_as(as = "serde_with::json::JsonString")]
#[serde(bound(deserialize = "T: DeserializeOwned"))]
pub detail: T, As the input of this field will always be a String, we expect it to be a JsonString as an EventBridgeEventObj, but if you create an alias EventBridgeEventObj<Option> and its contents are a simple String, not a JsonString, the test will fail. We're still working on solutions to avoid duplication, but so far we've had no success. Any suggestions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ok with this duplication. We can change it later if we find a way. |
||
#[serde(default)] | ||
pub version: Option<String>, | ||
#[serde(default)] | ||
pub id: Option<String>, | ||
pub detail_type: String, | ||
pub source: String, | ||
#[serde(default)] | ||
pub account: Option<String>, | ||
#[serde(default)] | ||
pub time: Option<DateTime<Utc>>, | ||
#[serde(default)] | ||
pub region: Option<String>, | ||
#[serde(default)] | ||
pub resources: Option<Vec<String>>, | ||
#[serde(default)] | ||
pub detail: Option<String>, | ||
} | ||
|
||
#[serde_with::serde_as] | ||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] | ||
#[serde(bound(deserialize = "T: DeserializeOwned"))] | ||
#[serde(rename_all = "kebab-case")] | ||
pub struct EventBridgeEventObj<T: Serialize> { | ||
#[serde(default)] | ||
pub version: Option<String>, | ||
#[serde(default)] | ||
pub id: Option<String>, | ||
pub detail_type: String, | ||
pub source: String, | ||
#[serde(default)] | ||
pub account: Option<String>, | ||
#[serde(default)] | ||
pub time: Option<DateTime<Utc>>, | ||
#[serde(default)] | ||
pub region: Option<String>, | ||
#[serde(default)] | ||
pub resources: Option<Vec<String>>, | ||
#[serde_as(as = "serde_with::json::JsonString")] | ||
#[serde(bound(deserialize = "T: DeserializeOwned"))] | ||
pub detail: T, | ||
} | ||
|
||
#[cfg(test)] | ||
#[cfg(feature = "eventbridge")] | ||
mod test { | ||
use super::*; | ||
|
||
use serde_json; | ||
|
||
#[test] | ||
fn example_eventbridge_obj_event() { | ||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] | ||
struct CustomStruct { | ||
a: String, | ||
b: String, | ||
} | ||
|
||
let data = include_bytes!("../../fixtures/example-eventbridge-event-obj.json"); | ||
let parsed: EventBridgeEventObj<CustomStruct> = serde_json::from_slice(data).unwrap(); | ||
|
||
assert_eq!(parsed.detail.a, "123"); | ||
assert_eq!(parsed.detail.b, "456"); | ||
|
||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: EventBridgeEventObj<CustomStruct> = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
|
||
#[test] | ||
fn example_eventbridge_event() { | ||
let data = include_bytes!("../../fixtures/example-eventbridge-event.json"); | ||
let parsed: EventBridgeEvent = serde_json::from_slice(data).unwrap(); | ||
assert_eq!(parsed.detail, Some(String::from("String Message"))); | ||
|
||
let output: String = serde_json::to_string(&parsed).unwrap(); | ||
let reparsed: EventBridgeEvent = serde_json::from_slice(output.as_bytes()).unwrap(); | ||
assert_eq!(parsed, reparsed); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "0", | ||
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718", | ||
"detail-type": "EC2 Instance State-change Notification", | ||
"source": "aws.ec2", | ||
"account": "111122223333", | ||
"time": "2017-12-22T18:43:48Z", | ||
"region": "us-west-1", | ||
"resources": [ | ||
"arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0" | ||
], | ||
"detail": "{\"a\":\"123\",\"b\":\"456\"}" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "0", | ||
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718", | ||
"detail-type": "EC2 Instance State-change Notification", | ||
"source": "aws.ec2", | ||
"account": "111122223333", | ||
"time": "2017-12-22T18:43:48Z", | ||
"region": "us-west-1", | ||
"resources": [ | ||
"arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0" | ||
], | ||
"detail": "String Message" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.