aboutsummaryrefslogtreecommitdiff
path: root/src/garage/cli_v2/util.rs
blob: 78399b0d82b8a59ba6ca77fc95ec811b036280ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use bytesize::ByteSize;
use format_table::format_table;

use garage_util::error::Error;

use garage_api::admin::api::*;

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
	}
}