Skip to content

Commit

Permalink
Refactor ebs event into new ec2 submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspustina committed Jun 17, 2020
1 parent 35cf650 commit 5925b9c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
File renamed without changes.
24 changes: 24 additions & 0 deletions aws-watchtower/src/events/ec2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{config::FunctionConfig, events::HandleResult};
use bosun::Bosun;
use failure::Error;
use lambda_runtime::Context;
use serde_derive::Deserialize;

pub mod ebs;

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Ec2Event {
Ebs(ebs::VolumeEvent),
}

pub fn handle<T: Bosun>(
event: Ec2Event,
ctx: &Context,
config: &FunctionConfig,
bosun: &T,
) -> Result<HandleResult, Error> {
match event {
Ec2Event::Ebs(ebs_event) => ebs::handle(ebs_event, ctx, config, bosun),
}
}
32 changes: 29 additions & 3 deletions aws-watchtower/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde_derive::{Deserialize, Serialize};
use serde_json::{self, Value};

pub mod asg;
pub mod ebs;
pub mod ec2;
pub mod ping;

#[derive(Debug, Deserialize)]
Expand All @@ -18,7 +18,7 @@ pub enum Event {
#[serde(rename = "aws.autoscaling")]
Asg(asg::AutoScalingEvent),
#[serde(rename = "aws.ec2")]
Ebs(ebs::VolumeEvent),
Ec2(ec2::Ec2Event),
#[serde(rename = "ping")]
Ping(ping::Ping),
}
Expand Down Expand Up @@ -73,7 +73,7 @@ fn handle_event<T: Bosun>(
) -> Result<HandleResult, Error> {
match event {
Event::Asg(asg) => asg::handle(asg, ctx, config, bosun),
Event::Ebs(ebs) => ebs::handle(ebs, ctx, config, bosun),
Event::Ec2(ec2) => ec2::handle(ec2, ctx, config, bosun),
Event::Ping(ping) => ping::handle(ping, ctx, config, bosun),
}
}
Expand All @@ -89,6 +89,32 @@ mod tests {
use spectral::prelude::*;
use testing::setup;

#[test]
/// The purpose of this test is to show if an event received at the `Event` level can be parsed down to an `events::ec2::ebs::EbsVolumeEvent`.
fn test_parse_ebs_volume_event() {
setup();

let json = r#"{
"version": "0",
"id": "01234567-0123-0123-0123-012345678901",
"detail-type": "EBS Volume Notification",
"source": "aws.ec2",
"account": "012345678901",
"time": "yyyy-mm-ddThh:mm:ssZ",
"region": "us-east-1",
"resources": [
"arn:aws:ec2:us-east-1:012345678901:volume/vol-01234567"
],
"detail": {
"result": "available",
"cause": "",
"event": "createVolume",
"request-id": "01234567-0123-0123-0123-0123456789ab"
}
}"#;
let _: ec2::ebs::VolumeEvent = serde_json::from_str(&json).unwrap();
}

#[test]
fn test_parsing_error() {
setup();
Expand Down

0 comments on commit 5925b9c

Please sign in to comment.