Skip to content

Commit

Permalink
refactor: clean code based on clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Mar 1, 2024
1 parent dd4f585 commit d773ad4
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 137 deletions.
3 changes: 1 addition & 2 deletions src/bos/template/mesa/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bos/template/shasta/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>,
hsm_group_name_vec: &Vec<String>,
hsm_group_name_vec: &[String],
bos_sessiontemplate_name_opt: Option<&String>,
cfs_configuration_name_vec_opt: Option<Vec<&str>>,
limit_number_opt: Option<&u8>,
Expand Down
2 changes: 1 addition & 1 deletion src/bss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub mod http_client {
shasta_base_url: &str,
shasta_token: &str,
shasta_root_cert: &[u8],
xnames: &Vec<String>,
xnames: &[String],
params: Option<&String>,
kernel: Option<&String>,
initrd: Option<&String>,
Expand Down
2 changes: 1 addition & 1 deletion src/cfs/configuration/mesa/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
hsm_group_name_vec: &[String],
limit_number_opt: Option<&u8>,
) -> Vec<CfsConfigurationResponse> {
let mut cfs_configuration_value_vec: Vec<CfsConfigurationResponse> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions src/cfs/configuration/mesa/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn create(
shasta_token,
shasta_base_url,
shasta_root_cert,
&cfs_configuration,
cfs_configuration,
&cfs_configuration.name,
)
.await;
Expand All @@ -50,7 +50,7 @@ pub async fn filter(
shasta_base_url: &str,
shasta_root_cert: &[u8],
cfs_configuration_vec: &mut Vec<CfsConfigurationResponse>,
hsm_group_name_vec: &Vec<String>,
hsm_group_name_vec: &[String],
limit_number_opt: Option<&u8>,
) -> Vec<CfsConfigurationResponse> {
let cfs_components: Vec<Value> = if !hsm_group_name_vec.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions src/cfs/configuration/shasta/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

Expand Down
8 changes: 4 additions & 4 deletions src/cfs/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -880,7 +880,7 @@ pub mod mesa {
let target: Vec<String> = 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();

Expand Down Expand Up @@ -911,7 +911,7 @@ pub mod mesa {
let target: Vec<String> = 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();

Expand Down
5 changes: 4 additions & 1 deletion src/common/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ pub mod http_client {
shasta_root_cert: &[u8],
) -> Result<Value, Box<dyn Error>> {
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/{}",
Expand Down
98 changes: 8 additions & 90 deletions src/hsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()) */
}
}

Expand All @@ -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<String>,
new_target_hsm_group_members: &Vec<String>,
Expand All @@ -375,27 +305,21 @@ 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;
}
}

// 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;
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -1115,7 +1033,7 @@ pub mod hw_inventory {
.info
.as_ref()
.unwrap()
.split(" ")
.split(' ')
.collect::<Vec<_>>()
.first()
.unwrap()
Expand Down
16 changes: 5 additions & 11 deletions src/ims/image/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 10 additions & 18 deletions src/ims/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,14 +139,10 @@ pub async fn s3_get_object_size(
bucket: &str,
) -> Result<i64, Box<dyn Error>> {
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
Expand All @@ -169,7 +165,6 @@ pub async fn s3_download_object(
) -> Result<String, Box<dyn Error>> {
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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d773ad4

Please sign in to comment.