move zone redundancy parsing/formatting to cli
Some checks failed
ci/woodpecker/pr/debug Pipeline failed
ci/woodpecker/push/debug Pipeline failed

This commit is contained in:
Alex 2025-03-11 10:00:37 +01:00
parent df758e8e0d
commit 5f308bd688
2 changed files with 27 additions and 36 deletions

View file

@ -4,7 +4,6 @@ use format_table::format_table;
use garage_util::error::*; use garage_util::error::*;
use garage_api_admin::api::*; use garage_api_admin::api::*;
use garage_rpc::layout;
use crate::cli::remote::*; use crate::cli::remote::*;
use crate::cli::structs::*; use crate::cli::structs::*;
@ -162,18 +161,17 @@ impl Cli {
match config_opt.redundancy { match config_opt.redundancy {
None => (), None => (),
Some(r_str) => { Some(r_str) => {
let r = r_str let r = parse_zone_redundancy(&r_str)?;
.parse::<layout::ZoneRedundancy>()
.ok_or_message("invalid zone redundancy value")?;
self.api_request(UpdateClusterLayoutRequest { self.api_request(UpdateClusterLayoutRequest {
roles: vec![], roles: vec![],
parameters: Some(LayoutParameters { parameters: Some(LayoutParameters { zone_redundancy: r }),
zone_redundancy: r.into(),
}),
}) })
.await?; .await?;
println!("The zone redundancy parameter has been set to '{}'.", r); println!(
"The zone redundancy parameter has been set to '{}'.",
display_zone_redundancy(r)
);
did_something = true; did_something = true;
} }
} }
@ -403,7 +401,7 @@ pub fn print_cluster_layout(layout: &GetClusterLayoutResponse, empty_msg: &str)
println!(); println!();
println!( println!(
"Zone redundancy: {}", "Zone redundancy: {}",
Into::<layout::ZoneRedundancy>::into(layout.parameters.zone_redundancy) display_zone_redundancy(layout.parameters.zone_redundancy),
); );
} else { } else {
println!("{}", empty_msg); println!("{}", empty_msg);
@ -447,7 +445,7 @@ pub fn print_staging_role_changes(layout: &GetClusterLayoutResponse) -> bool {
if let Some(p) = layout.staged_parameters.as_ref() { if let Some(p) = layout.staged_parameters.as_ref() {
println!( println!(
"Zone redundancy: {}", "Zone redundancy: {}",
Into::<layout::ZoneRedundancy>::into(p.zone_redundancy) display_zone_redundancy(p.zone_redundancy)
); );
} }
true true
@ -455,3 +453,22 @@ pub fn print_staging_role_changes(layout: &GetClusterLayoutResponse) -> bool {
false false
} }
} }
pub fn display_zone_redundancy(z: ZoneRedundancy) -> String {
match z {
ZoneRedundancy::Maximum => "maximum".into(),
ZoneRedundancy::AtLeast(x) => x.to_string(),
}
}
pub fn parse_zone_redundancy(s: &str) -> Result<ZoneRedundancy, Error> {
match s {
"none" | "max" | "maximum" => Ok(ZoneRedundancy::Maximum),
x => {
let v = x.parse::<usize>().map_err(|_| {
Error::Message("zone redundancy must be 'none'/'max' or an integer".into())
})?;
Ok(ZoneRedundancy::AtLeast(v))
}
}
}

View file

@ -1,5 +1,3 @@
use std::fmt;
use bytesize::ByteSize; use bytesize::ByteSize;
use garage_util::crdt::{AutoCrdt, Crdt}; use garage_util::crdt::{AutoCrdt, Crdt};
@ -397,30 +395,6 @@ impl NodeRole {
} }
} }
impl fmt::Display for ZoneRedundancy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ZoneRedundancy::Maximum => write!(f, "maximum"),
ZoneRedundancy::AtLeast(x) => write!(f, "{}", x),
}
}
}
impl core::str::FromStr for ZoneRedundancy {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" | "max" | "maximum" => Ok(ZoneRedundancy::Maximum),
x => {
let v = x
.parse::<usize>()
.map_err(|_| "zone redundancy must be 'none'/'max' or an integer")?;
Ok(ZoneRedundancy::AtLeast(v))
}
}
}
}
impl UpdateTracker { impl UpdateTracker {
fn merge(&mut self, other: &UpdateTracker) -> bool { fn merge(&mut self, other: &UpdateTracker) -> bool {
let mut changed = false; let mut changed = false;