From d509dcdae55f40af1f0d2903b875477490a84be9 Mon Sep 17 00:00:00 2001 From: Manuel Sopena Ballesteros Date: Sun, 21 Jan 2024 23:11:03 +0100 Subject: [PATCH] refactor: clean code --- Cargo.toml | 4 +- src/cli/commands/apply_cluster.rs | 11 +- ...elete_data_related_to_cfs_configuration.rs | 102 +----------------- src/cli/commands/get_configuration.rs | 2 +- src/cli/commands/update_hsm_group.rs | 2 +- src/cli/commands/update_node.rs | 2 +- 6 files changed, 15 insertions(+), 108 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 755c3074..eb6a73cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ publish = false # cargo dist --> Avoid publishing to crates.io # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -mesa = "0.23.6" -# mesa = { path = "../mesa" } # Only for development purposes +# mesa = "0.23.6" +mesa = { path = "../mesa" } # Only for development purposes strum = "0.25.0" strum_macros = "0.25" chrono = "0.4.31" diff --git a/src/cli/commands/apply_cluster.rs b/src/cli/commands/apply_cluster.rs index 21a6ec73..fbfabf50 100644 --- a/src/cli/commands/apply_cluster.rs +++ b/src/cli/commands/apply_cluster.rs @@ -86,11 +86,12 @@ pub async fn exec( ) .await; - let cfs_configuration = if let Ok(cfs_configuration_value) = cfs_configuration_value_rslt { - cfs_configuration_value - } else { - eprintln!("CFS configuration creation failed. Exit"); - std::process::exit(1); + let cfs_configuration = match cfs_configuration_value_rslt { + Ok(cfs_configuration_value) => cfs_configuration_value, + Err(error_message) => { + eprintln!("{}, Exit", error_message); + std::process::exit(1); + } }; let cfs_configuration_name = cfs_configuration.name.to_string(); diff --git a/src/cli/commands/delete_data_related_to_cfs_configuration.rs b/src/cli/commands/delete_data_related_to_cfs_configuration.rs index 0825c8d3..398b9ad4 100644 --- a/src/cli/commands/delete_data_related_to_cfs_configuration.rs +++ b/src/cli/commands/delete_data_related_to_cfs_configuration.rs @@ -75,7 +75,6 @@ pub async fn delete_data_related_cfs_configuration( shasta_base_url, shasta_root_cert, &mut cfs_configuration_vec, - None, &vec![hsm_group_name_opt.unwrap().to_string()], None, ) @@ -83,16 +82,16 @@ pub async fn delete_data_related_cfs_configuration( // Filter CFS configurations based on user input if let (Some(since), Some(until)) = (since_opt, until_opt) { - cfs_configuration_vec.retain(|cfs_configuration_value| { - let date = chrono::DateTime::parse_from_rfc3339(&cfs_configuration_value.last_updated) + cfs_configuration_vec.retain(|cfs_configuration| { + let date = chrono::DateTime::parse_from_rfc3339(&cfs_configuration.last_updated) .unwrap() .naive_utc(); since <= date && date < until }); } else if let Some(cfs_configuration_name) = cfs_configuration_name_opt { - cfs_configuration_vec.retain(|cfs_configuration_value| { - cfs_configuration_value + cfs_configuration_vec.retain(|cfs_configuration| { + cfs_configuration .name .eq_ignore_ascii_case(cfs_configuration_name) }); @@ -126,19 +125,6 @@ pub async fn delete_data_related_cfs_configuration( ) .await; - // Filter BOS sessiontemplates related to CFS configurations to be deleted - // - // Filter BOS sessiontemplate containing /cfs/configuration field - /* bos_sessiontemplate_value_vec.retain(|bos_sessiontemplate_value| { - cfs_configuration_name_vec.contains( - &bos_sessiontemplate_value - .pointer("/cfs/configuration") - .unwrap() - .as_str() - .unwrap(), - ) - }); */ - // Get CFS configurations related with BOS sessiontemplate let cfs_configuration_name_from_bos_sessiontemplate_value_iter = bos_sessiontemplate_value_vec .iter() @@ -454,86 +440,6 @@ pub async fn delete_data_related_cfs_configuration( bos_sessiontemplate_cfs_configuration_image_id_tuple_filtered_vec = Vec::new(); } - /* // VALIDATION - // - // Process CFS configurations to delete one by one - let mut cfs_configuration_to_keep_vec: Vec<&str> = Vec::new(); - for cfs_configuration_name in &cfs_configuration_name_vec { - // Check dessired configuration not using any CFS configuration to delete - let mut nodes_using_cfs_configuration_as_dessired_configuration_vec = - cfs_components - .iter() - .filter(|cfs_component| { - cfs_component["desiredConfig"] - .as_str() - .unwrap() - .eq(*cfs_configuration_name) - }) - .map(|cfs_component| cfs_component["id"].as_str().unwrap()) - .collect::>(); - - if !nodes_using_cfs_configuration_as_dessired_configuration_vec.is_empty() { - cfs_configuration_to_keep_vec.push(cfs_configuration_name); - - nodes_using_cfs_configuration_as_dessired_configuration_vec.sort(); - - eprintln!( - "CFS configuration {} can't be deleted. Reason:\nCFS configuration {} used as desired configuration for nodes: {}", - cfs_configuration_name, cfs_configuration_name, nodes_using_cfs_configuration_as_dessired_configuration_vec.join(", ")); - } - } - - // for cfs_configuration_name in &cfs_configuration_name_vec { - // Check images related to CFS configurations to delete are not used to boot nodes. For - // this we need to get images from both CFS session and BOS sessiontemplate because CSCS staff - let mut image_id_to_keep_vec: Vec<&str> = Vec::new(); - - // Check images related to CFS configurations to delete are not used to boot nodes. For - // this we need to get images from both CFS session and BOS sessiontemplate because CSCS staff - let mut boot_image_node_vec: Vec<(&str, Vec)> = Vec::new(); - - for image_id in &image_id_vec { - let nodes = get_node_vec_booting_image(image_id, &boot_param_vec); - - if !nodes.is_empty() { - boot_image_node_vec.push((image_id, nodes)); - } - } - - if !boot_image_node_vec.is_empty() { - // cfs_configuration_to_keep_vec.push(cfs_configuration_name); - - image_id_to_keep_vec.extend( - boot_image_node_vec - .iter() - .flat_map(|(_, nodes)| nodes) - .collect(), - ); - - eprintln!( - "Image based on CFS configuration {} can't be deleted. Reason:", - cfs_configuration_name - ); - for (image_id, node_vec) in boot_image_node_vec { - eprintln!("Image id {} used to boot nodes:\n{:?}", image_id, node_vec); - } - std::process::exit(1); - } - // } - - if !cfs_configuration_to_keep_vec.is_empty() || !image_id_to_keep_vec.is_empty() { - if *force { - cfs_configuration_name_vec.retain(|cfs_configuration_name| { - !cfs_configuration_to_keep_vec.contains(cfs_configuration_name) - }); - image_id_vec.retain(|image_id| !image_id_to_keep_vec.contains(image_id)); - } else { - // User don't want to force and there are cfs configurations or images used in the - // system. EXIT - std::process::exit(1); - } - } */ - // EVALUATE IF NEED TO CONTINUE. EXIT IF THERE IS NO DATA TO DELETE // if cfs_configuration_name_vec.is_empty() diff --git a/src/cli/commands/get_configuration.rs b/src/cli/commands/get_configuration.rs index 5ae08b47..aeda5085 100644 --- a/src/cli/commands/get_configuration.rs +++ b/src/cli/commands/get_configuration.rs @@ -24,7 +24,7 @@ pub async fn exec( shasta_token, shasta_base_url, shasta_root_cert, - configuration_name, + configuration_name.map(|elem| elem.as_str()), hsm_group_name_vec, limit, ) diff --git a/src/cli/commands/update_hsm_group.rs b/src/cli/commands/update_hsm_group.rs index 34048b31..707dd090 100644 --- a/src/cli/commands/update_hsm_group.rs +++ b/src/cli/commands/update_hsm_group.rs @@ -19,7 +19,7 @@ pub async fn exec( shasta_token, shasta_base_url, shasta_root_cert, - desired_configuration_opt, + desired_configuration_opt.map(|elem| elem.as_str()), ) .await; diff --git a/src/cli/commands/update_node.rs b/src/cli/commands/update_node.rs index e39dee62..fca5fec6 100644 --- a/src/cli/commands/update_node.rs +++ b/src/cli/commands/update_node.rs @@ -18,7 +18,7 @@ pub async fn exec( shasta_token, shasta_base_url, shasta_root_cert, - desired_configuration_opt, + desired_configuration_opt.map(|elem| elem.as_str()), ) .await;