From d773ad4b6ee47a8753064b35168b263f6e75502f Mon Sep 17 00:00:00 2001 From: Manuel Sopena Ballesteros Date: Fri, 1 Mar 2024 23:59:07 +0100 Subject: [PATCH] refactor: clean code based on clippy suggestions --- src/bos/template/mesa/utils.rs | 3 +- src/bos/template/shasta/utils.rs | 2 +- src/bss.rs | 2 +- src/cfs/configuration/mesa/http_client.rs | 2 +- .../mesa/struct/cfs_configuration_request.rs | 2 +- src/cfs/configuration/mesa/utils.rs | 4 +- src/cfs/configuration/shasta/utils.rs | 8 +- src/cfs/session.rs | 8 +- src/common/gitea.rs | 5 +- src/hsm.rs | 98 ++----------------- src/ims/image/utils.rs | 16 +-- src/ims/s3.rs | 28 ++---- src/node/utils.rs | 2 +- 13 files changed, 43 insertions(+), 137 deletions(-) diff --git a/src/bos/template/mesa/utils.rs b/src/bos/template/mesa/utils.rs index a084d3f..b81adc3 100644 --- a/src/bos/template/mesa/utils.rs +++ b/src/bos/template/mesa/utils.rs @@ -160,8 +160,7 @@ pub fn find_bos_sessiontemplate_related_to_image_id( .find(|bos_sessiontemplate| { bos_sessiontemplate .get_path() - .iter() - .next() + .first() .unwrap() .contains(image_id) /* bos_sessiontemplate diff --git a/src/bos/template/shasta/utils.rs b/src/bos/template/shasta/utils.rs index 2ee5de3..a484e0c 100644 --- a/src/bos/template/shasta/utils.rs +++ b/src/bos/template/shasta/utils.rs @@ -5,7 +5,7 @@ use crate::bos::template::mesa::r#struct::response_payload::BosSessionTemplate; /// Get BOS session templates. Ref --> https://apidocs.svc.cscs.ch/paas/bos/operation/get_v1_sessiontemplates/ pub async fn filter( bos_sessiontemplate_value_vec: &mut Vec, - hsm_group_name_vec: &Vec, + hsm_group_name_vec: &[String], bos_sessiontemplate_name_opt: Option<&String>, cfs_configuration_name_vec_opt: Option>, limit_number_opt: Option<&u8>, diff --git a/src/bss.rs b/src/bss.rs index b9ab0e6..b1be4f1 100644 --- a/src/bss.rs +++ b/src/bss.rs @@ -57,7 +57,7 @@ pub mod http_client { shasta_base_url: &str, shasta_token: &str, shasta_root_cert: &[u8], - xnames: &Vec, + xnames: &[String], params: Option<&String>, kernel: Option<&String>, initrd: Option<&String>, diff --git a/src/cfs/configuration/mesa/http_client.rs b/src/cfs/configuration/mesa/http_client.rs index 7f6fb69..7a6dcb4 100644 --- a/src/cfs/configuration/mesa/http_client.rs +++ b/src/cfs/configuration/mesa/http_client.rs @@ -46,7 +46,7 @@ pub async fn get_and_filter( shasta_base_url: &str, shasta_root_cert: &[u8], configuration_name: Option<&str>, - hsm_group_name_vec: &Vec, + hsm_group_name_vec: &[String], limit_number_opt: Option<&u8>, ) -> Vec { let mut cfs_configuration_value_vec: Vec = diff --git a/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs b/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs index 014a352..1b35645 100644 --- a/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs +++ b/src/cfs/configuration/mesa/struct/cfs_configuration_request.rs @@ -105,7 +105,7 @@ impl CfsConfigurationRequest { let tag_details = gitea::http_client::get_tag_details( &repo_url, - &git_tag, + git_tag, gitea_token, shasta_root_cert, ) diff --git a/src/cfs/configuration/mesa/utils.rs b/src/cfs/configuration/mesa/utils.rs index 58f2cde..aef867a 100644 --- a/src/cfs/configuration/mesa/utils.rs +++ b/src/cfs/configuration/mesa/utils.rs @@ -27,7 +27,7 @@ pub async fn create( shasta_token, shasta_base_url, shasta_root_cert, - &cfs_configuration, + cfs_configuration, &cfs_configuration.name, ) .await; @@ -50,7 +50,7 @@ pub async fn filter( shasta_base_url: &str, shasta_root_cert: &[u8], cfs_configuration_vec: &mut Vec, - hsm_group_name_vec: &Vec, + hsm_group_name_vec: &[String], limit_number_opt: Option<&u8>, ) -> Vec { let cfs_components: Vec = if !hsm_group_name_vec.is_empty() { diff --git a/src/cfs/configuration/shasta/utils.rs b/src/cfs/configuration/shasta/utils.rs index d03e702..ecc0969 100644 --- a/src/cfs/configuration/shasta/utils.rs +++ b/src/cfs/configuration/shasta/utils.rs @@ -75,14 +75,14 @@ pub async fn filter( let boot_set_node_groups_vec = bos_sessiontemplate.get_target_hsm(); let boot_set_node_list_vec = bos_sessiontemplate.get_target_xname(); - boot_set_node_groups_vec.len() > 0 + !boot_set_node_groups_vec.is_empty() && boot_set_node_groups_vec .iter() - .all(|node_group| hsm_group_name_vec.contains(&node_group)) - || boot_set_node_list_vec.len() > 0 + .all(|node_group| hsm_group_name_vec.contains(node_group)) + || !boot_set_node_list_vec.is_empty() && boot_set_node_list_vec .iter() - .all(|xname| hsm_group_member_vec.contains(&xname)) + .all(|xname| hsm_group_member_vec.contains(xname)) }) .collect::>(); diff --git a/src/cfs/session.rs b/src/cfs/session.rs index 365da60..53ee067 100644 --- a/src/cfs/session.rs +++ b/src/cfs/session.rs @@ -773,7 +773,7 @@ pub mod mesa { cfs_session.get_target_hsm().is_some_and(|target_hsm_vec| { target_hsm_vec .iter() - .any(|target_hsm| hsm_group_name_vec.contains(&target_hsm)) + .any(|target_hsm| hsm_group_name_vec.contains(target_hsm)) }) || cfs_session .get_target_xname() .is_some_and(|target_xname_vec| { @@ -816,7 +816,7 @@ pub mod mesa { cfs_session.get_target_hsm().is_some_and(|target_hsm_vec| { target_hsm_vec .iter() - .any(|target_hsm| hsm_group_name_vec.contains(&target_hsm)) + .any(|target_hsm| hsm_group_name_vec.contains(target_hsm)) }) || cfs_session .get_target_xname() .is_some_and(|target_xname_vec| { @@ -880,7 +880,7 @@ pub mod mesa { let target: Vec = cfs_session .get_target_hsm() .or_else(|| cfs_session.get_target_xname()) - .unwrap_or(Vec::new()); + .unwrap_or_default(); let cfs_configuration = cfs_session.get_configuration_name().unwrap(); @@ -911,7 +911,7 @@ pub mod mesa { let target: Vec = cfs_session .get_target_hsm() .or_else(|| cfs_session.get_target_xname()) - .unwrap_or(Vec::new()); + .unwrap_or_default(); let cfs_configuration = cfs_session.get_configuration_name().unwrap(); diff --git a/src/common/gitea.rs b/src/common/gitea.rs index 9eb786c..d5773c4 100644 --- a/src/common/gitea.rs +++ b/src/common/gitea.rs @@ -148,7 +148,10 @@ pub mod http_client { shasta_root_cert: &[u8], ) -> Result> { let repo_name: &str = gitea_api_tag_url - .trim_start_matches("https://vcs.cmn.alps.cscs.ch/vcs/api/v1/repos/cray/").split("/").next().unwrap(); + .trim_start_matches("https://vcs.cmn.alps.cscs.ch/vcs/api/v1/repos/cray/") + .split('/') + .next() + .unwrap(); let api_url = format!( "https://api.cmn.alps.cscs.ch/vcs/api/v1/repos/cray/{}/tags/{}", diff --git a/src/hsm.rs b/src/hsm.rs index 5c7d719..925c27f 100644 --- a/src/hsm.rs +++ b/src/hsm.rs @@ -262,88 +262,21 @@ pub mod group { } pub async fn post_member( - shasta_token: &str, - shasta_base_url: &str, - shasta_root_cert: &[u8], hsm_group_name: &str, member_id: &str, ) -> Result<(), reqwest::Error> { log::info!("DEBUG - ADD member {}/{}", hsm_group_name, member_id); Ok(()) - - /* let client_builder = reqwest::Client::builder() - .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?); - - // Build client - let client = if let Ok(socks5_env) = std::env::var("SOCKS5") { - // socks5 proxy - log::debug!("SOCKS5 enabled"); - let socks5proxy = reqwest::Proxy::all(socks5_env)?; - - // rest client to authenticate - client_builder.proxy(socks5proxy).build()? - } else { - client_builder.build()? - }; - - let api_url = format!( - "{}/smd/hsm/v2/groups/{}/members", - shasta_base_url, hsm_group_name - ); - - let _ = client - .post(api_url) - .bearer_auth(shasta_token) - .json(&serde_json::json!({ - "id": member_id - })) - .send() - .await? - .error_for_status(); - - Ok(()) */ } pub async fn delete_member( - shasta_token: &str, - shasta_base_url: &str, - shasta_root_cert: &[u8], hsm_group_name: &str, member_id: &str, ) -> Result<(), reqwest::Error> { log::info!("DEBUG - DELETE member {}/{}", hsm_group_name, member_id); Ok(()) - - /* let client_builder = reqwest::Client::builder() - .add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?); - - // Build client - let client = if let Ok(socks5_env) = std::env::var("SOCKS5") { - // socks5 proxy - log::debug!("SOCKS5 enabled"); - let socks5proxy = reqwest::Proxy::all(socks5_env)?; - - // rest client to authenticate - client_builder.proxy(socks5proxy).build()? - } else { - client_builder.build()? - }; - - let api_url = format!( - "{}/smd/hsm/v2/groups/{}/members/{}", - shasta_base_url, hsm_group_name, member_id - ); - - let _ = client - .delete(api_url) - .bearer_auth(shasta_token) - .send() - .await? - .error_for_status(); - - Ok(()) */ } } @@ -361,9 +294,6 @@ pub mod group { use super::http_client::{self, delete_member}; pub async fn update_hsm_group_members( - shasta_token: &str, - shasta_base_url: &str, - shasta_root_cert: &[u8], hsm_group_name: &str, old_target_hsm_group_members: &Vec, new_target_hsm_group_members: &Vec, @@ -375,13 +305,10 @@ pub mod group { // Delete members for old_member in old_target_hsm_group_members { - if !new_target_hsm_group_members.contains(&old_member) { + if !new_target_hsm_group_members.contains(old_member) { let _ = delete_member( - shasta_token, - shasta_base_url, - shasta_root_cert, hsm_group_name, - &old_member, + old_member, ) .await; } @@ -389,13 +316,10 @@ pub mod group { // Add members for new_member in new_target_hsm_group_members { - if !old_target_hsm_group_members.contains(&new_member) { + if !old_target_hsm_group_members.contains(new_member) { let _ = post_member( - shasta_token, - shasta_base_url, - shasta_root_cert, hsm_group_name, - &new_member, + new_member, ) .await; } @@ -751,17 +675,11 @@ pub mod group { .send() .await?; - let json_response: Value; - - if resp.status().is_success() { - json_response = serde_json::from_str(&resp.text().await?)?; + let json_response: Value = if resp.status().is_success() { + serde_json::from_str(&resp.text().await?)? } else { println!("Return code: {}\n", resp.status()); - if resp.status().to_string().to_lowercase().contains("409") { - return Err(resp.text().await?.into()); - } else { - return Err(resp.text().await?.into()); // Black magic conversion from Err(Box::new("my error msg")) which does not - } + return Err(resp.text().await?.into()); }; Ok(json_response.as_array().unwrap().to_owned()) @@ -1115,7 +1033,7 @@ pub mod hw_inventory { .info .as_ref() .unwrap() - .split(" ") + .split(' ') .collect::>() .first() .unwrap() diff --git a/src/ims/image/utils.rs b/src/ims/image/utils.rs index fac2ade..be23474 100644 --- a/src/ims/image/utils.rs +++ b/src/ims/image/utils.rs @@ -176,21 +176,15 @@ pub async fn filter( // Image details in BOS session template cfs_configuration = tuple.clone().1; target_group_name_vec = tuple.2.clone(); - } else if image_id_from_boot_params.contains(image_id) { + } else if image_id_from_boot_params.contains(image_id) + || hsm_group_name_vec + .iter() + .any(|hsm_group_name| image.name.contains(hsm_group_name)) + { // Image details where image is found in a node boot param related to HSM we are // working with cfs_configuration = "Not found".to_string(); target_group_name_vec = vec![]; - } else if hsm_group_name_vec - .iter() - .any(|hsm_group_name| image.name.contains(hsm_group_name)) - { - // Image details where the image name contains the HSM group name we are filtering (This - // is a bad practice hence image name is a free text and user may make mistakes typing - // it but CSCS staff deletes the CFS sessions therefore we should do this to fetch as - // much related images as we can) - cfs_configuration = "Not found".to_string(); - target_group_name_vec = vec![]; } else { continue; } diff --git a/src/ims/s3.rs b/src/ims/s3.rs index 9177bbb..c3878f8 100644 --- a/src/ims/s3.rs +++ b/src/ims/s3.rs @@ -9,7 +9,7 @@ use serde_json::Value; use anyhow::Result; use aws_sdk_s3::{primitives::ByteStream, Client}; -use indicatif::{ProgressBar,ProgressStyle}; +use indicatif::{ProgressBar, ProgressStyle}; pub const BAR_FORMAT: &str = "[{elapsed_precise}] {bar:40.cyan/blue} ({bytes_per_sec}) {bytes:>7}/{total_bytes:7} {msg} [ETA {eta}]"; // Get a token for S3 and return the result @@ -139,14 +139,10 @@ pub async fn s3_get_object_size( bucket: &str, ) -> Result> { let client = setup_client(sts_value).await; - let _object = match client.get_object() - .bucket(bucket) - .key(key) - .send() - .await { - Ok(object) => return Ok(object.content_length().clone().unwrap()), - Err(e) => panic!("Error, unable to get object size from s3. Error msg: {}", e) - }; + match client.get_object().bucket(bucket).key(key).send().await { + Ok(object) => Ok(object.content_length().unwrap()), + Err(e) => panic!("Error, unable to get object size from s3. Error msg: {}", e), + } } /// Gets an object from S3 @@ -169,7 +165,6 @@ pub async fn s3_download_object( ) -> Result> { let client = setup_client(sts_value).await; - let filename = Path::new(object_path).file_name().unwrap(); let file_path = Path::new(destination_path).join(filename); log::debug!("Create directory '{}'", destination_path); @@ -200,7 +195,7 @@ pub async fn s3_download_object( .send() .await?; - let bar_size = object.content_length().clone().unwrap(); + let bar_size = object.content_length().unwrap(); let bar = ProgressBar::new(bar_size as u64); bar.set_style(ProgressStyle::with_template(BAR_FORMAT).unwrap()); @@ -232,7 +227,7 @@ pub async fn s3_upload_object( let body = ByteStream::from_path(Path::new(&file_path)).await; - let put_object_output = match client + match client .put_object() .bucket(bucket) .key(object_path) @@ -242,11 +237,10 @@ pub async fn s3_upload_object( { Ok(put_object_output) => { log::debug!("Uploaded file '{}' successfully", &file_path); - return Ok(put_object_output.e_tag.clone().unwrap()) + Ok(put_object_output.e_tag.unwrap()) } Err(error) => panic!("Error uploading file {}: {}", &file_path, error), - // - }; + } } /// Removes an object from S3 @@ -321,9 +315,7 @@ pub async fn s3_multipart_upload_object( // Get details of the upload, this is needed because multipart uploads // are tricky and have a minimum chunk size of 5MB let path = Path::new(&file_path); - let file_size = std::fs::metadata(path) - .expect("it exists I swear") - .len(); + let file_size = std::fs::metadata(path).expect("it exists I swear").len(); let mut chunk_count = (file_size / CHUNK_SIZE) + 1; let mut size_of_last_chunk = file_size % CHUNK_SIZE; diff --git a/src/node/utils.rs b/src/node/utils.rs index 7356f84..bcdd38f 100644 --- a/src/node/utils.rs +++ b/src/node/utils.rs @@ -103,7 +103,7 @@ pub async fn get_node_details( .unwrap(); // Get BOS session template - let bos_sessiontemplate_vec = crate::bos::template::mesa::http_client::get_all( + let _ = crate::bos::template::mesa::http_client::get_all( shasta_token, shasta_base_url, shasta_root_cert,