From c9aded75272a42bb5d2b5b00b69d15c3e3dc000f Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Tue, 30 Jul 2024 11:19:09 -0400 Subject: Unify the apply arguments into a `Transaction` record type. --- src/apply.rs | 46 ++++++++++++++++++++++++++-------------------- src/cli.rs | 14 ++++---------- src/converge.rs | 29 +++++++++++------------------ 3 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/apply.rs b/src/apply.rs index e0f3ff2..bf8373f 100644 --- a/src/apply.rs +++ b/src/apply.rs @@ -7,51 +7,57 @@ use itertools::Itertools; use crate::route53::{ResourceRecordSet, Route53}; +#[derive(Debug)] +pub struct Transaction { + pub zone_id: String, + pub changes: Changes, +} + +#[derive(Debug)] +pub struct Changes { + pub remove: T, + pub insert: T, +} + pub enum ApplyMode { DryRun, Apply, } impl ApplyMode { - pub async fn apply( - &self, - aws_context: &C, - zone_id: &str, - remove_records: R, - insert_records: R, - ) -> Result<()> + pub async fn apply(&self, aws_context: &C, transaction: Transaction) -> Result<()> where C: Route53, R: IntoIterator + Debug, { match self { - ApplyMode::DryRun => dry_run(zone_id, remove_records, insert_records).await, - ApplyMode::Apply => apply(aws_context, zone_id, remove_records, insert_records).await, + ApplyMode::DryRun => dry_run(transaction).await, + ApplyMode::Apply => apply(aws_context, transaction).await, } } } -async fn dry_run(zone_id: &str, remove_records: R, insert_records: R) -> Result<()> +async fn dry_run(transaction: Transaction) -> Result<()> where R: IntoIterator + Debug, { - println!("ZONE: {}", zone_id); - println!("REMOVE: {:#?}", remove_records); - println!("INSERT: {:#?}", insert_records); + println!("ZONE: {}", transaction.zone_id); + println!("REMOVE: {:#?}", transaction.changes.remove); + println!("INSERT: {:#?}", transaction.changes.insert); Ok(()) } -async fn apply( - aws_context: &C, - zone_id: &str, - remove_records: R, - insert_records: R, -) -> Result<()> +async fn apply(aws_context: &C, transaction: Transaction) -> Result<()> where C: Route53, R: IntoIterator, { + let Changes { + remove: remove_records, + insert: insert_records, + } = transaction.changes; + let remove_records = remove_records.into_iter().map(|record| { Change::builder() .action(ChangeAction::Delete) // <-- @@ -74,7 +80,7 @@ where aws_context .route53() .change_resource_record_sets() - .hosted_zone_id(zone_id) + .hosted_zone_id(transaction.zone_id) .change_batch(change_batch) .send() .await?; diff --git a/src/cli.rs b/src/cli.rs index 7a8d2c7..972f13c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,7 +6,7 @@ use trust_dns_proto::rr::Name; use crate::apply::ApplyMode; use crate::aws_context::AwsContext; -use crate::converge::named_asg_changes; +use crate::converge::propose_transaction; use crate::route53::Target; /// Synchronize a DNS entry with an autoscaling group's running instances. @@ -47,16 +47,10 @@ impl Args { let aws_context = AwsContext::from_env().await; let target = self.target()?; - let changes = named_asg_changes(&aws_context, &target, &args.autoscaling_group).await?; + let transaction = + propose_transaction(&aws_context, &target, &args.autoscaling_group).await?; - self.apply_mode() - .apply( - &aws_context, - &changes.zone_id, - changes.remove, - changes.insert, - ) - .await?; + self.apply_mode().apply(&aws_context, transaction).await?; Ok(()) } diff --git a/src/converge.rs b/src/converge.rs index 29c4878..d9a4188 100644 --- a/src/converge.rs +++ b/src/converge.rs @@ -4,22 +4,16 @@ use std::fmt::Debug; use anyhow::Result; use futures::try_join; +use crate::apply::{Changes, Transaction}; use crate::autoscaling::{propose_asg_recordsets, AutoScaling}; use crate::ec2::Ec2; use crate::route53::{zone_actual_recordsets, zone_for_name, ResourceRecordSet, Route53, Target}; -#[derive(Debug)] -pub struct Changes { - pub zone_id: String, - pub remove: T, - pub insert: T, -} - -pub async fn named_asg_changes( +pub async fn propose_transaction( aws_context: &C, target: &Target, asg_name: &str, -) -> Result + Debug>> +) -> Result + Debug>> where C: AutoScaling + Ec2 + Route53, { @@ -30,12 +24,15 @@ where zone_actual_recordsets(aws_context, &zone.id, target.name()), )?; - let changes = changes_for_records(&zone.id, &proposed, &actual); - Ok(changes) + let changes = propose_changes(&proposed, &actual); + let transaction = Transaction { + zone_id: zone.id().into(), + changes, + }; + Ok(transaction) } -fn changes_for_records( - zone_id: &str, +fn propose_changes( intended: &HashSet, actual: &HashSet, ) -> Changes + Debug> @@ -45,9 +42,5 @@ where let remove: Vec<_> = actual.difference(intended).cloned().collect(); let insert: Vec<_> = intended.difference(actual).cloned().collect(); - Changes { - zone_id: zone_id.into(), - remove, - insert, - } + Changes { remove, insert } } -- cgit v1.2.3