Skip to content

Commit e732a78

Browse files
authored
[omdb] print out chicken switch diff when setting switches (#8694)
Nice quality of life improvement.
1 parent ddce3b6 commit e732a78

File tree

6 files changed

+112
-35
lines changed

6 files changed

+112
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev-tools/omdb/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ crossterm.workspace = true
2222
crucible-agent-client.workspace = true
2323
crucible-pantry-client.workspace = true
2424
csv.workspace = true
25+
daft.workspace = true
2526
diesel.workspace = true
2627
dropshot.workspace = true
2728
dyn-clone.workspace = true

dev-tools/omdb/src/bin/omdb/nexus/chicken_switches.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::check_allow_destructive::DestructiveOperationToken;
99
use clap::ArgAction;
1010
use clap::Args;
1111
use clap::Subcommand;
12+
use daft::Diffable;
1213
use http::StatusCode;
1314
use indent_write::io::IndentWriter;
1415
use nexus_types::deployment::PlannerChickenSwitches;
@@ -169,7 +170,7 @@ async fn chicken_switches_set(
169170
args: &ChickenSwitchesSetArgs,
170171
_destruction_token: DestructiveOperationToken,
171172
) -> Result<(), anyhow::Error> {
172-
let switches = match client
173+
let (current_switches, new_switches) = match client
173174
.reconfigurator_chicken_switches_show_current()
174175
.await
175176
{
@@ -183,33 +184,57 @@ async fn chicken_switches_set(
183184
let switches = switches.into_inner();
184185
// Future switches should use the following pattern, and only update
185186
// the current switch values if a setting changed.
186-
let Some(switches) = args
187+
let Some(new_switches) = args
187188
.switches
188189
.update_if_modified(&switches.switches, next_version)
189190
else {
190-
println!("No modifications made to current switch values");
191+
println!("no modifications made to current switch values:");
192+
let stdout = io::stdout();
193+
let mut indented = IndentWriter::new(" ", stdout.lock());
194+
// No need for writeln! here because .display() adds its own
195+
// newlines.
196+
write!(indented, "{}", switches.display()).unwrap();
191197
return Ok(());
192198
};
193-
switches
199+
(Some(switches), new_switches)
194200
}
195201
Err(err) => {
196202
if err.status() == Some(StatusCode::NOT_FOUND) {
197203
let default_switches = ReconfiguratorChickenSwitches::default();
198204
// In this initial case, the operator expects that we always set
199205
// switches.
200-
ReconfiguratorChickenSwitchesParam {
206+
let new_switches = ReconfiguratorChickenSwitchesParam {
201207
version: 1,
202208
switches: args.switches.update(&default_switches),
203-
}
209+
};
210+
(None, new_switches)
204211
} else {
205212
eprintln!("error: {:#}", err);
206213
return Ok(());
207214
}
208215
}
209216
};
210217

211-
client.reconfigurator_chicken_switches_set(&switches).await?;
212-
println!("Chicken switches updated at version {}", switches.version);
218+
client.reconfigurator_chicken_switches_set(&new_switches).await?;
219+
println!("chicken switches updated to version {}:", new_switches.version);
220+
match current_switches {
221+
Some(current_switches) => {
222+
// ReconfiguratorChickenSwitchesDiffDisplay does its own
223+
// indentation, so more isn't required.
224+
print!(
225+
"{}",
226+
current_switches
227+
.switches
228+
.diff(&new_switches.switches)
229+
.display(),
230+
);
231+
}
232+
None => {
233+
let stdout = io::stdout();
234+
let mut indented = IndentWriter::new(" ", stdout.lock());
235+
write!(indented, "{}", new_switches.switches.display()).unwrap();
236+
}
237+
}
213238

214239
Ok(())
215240
}

dev-tools/omdb/tests/successes.out

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,10 @@ EXECUTING COMMAND: omdb ["-w", "nexus", "chicken-switches", "set", "--planner-en
14111411
termination: Exited(0)
14121412
---------------------------------------------
14131413
stdout:
1414-
Chicken switches updated at version 1
1414+
chicken switches updated to version 1:
1415+
planner enabled: true
1416+
planner switches:
1417+
add zones with mupdate override: true
14151418
---------------------------------------------
14161419
stderr:
14171420
note: using Nexus URL http://127.0.0.1:REDACTED_PORT/
@@ -1420,7 +1423,10 @@ EXECUTING COMMAND: omdb ["-w", "nexus", "chicken-switches", "set", "--add-zones-
14201423
termination: Exited(0)
14211424
---------------------------------------------
14221425
stdout:
1423-
Chicken switches updated at version 2
1426+
chicken switches updated to version 2:
1427+
planner enabled: true (unchanged)
1428+
planner switches:
1429+
* add zones with mupdate override: true -> false
14241430
---------------------------------------------
14251431
stderr:
14261432
note: using Nexus URL http://127.0.0.1:REDACTED_PORT/

nexus/types/src/deployment.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub use chicken_switches::PlannerChickenSwitches;
7979
pub use chicken_switches::PlannerChickenSwitchesDiff;
8080
pub use chicken_switches::PlannerChickenSwitchesDisplay;
8181
pub use chicken_switches::ReconfiguratorChickenSwitches;
82+
pub use chicken_switches::ReconfiguratorChickenSwitchesDiff;
83+
pub use chicken_switches::ReconfiguratorChickenSwitchesDiffDisplay;
8284
pub use chicken_switches::ReconfiguratorChickenSwitchesDisplay;
8385
pub use chicken_switches::ReconfiguratorChickenSwitchesParam;
8486
pub use chicken_switches::ReconfiguratorChickenSwitchesView;

nexus/types/src/deployment/chicken_switches.rs

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,38 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
//! Runtime configuration for reconfigurator
6-
//!
7-
use std::fmt;
6+
7+
use std::fmt::{self, Write};
88

99
use chrono::{DateTime, TimeZone, Utc};
1010
use daft::Diffable;
11+
use indent_write::fmt::IndentWriter;
1112
use schemars::JsonSchema;
1213
use serde::{Deserialize, Serialize};
1314

1415
use crate::deployment::blueprint_display::{BpDiffState, KvList, KvPair};
1516

17+
macro_rules! diff_row {
18+
($diff:expr, $label:expr) => {
19+
if $diff.before == $diff.after {
20+
KvPair::new(
21+
BpDiffState::Unchanged,
22+
$label,
23+
super::blueprint_display::linear_table_unchanged(&$diff.after),
24+
)
25+
} else {
26+
KvPair::new(
27+
BpDiffState::Modified,
28+
$label,
29+
super::blueprint_display::linear_table_modified(
30+
&$diff.before,
31+
&$diff.after,
32+
),
33+
)
34+
}
35+
};
36+
}
37+
1638
#[derive(
1739
Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
1840
)]
@@ -87,7 +109,15 @@ impl fmt::Display for ReconfiguratorChickenSwitchesViewDisplay<'_> {
87109
}
88110

