diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-07-23 20:32:50 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-07-23 20:32:50 -0400 |
| commit | 38a783ede198026e784859e2e702bb2c1260a098 (patch) | |
| tree | e95ff0c31afd01c965bf628c1e2b6b1cc04fa6c5 /src/ec2.rs | |
| parent | 4f568db968cced94eb282d1353e43625ee8811bb (diff) | |
Some code cleanup on the process of identifying the resource record sets for an ASG.
Diffstat (limited to 'src/ec2.rs')
| -rw-r--r-- | src/ec2.rs | 46 |
1 files changed, 35 insertions, 11 deletions
@@ -3,13 +3,12 @@ use std::collections::HashSet; use anyhow::Result; use aws_sdk_ec2 as ec2; use aws_sdk_ec2::types::Filter; -use aws_sdk_route53::types::{ResourceRecordSet, RrType}; +use aws_sdk_route53::types::{ResourceRecord, ResourceRecordSet, RrType}; use tokio_stream::StreamExt; use trust_dns_proto::rr::Name; use crate::dns::absolute; use crate::hashable::Hashable; -use crate::route53::recordset; pub trait Ec2 { fn ec2(&self) -> &ec2::Client; @@ -61,7 +60,7 @@ where .flat_map(|interface| interface.ipv6_addresses().unwrap_or(&[])) // Flatmap here to drop the None values, unwrap the Some values .flat_map(|ipv6| ipv6.ipv6_address()) - .map(String::from) + .to_owned() .collect(); apex_ip6.extend(instance_ip6.iter().map(ToOwned::to_owned).map(String::from)); @@ -71,13 +70,38 @@ where let apex_hostname = absolute(dns_suffix.clone())?; let apex_hostname = apex_hostname.to_ascii(); - let mut asg_recordsets = HashSet::new(); - if !apex_ip4.is_empty() { - asg_recordsets.insert(recordset(&apex_hostname, dns_ttl, RrType::A, apex_ip4).into()); - } - if !apex_ip6.is_empty() { - asg_recordsets.insert(recordset(&apex_hostname, dns_ttl, RrType::Aaaa, apex_ip6).into()); - } + 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); + + Ok(apex_ip4_recordset + .into_iter() + .chain(apex_ip6_recordset.into_iter()) + .map(Hashable::from) + .collect()) +} - Ok(asg_recordsets) +fn apex_recordset( + apex_hostname: &str, + dns_ttl: i64, + rr_type: RrType, + addresses: HashSet<impl Into<String>>, +) -> Option<ResourceRecordSet> { + if addresses.is_empty() { + None + } else { + let records = addresses + .into_iter() + .map(|address| address.into()) + .map(|address| ResourceRecord::builder().value(address).build()) + .collect(); + + let record_set = ResourceRecordSet::builder() + .name(apex_hostname) + .r#type(rr_type) + .ttl(dns_ttl) + .set_resource_records(Some(records)) + .build(); + + Some(record_set) + } } |
