|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! omdb commands for reconfigurator chicken switches |
| 6 | +
|
| 7 | +use crate::Omdb; |
| 8 | +use crate::check_allow_destructive::DestructiveOperationToken; |
| 9 | +use clap::Args; |
| 10 | +use clap::Subcommand; |
| 11 | +use http::StatusCode; |
| 12 | +use nexus_client::types::{ |
| 13 | + ReconfiguratorChickenSwitches, ReconfiguratorChickenSwitchesParam, |
| 14 | +}; |
| 15 | +use std::num::ParseIntError; |
| 16 | +use std::str::FromStr; |
| 17 | + |
| 18 | +#[derive(Debug, Args)] |
| 19 | +pub struct ChickenSwitchesArgs { |
| 20 | + #[command(subcommand)] |
| 21 | + command: ChickenSwitchesCommands, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Subcommand)] |
| 25 | +pub enum ChickenSwitchesCommands { |
| 26 | + /// Show a chicken switch at a given version |
| 27 | + Show(ChickenSwitchesShowArgs), |
| 28 | + |
| 29 | + /// Set the value of all chicken switches for the latest version |
| 30 | + /// Values carry over from the latest version if unset on the CLI. |
| 31 | + Set(ChickenSwitchesSetArgs), |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug, Clone, Args)] |
| 35 | +pub struct ChickenSwitchesSetArgs { |
| 36 | + planner_enabled: bool, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug, Clone, Copy, Args)] |
| 40 | +pub struct ChickenSwitchesShowArgs { |
| 41 | + version: ChickenSwitchesVersionOrCurrent, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Debug, Clone, Copy)] |
| 45 | +pub enum ChickenSwitchesVersionOrCurrent { |
| 46 | + Current, |
| 47 | + Version(u32), |
| 48 | +} |
| 49 | + |
| 50 | +impl FromStr for ChickenSwitchesVersionOrCurrent { |
| 51 | + type Err = ParseIntError; |
| 52 | + |
| 53 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 54 | + if matches!(s, "current" | "latest") { |
| 55 | + Ok(Self::Current) |
| 56 | + } else { |
| 57 | + let version = s.parse()?; |
| 58 | + Ok(Self::Version(version)) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub async fn cmd_nexus_chicken_switches( |
| 64 | + omdb: &Omdb, |
| 65 | + client: &nexus_client::Client, |
| 66 | + args: &ChickenSwitchesArgs, |
| 67 | +) -> Result<(), anyhow::Error> { |
| 68 | + match &args.command { |
| 69 | + ChickenSwitchesCommands::Show(version) => { |
| 70 | + chicken_switches_show(&client, version).await |
| 71 | + } |
| 72 | + ChickenSwitchesCommands::Set(args) => { |
| 73 | + let token = omdb.check_allow_destructive()?; |
| 74 | + chicken_switches_set(&client, args, token).await |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +async fn chicken_switches_show( |
| 79 | + client: &nexus_client::Client, |
| 80 | + args: &ChickenSwitchesShowArgs, |
| 81 | +) -> Result<(), anyhow::Error> { |
| 82 | + let res = match args.version { |
| 83 | + ChickenSwitchesVersionOrCurrent::Current => { |
| 84 | + client.reconfigurator_chicken_switches_show_current().await |
| 85 | + } |
| 86 | + ChickenSwitchesVersionOrCurrent::Version(version) => { |
| 87 | + client.reconfigurator_chicken_switches_show(version).await |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + match res { |
| 92 | + Ok(switches) => { |
| 93 | + let ReconfiguratorChickenSwitches { |
| 94 | + version, |
| 95 | + planner_enabled, |
| 96 | + time_modified, |
| 97 | + } = switches.into_inner(); |
| 98 | + println!("Reconfigurator Chicken Switches: "); |
| 99 | + println!(" version: {version}"); |
| 100 | + println!(" modified time: {time_modified}"); |
| 101 | + println!(" planner enabled: {planner_enabled}"); |
| 102 | + } |
| 103 | + Err(err) => eprintln!("error: {:#}", err), |
| 104 | + } |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +async fn chicken_switches_set( |
| 110 | + client: &nexus_client::Client, |
| 111 | + args: &ChickenSwitchesSetArgs, |
| 112 | + _destruction_token: DestructiveOperationToken, |
| 113 | +) -> Result<(), anyhow::Error> { |
| 114 | + let switches = match client |
| 115 | + .reconfigurator_chicken_switches_show_current() |
| 116 | + .await |
| 117 | + { |
| 118 | + Ok(switches) => { |
| 119 | + let Some(version) = switches.version.checked_add(1) else { |
| 120 | + eprintln!( |
| 121 | + "ERROR: Failed to update chicken switches. Max version reached." |
| 122 | + ); |
| 123 | + return Ok(()); |
| 124 | + }; |
| 125 | + let switches = switches.into_inner(); |
| 126 | + // Future switches should use the following pattern, and only update |
| 127 | + // the current switch values if a setting changed. |
| 128 | + // |
| 129 | + // We may want to use `Options` in `args` to allow defaulting to |
| 130 | + // the current setting rather than forcing the user to update all |
| 131 | + // settings if the number of switches grows significantly. However, |
| 132 | + // this will not play nice with the `NOT_FOUND` case below. |
| 133 | + let mut modified = false; |
| 134 | + if args.planner_enabled != switches.planner_enabled { |
| 135 | + modified = true; |
| 136 | + } |
| 137 | + if modified { |
| 138 | + ReconfiguratorChickenSwitchesParam { |
| 139 | + version, |
| 140 | + planner_enabled: args.planner_enabled, |
| 141 | + } |
| 142 | + } else { |
| 143 | + println!("No modifications made to current switch values"); |
| 144 | + return Ok(()); |
| 145 | + } |
| 146 | + } |
| 147 | + Err(err) => { |
| 148 | + if err.status() == Some(StatusCode::NOT_FOUND) { |
| 149 | + ReconfiguratorChickenSwitchesParam { |
| 150 | + version: 1, |
| 151 | + planner_enabled: args.planner_enabled, |
| 152 | + } |
| 153 | + } else { |
| 154 | + eprintln!("error: {:#}", err); |
| 155 | + return Ok(()); |
| 156 | + } |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + client.reconfigurator_chicken_switches_set(&switches).await?; |
| 161 | + println!("Chicken switches updated at version {}", switches.version); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
0 commit comments