diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2023-10-17 21:17:55 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2023-10-17 22:32:21 -0400 |
| commit | 288205e302d9f6afa06b8602184e983d2080a5b6 (patch) | |
| tree | dfb307e8f3cb82d280e5a0392f11318194e09ef1 /src/dns.rs | |
CLI tool for updating Route53 DNS for an ASG.
Diffstat (limited to 'src/dns.rs')
| -rw-r--r-- | src/dns.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/dns.rs b/src/dns.rs new file mode 100644 index 0000000..13e3650 --- /dev/null +++ b/src/dns.rs @@ -0,0 +1,75 @@ +use std::convert::TryFrom; +use std::fmt::Debug; + +use anyhow::anyhow; +use aws_sdk_autoscaling::types::{AutoScalingGroup, Instance, LifecycleState}; +use trust_dns_proto::rr::Name; + +use crate::result::{Error, Result}; + +#[derive(Debug)] +pub struct AutoScalingGroupConfig { + pub name: String, + pub live_instance_ids: Vec<String>, +} + +impl AutoScalingGroupConfig { + fn live_instance_ids<'a>(instances: impl IntoIterator<Item = &'a Instance>) -> Vec<String> { + instances + .into_iter() + .filter(|instance| match instance.lifecycle_state() { + // See <https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroupLifecycle.html> + // + // Include pending instances so that they can obtain certs, etc. + Some(LifecycleState::Pending) => true, + Some(LifecycleState::PendingWait) => true, + Some(LifecycleState::PendingProceed) => true, + Some(LifecycleState::InService) => true, + _ => false, + }) + .flat_map(|instance| instance.instance_id()) + .map(ToOwned::to_owned) + .collect() + } +} + +impl TryFrom<&AutoScalingGroup> for AutoScalingGroupConfig { + type Error = Error; + + fn try_from(autoscaling_group: &AutoScalingGroup) -> Result<Self> { + let name = autoscaling_group + .auto_scaling_group_name() + .ok_or(anyhow!("Autoscaling group returned from AWS with no name"))? + .to_owned(); + + let instances = autoscaling_group.instances().unwrap_or(&[]); + + let live_instance_ids = Self::live_instance_ids(instances); + + Ok(Self { + name, + live_instance_ids, + }) + } +} + +pub fn suffixes(mut name: Name) -> Vec<Name> { + let mut names = Vec::new(); + + loop { + names.push(name.clone()); + if name.is_root() { + break; + } else { + name = name.base_name(); + } + } + + names +} + +pub fn absolute(name: Name) -> Result<Name> { + let root = &Name::root(); + let absolute = name.append_name(root)?; + Ok(absolute) +} |
