diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-07-24 19:39:46 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-07-24 19:40:30 -0400 |
| commit | f2d288fd5d1054075374f3f7229f8a9332adf61c (patch) | |
| tree | 2884fe74bbd15ed5951619ee3438068e9101d4bc | |
| parent | a556388635b1f7543a361985d1ef3e2c58ddd386 (diff) | |
Be more specific about zero vs. two or more ASGs matching the name.
This also lets me remove the clever-but-silly Single trait.
| -rw-r--r-- | src/autoscaling.rs | 23 | ||||
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | src/single.rs | 24 |
3 files changed, 9 insertions, 39 deletions
diff --git a/src/autoscaling.rs b/src/autoscaling.rs index 327a4f7..ea76dc0 100644 --- a/src/autoscaling.rs +++ b/src/autoscaling.rs @@ -1,9 +1,7 @@ -use anyhow::{anyhow, Result}; +use anyhow::{bail, Result}; use aws_sdk_autoscaling as autoscaling; use aws_sdk_autoscaling::types::AutoScalingGroup; -use crate::single::Single; - pub trait AutoScaling { fn autoscaling(&self) -> &autoscaling::Client; } @@ -12,22 +10,19 @@ pub async fn asg_by_name<C>(aws_context: &C, name: &str) -> Result<AutoScalingGr where C: AutoScaling, { - let asg_resp = aws_context + let groups_resp = aws_context .autoscaling() .describe_auto_scaling_groups() .auto_scaling_group_names(name) .send() .await?; - let auto_scaling_groups = asg_resp.auto_scaling_groups(); - let auto_scaling_group = auto_scaling_groups - .iter() - .map(ToOwned::to_owned) - .single() - .ok_or(anyhow!( - "No unique autoscaling group found with name: {}", - name - ))?; + let mut groups = groups_resp.auto_scaling_groups().iter(); + let group = match (groups.next(), groups.next()) { + (Some(group), None) => group, + (None, _) => bail!("No autoscaling group found with name: {name}"), + (Some(_), Some(_)) => bail!("Multiple autoscaling groups found with name: {name}"), + }; - Ok(auto_scaling_group) + Ok(group.to_owned()) } @@ -7,4 +7,3 @@ mod dns; mod ec2; mod hashable; mod route53; -mod single; diff --git a/src/single.rs b/src/single.rs deleted file mode 100644 index b3f0b18..0000000 --- a/src/single.rs +++ /dev/null @@ -1,24 +0,0 @@ -pub trait Single { - type Item; - - fn single(self) -> Option<Self::Item>; -} - -impl<T, I> Single for I -where - I: IntoIterator<Item = T>, -{ - type Item = T; - - fn single(self) -> Option<Self::Item> { - let mut iter = self.into_iter(); - - // There are three cases of interest: - // - // 1. `self` has zero items -> return None. - // 2. `self` has two or more items -> return None. - // 3. `self` has exactly one item -> return `iter.next()` unchanged, as - // it holds that item. - iter.next().filter(|_| iter.next().is_none()) - } -} |
