-
Notifications
You must be signed in to change notification settings - Fork 360
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
Merged
calavera
merged 6 commits into
awslabs:main
from
open-crust:feature/eventbridge-support
Oct 19, 2023
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
363dbcb
Eventbridge Event Processor
morgan1371 bce30ba
cfg feature fix
nichmorgan 771e0a0
feature comment
nichmorgan 47aa639
Removed whitespace
EduardoRSO dc4f4e9
Merge branch 'main' into feature/eventbridge-support
nichmorgan 1165ba6
makefile fix
nichmorgan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be an alias for
EventBridgeEventObj<Option<String>>
to avoid code duplication?There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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:
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 comment
The 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.