From 1754b64db14a1ea409779738a84d020fbb1ac79b Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Thu, 25 Jul 2024 20:01:46 -0400 Subject: Unify dns_name and dns_ttl into a "target" type, pass that around. --- src/route53.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'src/route53.rs') diff --git a/src/route53.rs b/src/route53.rs index e4379af..e63ce4c 100644 --- a/src/route53.rs +++ b/src/route53.rs @@ -3,21 +3,23 @@ use std::str::FromStr; use anyhow::{anyhow, Result}; use aws_sdk_route53 as route53; -use aws_sdk_route53::types::{HostedZone, ResourceRecordSet, RrType}; +use aws_sdk_route53::types::{HostedZone, ResourceRecord, ResourceRecordSet, RrType}; +// Needed until try_collect is stable, see +use itertools::Itertools; use trust_dns_proto::rr::Name; -use crate::dns::suffixes; +use crate::dns; use crate::hashable::Hashable; pub trait Route53 { fn route53(&self) -> &route53::Client; } -pub async fn zone_for_domain(aws_context: &C, name: &Name) -> Result +pub async fn zone_for_name(aws_context: &C, name: &Name) -> Result where C: Route53, { - let names = suffixes(name.clone()); + let names = dns::suffixes(name.clone()); let mut zone = None; let mut depth = None; @@ -98,3 +100,44 @@ where Ok(suffix_records) } + +pub struct Target { + name: Name, + ttl: i64, +} + +impl Target { + pub fn new(name: &Name, ttl: i64) -> Result { + let name = dns::absolute(name.to_owned())?; + Ok(Self { name, ttl }) + } + + pub fn name(&self) -> &Name { + &self.name + } + + pub fn host_proposal( + &self, + rr_type: RrType, + addresses: HashSet>, + ) -> Result> { + if addresses.is_empty() { + Ok(None) + } else { + let records = addresses + .into_iter() + .map(|address| address.into()) + .map(|address| ResourceRecord::builder().value(address).build()) + .try_collect()?; + + let record_set = ResourceRecordSet::builder() + .name(self.name().to_ascii()) + .r#type(rr_type) + .ttl(self.ttl) + .set_resource_records(Some(records)) + .build()?; + + Ok(Some(record_set)) + } + } +} -- cgit v1.2.3