use crate::region::Region;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ConnectorIntent {
AllIngress,
Egress,
}
impl ConnectorIntent {
pub const ALL: [ConnectorIntent; 2] = [ConnectorIntent::AllIngress, ConnectorIntent::Egress];
pub fn wire_name(self) -> &'static str {
match self {
ConnectorIntent::AllIngress => "ALL_INGRESS",
ConnectorIntent::Egress => "INTERNET_EGRESS",
}
}
pub fn arn(self, region: &Region) -> String {
format!(
"arn:aws:lambda:{}:aws:network-connector:aws-network-connector:{}",
region.as_str(),
self.wire_name()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_ingress_arn_is_the_measured_literal() {
assert_eq!(
ConnectorIntent::AllIngress.arn(&Region::UsEast1),
"arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:ALL_INGRESS"
);
}
#[test]
fn the_egress_arn_uses_the_internet_egress_wire_name() {
assert_eq!(
ConnectorIntent::Egress.arn(&Region::UsEast1),
"arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:INTERNET_EGRESS"
);
}
#[test]
fn the_arn_carries_the_request_region_for_every_region() {
for region in crate::region::MICROVM_REGIONS {
let arn = ConnectorIntent::AllIngress.arn(®ion);
assert!(
arn.contains(&format!(":lambda:{}:aws:", region.as_str())),
"{arn}"
);
}
let unlisted = Region::unlisted("me-south-1");
assert!(
ConnectorIntent::Egress
.arn(&unlisted)
.contains(":lambda:me-south-1:aws:"),
"the escape hatch still derives an ARN for its own region"
);
}
#[test]
fn no_intent_names_shell_ingress() {
assert_eq!(
ConnectorIntent::ALL.len(),
2,
"two intents, and shell is not one"
);
for intent in ConnectorIntent::ALL {
let arn = intent.arn(&Region::UsEast1);
assert!(
!arn.contains("SHELL"),
"SHELL_INGRESS gates a debug console, not programmatic exec: {arn}"
);
assert!(!intent.wire_name().contains("SHELL"));
}
}
#[test]
fn the_two_intents_do_not_render_the_same_arn() {
let ingress = ConnectorIntent::AllIngress.arn(&Region::EuWest1);
let egress = ConnectorIntent::Egress.arn(&Region::EuWest1);
assert_ne!(ingress, egress);
assert!(!ingress.contains("INTERNET"), "{ingress}");
}
#[test]
fn every_derived_arn_fits_the_models_connector_bounds() {
for region in crate::region::MICROVM_REGIONS {
for intent in ConnectorIntent::ALL {
let arn = intent.arn(®ion);
assert!((1..=2048).contains(&arn.len()), "{arn}");
assert!(arn.starts_with("arn:aws:lambda:"), "{arn}");
}
}
}
}