Skip to content

Commit

Permalink
refactor: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Jan 21, 2024
1 parent c331ea4 commit d509dcd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 108 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions src/cli/commands/apply_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
102 changes: 4 additions & 98 deletions src/cli/commands/delete_data_related_to_cfs_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,23 @@ 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,
)
.await;

// 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)
});
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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::<Vec<&str>>();
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<String>)> = 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()
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/get_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/update_hsm_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/update_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit d509dcd

Please sign in to comment.