diff options
author | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-30 16:24:55 +0100 |
---|---|---|
committer | Alex Auvolat <lx@deuxfleurs.fr> | 2025-01-30 16:24:55 +0100 |
commit | 3caea5fc06a36b9e2f446c263b29948de431f30f (patch) | |
tree | 02413b5a9b458d721b45fa1d3e0230d8997916b5 /src/garage/cli_v2/layout.rs | |
parent | ebc0e9319e8e0f8d77eb538d8d0356189597acaa (diff) | |
download | garage-3caea5fc06a36b9e2f446c263b29948de431f30f.tar.gz garage-3caea5fc06a36b9e2f446c263b29948de431f30f.zip |
cli_v2: merge util.rs into layout.rs
Diffstat (limited to 'src/garage/cli_v2/layout.rs')
-rw-r--r-- | src/garage/cli_v2/layout.rs | 118 |
1 files changed, 115 insertions, 3 deletions
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<u64>) -> 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<NodeRoleResp> { + 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<Item = &'a str>, + pattern: &'a str, +) -> Result<String, Error> { + 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 + } +} |