89111
#[derive(
90-
Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
112+
Clone,
113+
Copy,
114+
Debug,
115+
Diffable,
116+
PartialEq,
117+
Eq,
118+
Serialize,
119+
Deserialize,
120+
JsonSchema,
91121
)]
92122
pub struct ReconfiguratorChickenSwitches {
93123
pub planner_enabled: bool,
@@ -134,6 +164,38 @@ impl fmt::Display for ReconfiguratorChickenSwitchesDisplay<'_> {
134164
}
135165
}
136166

167+
impl<'a> ReconfiguratorChickenSwitchesDiff<'a> {
168+
pub fn display(&self) -> ReconfiguratorChickenSwitchesDiffDisplay<'a, '_> {
169+
ReconfiguratorChickenSwitchesDiffDisplay { diff: self }
170+
}
171+
}
172+
173+
pub struct ReconfiguratorChickenSwitchesDiffDisplay<'a, 'b> {
174+
diff: &'b ReconfiguratorChickenSwitchesDiff<'a>,
175+
}
176+
177+
impl fmt::Display for ReconfiguratorChickenSwitchesDiffDisplay<'_, '_> {
178+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179+
let ReconfiguratorChickenSwitchesDiff {
180+
planner_enabled,
181+
planner_switches,
182+
} = self.diff;
183+
184+
let list = KvList::new(
185+
None,
186+
vec![diff_row!(planner_enabled, "planner enabled")],
187+
);
188+
// No need for writeln! here because KvList adds its own newlines.
189+
write!(f, "{list}")?;
190+
191+
let mut indented = IndentWriter::new(" ", f);
192+
writeln!(indented, "planner switches:")?;
193+
write!(indented, "{}", planner_switches.display())?;
194+
195+
Ok(())
196+
}
197+
}
198+
137199
#[derive(
138200
Clone,
139201
Copy,
@@ -229,28 +291,8 @@ pub struct PlannerChickenSwitchesDiffDisplay<'a, 'b> {
229291

230292
impl fmt::Display for PlannerChickenSwitchesDiffDisplay<'_, '_> {
231293
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232-
macro_rules! diff_row {
233-
($member:ident, $label:expr) => {
234-
if self.diff.$member.before == self.diff.$member.after {
235-
KvPair::new(
236-
BpDiffState::Unchanged,
237-
$label,
238-
super::blueprint_display::linear_table_unchanged(
239-
&self.diff.$member.after,
240-
),
241-
)
242-
} else {
243-
KvPair::new(
244-
BpDiffState::Modified,
245-
$label,
246-
super::blueprint_display::linear_table_modified(
247-
&self.diff.$member.before,
248-
&self.diff.$member.after,
249-
),
250-
)
251-
}
252-
};
253-
}
294+
let PlannerChickenSwitchesDiff { add_zones_with_mupdate_override } =
295+
self.diff;
254296

255297
let list = KvList::new(
256298
None,

0 commit comments

Comments
 (0)