From 3caea5fc06a36b9e2f446c263b29948de431f30f Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 30 Jan 2025 16:24:55 +0100 Subject: [PATCH] cli_v2: merge util.rs into layout.rs --- src/garage/cli_v2/cluster.rs | 2 +- src/garage/cli_v2/layout.rs | 118 ++++++++++++++++++++++++++++++++++- src/garage/cli_v2/mod.rs | 2 - src/garage/cli_v2/util.rs | 115 ---------------------------------- 4 files changed, 116 insertions(+), 121 deletions(-) delete mode 100644 src/garage/cli_v2/util.rs diff --git a/src/garage/cli_v2/cluster.rs b/src/garage/cli_v2/cluster.rs index adaf9a25..e6ba2428 100644 --- a/src/garage/cli_v2/cluster.rs +++ b/src/garage/cli_v2/cluster.rs @@ -5,7 +5,7 @@ use garage_util::error::*; use garage_api::admin::api::*; use crate::cli::structs::*; -use crate::cli_v2::util::*; +use crate::cli_v2::layout::*; use crate::cli_v2::*; impl Cli { diff --git a/src/garage/cli_v2/layout.rs b/src/garage/cli_v2/layout.rs index 8088f019..d44771c7 100644 --- a/src/garage/cli_v2/layout.rs +++ b/src/garage/cli_v2/layout.rs @@ -1,5 +1,5 @@ -//use bytesize::ByteSize; -//use format_table::format_table; +use bytesize::ByteSize; +use format_table::format_table; use garage_util::error::*; @@ -7,7 +7,6 @@ use garage_api::admin::api::*; use crate::cli::layout as cli_v1; use crate::cli::structs::*; -use crate::cli_v2::util::*; use crate::cli_v2::*; impl Cli { @@ -170,3 +169,116 @@ To know the correct value of the new layout version, invoke `garage layout show` Ok(()) } } + +// -------------------------- +// ---- helper functions ---- +// -------------------------- + +pub fn capacity_string(v: Option) -> String { + match v { + Some(c) => ByteSize::b(c).to_string_as(false), + None => "gateway".to_string(), + } +} + +pub fn get_staged_or_current_role( + id: &str, + layout: &GetClusterLayoutResponse, +) -> Option { + for node in layout.staged_role_changes.iter() { + if node.id == id { + return match &node.action { + NodeRoleChangeEnum::Remove { .. } => None, + NodeRoleChangeEnum::Update { + zone, + capacity, + tags, + } => Some(NodeRoleResp { + id: id.to_string(), + zone: zone.to_string(), + capacity: *capacity, + tags: tags.clone(), + }), + }; + } + } + + for node in layout.roles.iter() { + if node.id == id { + return Some(node.clone()); + } + } + + None +} + +pub fn find_matching_node<'a>( + cand: impl std::iter::Iterator, + pattern: &'a str, +) -> Result { + let mut candidates = vec![]; + for c in cand { + if c.starts_with(pattern) && !candidates.contains(&c) { + candidates.push(c); + } + } + if candidates.len() != 1 { + Err(Error::Message(format!( + "{} nodes match '{}'", + candidates.len(), + pattern, + ))) + } else { + Ok(candidates[0].to_string()) + } +} + +pub fn print_staging_role_changes(layout: &GetClusterLayoutResponse) -> bool { + let has_role_changes = !layout.staged_role_changes.is_empty(); + + // TODO!! Layout parameters + let has_layout_changes = false; + + if has_role_changes || has_layout_changes { + println!(); + println!("==== STAGED ROLE CHANGES ===="); + if has_role_changes { + let mut table = vec!["ID\tTags\tZone\tCapacity".to_string()]; + for change in layout.staged_role_changes.iter() { + match &change.action { + NodeRoleChangeEnum::Update { + tags, + zone, + capacity, + } => { + let tags = tags.join(","); + table.push(format!( + "{:.16}\t{}\t{}\t{}", + change.id, + tags, + zone, + capacity_string(*capacity), + )); + } + NodeRoleChangeEnum::Remove { .. } => { + table.push(format!("{:.16}\tREMOVED", change.id)); + } + } + } + format_table(table); + println!(); + } + //TODO + /* + if has_layout_changes { + println!( + "Zone redundancy: {}", + staging.parameters.get().zone_redundancy + ); + } + */ + true + } else { + false + } +} diff --git a/src/garage/cli_v2/mod.rs b/src/garage/cli_v2/mod.rs index b51ed67f..51c4d144 100644 --- a/src/garage/cli_v2/mod.rs +++ b/src/garage/cli_v2/mod.rs @@ -1,5 +1,3 @@ -pub mod util; - pub mod bucket; pub mod cluster; pub mod key; diff --git a/src/garage/cli_v2/util.rs b/src/garage/cli_v2/util.rs deleted file mode 100644 index 78399b0d..00000000 --- a/src/garage/cli_v2/util.rs +++ /dev/null @@ -1,115 +0,0 @@ -use bytesize::ByteSize; -use format_table::format_table; - -use garage_util::error::Error; - -use garage_api::admin::api::*; - -pub fn capacity_string(v: Option) -> String { - match v { - Some(c) => ByteSize::b(c).to_string_as(false), - None => "gateway".to_string(), - } -} - -pub fn get_staged_or_current_role( - id: &str, - layout: &GetClusterLayoutResponse, -) -> Option { - for node in layout.staged_role_changes.iter() { - if node.id == id { - return match &node.action { - NodeRoleChangeEnum::Remove { .. } => None, - NodeRoleChangeEnum::Update { - zone, - capacity, - tags, - } => Some(NodeRoleResp { - id: id.to_string(), - zone: zone.to_string(), - capacity: *capacity, - tags: tags.clone(), - }), - }; - } - } - - for node in layout.roles.iter() { - if node.id == id { - return Some(node.clone()); - } - } - - None -} - -pub fn find_matching_node<'a>( - cand: impl std::iter::Iterator, - pattern: &'a str, -) -> Result { - let mut candidates = vec![]; - for c in cand { - if c.starts_with(pattern) && !candidates.contains(&c) { - candidates.push(c); - } - } - if candidates.len() != 1 { - Err(Error::Message(format!( - "{} nodes match '{}'", - candidates.len(), - pattern, - ))) - } else { - Ok(candidates[0].to_string()) - } -} - -pub fn print_staging_role_changes(layout: &GetClusterLayoutResponse) -> bool { - let has_role_changes = !layout.staged_role_changes.is_empty(); - - // TODO!! Layout parameters - let has_layout_changes = false; - - if has_role_changes || has_layout_changes { - println!(); - println!("==== STAGED ROLE CHANGES ===="); - if has_role_changes { - let mut table = vec!["ID\tTags\tZone\tCapacity".to_string()]; - for change in layout.staged_role_changes.iter() { - match &change.action { - NodeRoleChangeEnum::Update { - tags, - zone, - capacity, - } => { - let tags = tags.join(","); - table.push(format!( - "{:.16}\t{}\t{}\t{}", - change.id, - tags, - zone, - capacity_string(*capacity), - )); - } - NodeRoleChangeEnum::Remove { .. } => { - table.push(format!("{:.16}\tREMOVED", change.id)); - } - } - } - format_table(table); - println!(); - } - //TODO - /* - if has_layout_changes { - println!( - "Zone redundancy: {}", - staging.parameters.get().zone_redundancy - ); - } - */ - true - } else { - false - } -}