From 76bddf97151927081d290d5b124ef51c408a4fc7 Mon Sep 17 00:00:00 2001 From: Jakob Naucke Date: Thu, 20 Feb 2025 14:39:04 +0100 Subject: [PATCH] rust/pvapconfig: Add --unbind option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the --unbind option is enabled, run with an empty config instead, effectively unbinding all APQNs. Reviewed-by: Harald Freudenberger Acked-by: Steffen Eiden Signed-off-by: Jakob Naucke Signed-off-by: Jan Höppner --- rust/pvapconfig/man/pvapconfig.1 | 3 ++ rust/pvapconfig/src/cli.rs | 4 ++ rust/pvapconfig/src/config.rs | 1 + rust/pvapconfig/src/main.rs | 72 ++++++++++++++++++-------------- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/rust/pvapconfig/man/pvapconfig.1 b/rust/pvapconfig/man/pvapconfig.1 index e0bfe9bf..314c88fb 100644 --- a/rust/pvapconfig/man/pvapconfig.1 +++ b/rust/pvapconfig/man/pvapconfig.1 @@ -91,6 +91,9 @@ All AP config entries need to be satisfied to have pvapconfig terminate with success. Without this option one applied AP config entry is enough to meet the expectations. .TP 8 +.B \-\-unbind +Unbind all available APQNs. +.TP 8 .B \-v, \-\-verbose Print out informational messages about what pvapconfig is actually doing. diff --git a/rust/pvapconfig/src/cli.rs b/rust/pvapconfig/src/cli.rs index 3088ca17..32faaf45 100644 --- a/rust/pvapconfig/src/cli.rs +++ b/rust/pvapconfig/src/cli.rs @@ -29,6 +29,10 @@ pub struct Cli { #[arg(long = "strict")] pub strict: bool, + /// Unbind all available APQNs. + #[arg(long, conflicts_with_all = ["config", "strict"])] + pub unbind: bool, + /// Provide more detailed output. #[arg(short, long)] pub verbose: bool, diff --git a/rust/pvapconfig/src/config.rs b/rust/pvapconfig/src/config.rs index 11b2b9d9..289c1e8c 100644 --- a/rust/pvapconfig/src/config.rs +++ b/rust/pvapconfig/src/config.rs @@ -150,6 +150,7 @@ impl ApConfigEntry { } /// Wrapper object around Vector of ApConfigEntry +#[derive(Default)] pub struct ApConfigList(Vec); impl ApConfigList { diff --git a/rust/pvapconfig/src/main.rs b/rust/pvapconfig/src/main.rs index 95649f86..1e4a3cce 100644 --- a/rust/pvapconfig/src/main.rs +++ b/rust/pvapconfig/src/main.rs @@ -86,42 +86,48 @@ fn main() -> ExitCode { on_error_print_and_exit!(r); info!("UV support and environment is ok.\n"); - // read configuration - let configfile: &str = match &cli::ARGS.config { - Some(f) => f, - _ => cli::PATH_DEFAULT_CONFIG_FILE, - }; - info!( - "Reading AP configuration entries from file '{}'...\n", - configfile - ); - let apconfig: ApConfigList = match ApConfigList::read_and_validate_yaml_file(configfile) { - Ok(apcfg) => apcfg, - Err(err) => println_and_exit_failure!("{}", err), - }; - if apconfig.is_empty() { - println!( - "No AP configuration entries in config file '{}': Nothing to do.", + let mut apconfig: ApConfigList = Default::default(); + if !cli::ARGS.unbind { + // read configuration + let configfile: &str = match &cli::ARGS.config { + Some(f) => f, + _ => cli::PATH_DEFAULT_CONFIG_FILE, + }; + info!( + "Reading AP configuration entries from file '{}'...\n", configfile ); - return ExitCode::SUCCESS; - } - info!("Found {} AP configuration entries.\n", apconfig.len()); + apconfig = match ApConfigList::read_and_validate_yaml_file(configfile) { + Ok(apcfg) => apcfg, + Err(err) => println_and_exit_failure!("{}", err), + }; + if apconfig.is_empty() { + println!( + "No AP configuration entries in config file '{}': Nothing to do.", + configfile + ); + return ExitCode::SUCCESS; + } + info!("Found {} AP configuration entries.\n", apconfig.len()); + }; // get list of secrets from UV - info!("Fetching list of secrets from UV...\n"); - let secrets: SecretList = match uv::gather_secrets() { - Err(e) => println_and_exit_failure!("{}", e), - Ok(los) => los, - }; - info!("Fetched {} Secret entries from UV.\n", secrets.len()); + let mut secrets = SecretList::new(0, Vec::new()); + if !cli::ARGS.unbind { + info!("Fetching list of secrets from UV...\n"); + secrets = match uv::gather_secrets() { + Err(e) => println_and_exit_failure!("{}", e), + Ok(los) => los, + }; + info!("Fetched {} Secret entries from UV.\n", secrets.len()); + } // Warning if no UV secrets given but AP config entries require it let non_accel_apc = apconfig .iter() .filter(|apc| apc.mode != config::STR_MODE_ACCEL) .count(); - if non_accel_apc > 0 && secrets.is_empty() { + if !cli::ARGS.unbind && non_accel_apc > 0 && secrets.is_empty() { println!( "Warning: No UV Secrets given but at least one AP config entry requires a Secret." ); @@ -153,7 +159,7 @@ fn main() -> ExitCode { Ok(n) => n, }; - if n == 0 { + if !cli::ARGS.unbind && n == 0 { println_and_exit_failure!( "None out of {} AP config entries could be applied.", apconfig.len() @@ -166,11 +172,13 @@ fn main() -> ExitCode { ); } - info!( - "Successfully applied {} out of {} AP config entries.\n", - n, - apconfig.len() - ); + if !cli::ARGS.unbind { + info!( + "Successfully applied {} out of {} AP config entries.\n", + n, + apconfig.len() + ); + } ExitCode::SUCCESS }