From 2996d2b6268a320fafc512fcbee03a1b6182c521 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 24 Jul 2024 22:16:39 -0400 Subject: Normalize the target DNS name much earlier in the program. Also clean up some naming. --- src/cli.rs | 3 ++- src/ec2.rs | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0362b5e..340d537 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,6 +7,7 @@ use trust_dns_proto::rr::Name; use crate::apply::ApplyMode; use crate::aws_context::AwsContext; use crate::converge::named_asg_changes; +use crate::dns::absolute; /// Synchronize a DNS entry with an autoscaling group's running instances. /// @@ -48,7 +49,7 @@ impl Args { let changes = named_asg_changes( &aws_context, &args.autoscaling_group, - &args.dns_name, + &absolute(args.dns_name)?, args.dns_ttl, ) .await?; diff --git a/src/ec2.rs b/src/ec2.rs index 44719ed..0c55a20 100644 --- a/src/ec2.rs +++ b/src/ec2.rs @@ -8,7 +8,6 @@ use aws_sdk_route53::types::{ResourceRecord, ResourceRecordSet, RrType}; use itertools::Itertools; use trust_dns_proto::rr::Name; -use crate::dns::absolute; use crate::hashable::Hashable; pub trait Ec2 { @@ -18,13 +17,15 @@ pub trait Ec2 { pub async fn instance_recordsets( aws_context: &C, asg_name: &str, - dns_suffix: &Name, + dns_name: &Name, dns_ttl: i64, live_instance_ids: &[String], ) -> Result>> where C: Ec2, { + assert!(dns_name.is_fqdn()); + // If there's nothing running, then (a) we don't need to ask AWS about // running instances, and (b) we can't anyways as the API call requires at // least one instance ID. Abort here. @@ -37,8 +38,8 @@ where .values(asg_name) .build(); - let mut apex_ip4 = HashSet::new(); - let mut apex_ip6 = HashSet::new(); + let mut ip4 = HashSet::new(); + let mut ip6 = HashSet::new(); let mut instances_paginator = aws_context .ec2() @@ -53,7 +54,7 @@ where let instances = reservation.instances(); for instance in instances { // Mild abuse of the fact that optional values are also iterable - apex_ip4.extend(instance.public_ip_address().map(String::from)); + ip4.extend(instance.public_ip_address().map(String::from)); let instance_interfaces = instance.network_interfaces(); let instance_ip6: Vec<_> = instance_interfaces @@ -64,25 +65,23 @@ where .to_owned() .collect(); - apex_ip6.extend(instance_ip6.iter().map(ToOwned::to_owned).map(String::from)); + ip6.extend(instance_ip6.iter().map(ToOwned::to_owned).map(String::from)); } } - let apex_hostname = absolute(dns_suffix.clone())?; - let apex_hostname = apex_hostname.to_ascii(); - - let apex_ip4_recordset = apex_recordset(&apex_hostname, dns_ttl, RrType::A, apex_ip4)?; - let apex_ip6_recordset = apex_recordset(&apex_hostname, dns_ttl, RrType::Aaaa, apex_ip6)?; + let dns_name = dns_name.to_ascii(); + let ip4_recordset = host_recordset(&dns_name, dns_ttl, RrType::A, ip4)?; + let ip6_recordset = host_recordset(&dns_name, dns_ttl, RrType::Aaaa, ip6)?; - Ok(apex_ip4_recordset + Ok(ip4_recordset .into_iter() - .chain(apex_ip6_recordset.into_iter()) + .chain(ip6_recordset.into_iter()) .map(Hashable::from) .collect()) } -fn apex_recordset( - apex_hostname: &str, +fn host_recordset( + dns_name: &str, dns_ttl: i64, rr_type: RrType, addresses: HashSet>, @@ -97,7 +96,7 @@ fn apex_recordset( .try_collect()?; let record_set = ResourceRecordSet::builder() - .name(apex_hostname) + .name(dns_name) .r#type(rr_type) .ttl(dns_ttl) .set_resource_records(Some(records)) -- cgit v1.2.3