summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-07-23 20:32:50 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-07-23 20:32:50 -0400
commit38a783ede198026e784859e2e702bb2c1260a098 (patch)
treee95ff0c31afd01c965bf628c1e2b6b1cc04fa6c5
parent4f568db968cced94eb282d1353e43625ee8811bb (diff)
Some code cleanup on the process of identifying the resource record sets for an ASG.
-rw-r--r--src/ec2.rs46
-rw-r--r--src/route53.rs26
2 files changed, 36 insertions, 36 deletions
diff --git a/src/ec2.rs b/src/ec2.rs
index 6e8af57..604c2a2 100644
--- a/src/ec2.rs
+++ b/src/ec2.rs
@@ -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)
+ }
}
diff --git a/src/route53.rs b/src/route53.rs
index 89f4b03..5d9125e 100644
--- a/src/route53.rs
+++ b/src/route53.rs
@@ -3,7 +3,7 @@ use std::str::FromStr;
use anyhow::{anyhow, Result};
use aws_sdk_route53 as route53;
-use aws_sdk_route53::types::{HostedZone, ResourceRecord, ResourceRecordSet, RrType};
+use aws_sdk_route53::types::{HostedZone, ResourceRecordSet, RrType};
use trust_dns_proto::rr::Name;
use crate::dns::suffixes;
@@ -118,27 +118,3 @@ where
Ok(suffix_records)
}
-
-pub fn recordset<I, S>(
- apex_hostname: &str,
- dns_ttl: i64,
- rr_type: RrType,
- addresses: I,
-) -> ResourceRecordSet
-where
- I: IntoIterator<Item = S>,
- S: Into<String>,
-{
- let apex_ip4_records = addresses
- .into_iter()
- .map(|address| address.into())
- .map(|address| ResourceRecord::builder().value(address).build())
- .collect();
-
- ResourceRecordSet::builder()
- .name(apex_hostname)
- .r#type(rr_type)
- .ttl(dns_ttl)
- .set_resource_records(Some(apex_ip4_records))
- .build()
-}