Skip to content

Commit

Permalink
Fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspustina committed Jun 17, 2020
1 parent f612d20 commit 6fa41d6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 63 deletions.
7 changes: 5 additions & 2 deletions aws-watchtower/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ impl EncryptedFunctionConfig {
..self.bosun
};

let config = FunctionConfig { bosun, asg: self.asg, ec2: self.ec2 };
let config = FunctionConfig {
bosun,
asg: self.asg,
ec2: self.ec2,
};

Ok(config)
}
Expand Down Expand Up @@ -53,7 +57,6 @@ pub struct Ec2 {
pub scaledown_silence_duration: String,
}


#[derive(PartialEq, Deserialize, Serialize, Debug)]
pub struct FunctionConfig {
pub bosun: Bosun,
Expand Down
23 changes: 11 additions & 12 deletions aws-watchtower/src/events/ec2/ebs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ use std::fmt;
// }
#[derive(Debug, Deserialize)]
pub struct VolumeEvent {
pub version: String,
pub id: String,
/* These fields do not exist, because serde used both of them to "route" the deserialization to this point.
#[serde(rename = "detail-type")]
pub detail_type: String,
pub source: String,
*/
pub account: String,
pub time: String,
pub region: String,
pub resources: Vec<String>,
pub detail: VolumeEventDetail,
pub version: String,
pub id: String,
// These fields do not exist, because serde used both of them to "route" the deserialization to this point.
// #[serde(rename = "detail-type")]
// pub detail_type: String,
// pub source: String,
pub account: String,
pub time: String,
pub region: String,
pub resources: Vec<String>,
pub detail: VolumeEventDetail,
}

#[derive(Debug, Deserialize)]
Expand Down
74 changes: 42 additions & 32 deletions aws-watchtower/src/events/ec2/ec2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{asg_mapping::Mapping, config::FunctionConfig, events::HandleResult, metrics};
use aws::ec2::ec2::{Ec2StateInfo, Ec2State};
use aws::ec2::ec2::{Ec2State, Ec2StateInfo};
use bosun::{Bosun, Datum, Silence, Tags};
use failure::Error;
use lambda_runtime::Context;
Expand All @@ -24,24 +24,23 @@ use serde_derive::Deserialize;
// }
#[derive(Debug, Deserialize)]
pub struct Ec2StateChangeEvent {
pub id: String,
/* These fields do not exist, because serde used both of them to "route" the deserialization to this point.
#[serde(rename = "detail-type")]
pub detail_type: String,
pub source: String,
*/
pub account: String,
pub time: String,
pub region: String,
pub resources: Vec<String>,
pub detail: Ec2StateChangeDetail,
pub id: String,
// These fields do not exist, because serde used both of them to "route" the deserialization to this point.
// #[serde(rename = "detail-type")]
// pub detail_type: String,
// pub source: String,
pub account: String,
pub time: String,
pub region: String,
pub resources: Vec<String>,
pub detail: Ec2StateChangeDetail,
}

#[derive(Debug, Deserialize)]
pub struct Ec2StateChangeDetail {
#[serde(rename = "instance-id")]
pub instance_id: String,
pub state: Ec2State,
pub instance_id: String,
pub state: Ec2State,
}

pub fn handle<T: Bosun>(
Expand All @@ -54,7 +53,12 @@ pub fn handle<T: Bosun>(

// Get ASG for this instance if any
let asg = aws::ec2::asg::get_asg_by_instance_id(state_change.detail.instance_id.clone())?;
info!("Mapped instance id to ASG '{:?}'.", asg.as_ref().map(|x| x.auto_scaling_group_name.as_str()).unwrap_or("unmapped"));
info!(
"Mapped instance id to ASG '{:?}'.",
asg.as_ref()
.map(|x| x.auto_scaling_group_name.as_str())
.unwrap_or("unmapped")
);

let mapping = asg
.as_ref()
Expand All @@ -75,35 +79,41 @@ pub fn handle<T: Bosun>(
bosun.emit_datum(&datum)?;

// If we haven't found an ASG this instance belongs to, the
// the instance cannot have been terminated because of an
// auto-scaling lifecycle event. Therefore we're not going to set a silence to
// the instance cannot have been terminated because of an
// auto-scaling lifecycle event. Therefore we're not going to set a silence to
// prevent silencing a infrastructure problem.
match mapping {
Some(ref mapping) if state_change.detail.state == Ec2State::ShuttingDown => {
set_bosun_silence(&state_change.detail.instance_id, &config.ec2.scaledown_silence_duration, mapping, bosun)?;
set_bosun_silence(
&state_change.detail.instance_id,
&config.ec2.scaledown_silence_duration,
mapping,
bosun,
)?;
}
Some(_) => {
debug!("Non-shutting-down state change for instance id ({}), no silence necessary", &state_change.detail.instance_id);
debug!(
"Non-shutting-down state change for instance id ({}), no silence necessary",
&state_change.detail.instance_id
);
}
None => {
info!("No ASG found for instance id ({}), refusing to set a silence", &state_change.detail.instance_id);
info!(
"No ASG found for instance id ({}), refusing to set a silence",
&state_change.detail.instance_id
);
}
}

let ec2_state_info = Ec2StateInfo {
ec2_instance_id: state_change.detail.instance_id,
state: state_change.detail.state,
state: state_change.detail.state,
};

Ok(HandleResult::Ec2StateInfo{ec2_state_info})
Ok(HandleResult::Ec2StateInfo { ec2_state_info })
}

fn set_bosun_silence(
instance_id: &str,
duration: &str,
mapping: &Mapping,
bosun: &dyn Bosun,
) -> Result<(), Error> {
fn set_bosun_silence(instance_id: &str, duration: &str, mapping: &Mapping, bosun: &dyn Bosun) -> Result<(), Error> {
let host = format!("{}{}*", &mapping.host_prefix, instance_id);
info!("Setting silence of {} for host '{}'.", duration, host);

Expand All @@ -123,8 +133,8 @@ mod tests {

fn setup() { testing::setup(); }

#[test]
fn parse_pending_ec2_state_change_event_from_json() {
#[test]
fn parse_pending_ec2_state_change_event_from_json() {
setup();

let json = json!(
Expand Down Expand Up @@ -152,7 +162,7 @@ mod tests {
assert_that(&event).is_ok();
}

#[test]
#[test]
fn parse_shutting_down_ec2_state_change_event_from_json() {
setup();

Expand All @@ -179,5 +189,5 @@ mod tests {
info!("event = {:?}", event);

assert_that(&event).is_ok();
}
}
}
18 changes: 10 additions & 8 deletions aws-watchtower/src/events/ec2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ mod test {
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 `ec2::ebs::EbsVolumeEvent`.
/// The purpose of this test is to show if an event received at the `Event` level can be parsed
/// down to an `ec2::ebs::EbsVolumeEvent`.
fn test_parse_ebs_volume_event() {
setup();

Expand All @@ -64,13 +65,14 @@ mod test {
let event: Ec2Event = serde_json::from_str(&json).unwrap();

match event {
Ec2Event::VolumeEvent(_) => {},
Ec2Event::VolumeEvent(_) => {}
_ => panic!("Parsed wrong event"),
}
}

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

Expand All @@ -89,11 +91,11 @@ mod test {
"state":"pending"
}
}"#;
let event: Ec2Event = serde_json::from_str(&json).unwrap();
let event: Ec2Event = serde_json::from_str(&json).unwrap();

match event {
Ec2Event::Ec2StateChangeEvent(_) => {},
_ => panic!("Parsed wrong event"),
}
match event {
Ec2Event::Ec2StateChangeEvent(_) => {}
_ => panic!("Parsed wrong event"),
}
}
}
18 changes: 9 additions & 9 deletions aws/src/ec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub mod asg {
use crate::{auth, AwsError};
use failure::Error;
use log::debug;
use rusoto_core::{HttpClient, Region};
use rusoto_autoscaling::{Autoscaling, AutoscalingClient, DescribeAutoScalingInstancesType};
use rusoto_core::{HttpClient, Region};
use serde_derive::Serialize;

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -45,10 +45,10 @@ pub mod asg {

let asg_info = first_asg.map(|details| {
AsgInfo {
ec2_instance_id: instance_id,
ec2_instance_id: instance_id,
auto_scaling_group_name: details.auto_scaling_group_name,
}
} );
});
debug!("Parsed autoscaling information: '{:?}'", asg_info);

Ok(asg_info)
Expand Down Expand Up @@ -179,17 +179,17 @@ pub mod ec2 {
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "kebab-case")]
pub enum Ec2State {
Pending = 1,
Running = 2,
Pending = 1,
Running = 2,
ShuttingDown = 3,
Stopped = 4,
Stopping = 5,
Terminated = 6,
Stopped = 4,
Stopping = 5,
Terminated = 6,
}

#[derive(Debug, Serialize)]
pub struct Ec2StateInfo {
pub ec2_instance_id: String,
pub state: Ec2State,
pub state: Ec2State,
}
}

0 comments on commit 6fa41d6

Please sign in to comment.