-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Eventbridge Event Processor * cfg feature fix * feature comment * Removed whitespace * makefile fix --------- Co-authored-by: nich.morgan <[email protected]> Co-authored-by: erso <[email protected]>
- Loading branch information
1 parent
1cbe34b
commit bcd3f97
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
#[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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
lambda-events/src/fixtures/example-eventbridge-event-obj.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters