diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-07-25 20:01:46 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-07-25 20:01:46 -0400 |
| commit | 1754b64db14a1ea409779738a84d020fbb1ac79b (patch) | |
| tree | 8b41783dd178884492529b76c1976658dac497b5 /src/route53.rs | |
| parent | a2e2df7c0f97633bba3a91981c4c1598a062aaac (diff) | |
Unify dns_name and dns_ttl into a "target" type, pass that around.
Diffstat (limited to 'src/route53.rs')
| -rw-r--r-- | src/route53.rs | 51 |
1 files changed, 47 insertions, 4 deletions
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 <https://github.com/rust-lang/rust/issues/94047> +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<C>(aws_context: &C, name: &Name) -> Result<HostedZone> +pub async fn zone_for_name<C>(aws_context: &C, name: &Name) -> Result<HostedZone> 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<Self> { + 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<impl Into<String>>, + ) -> Result<Option<ResourceRecordSet>> { + 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)) + } + } +} |
