Skip to content

Commit

Permalink
refactor: add methods to fetch tuple with image id, cfs configuration
Browse files Browse the repository at this point in the history
and targets (either hsm group or nodes) from a list of jsons
  • Loading branch information
Manuel Sopena Ballesteros committed Dec 7, 2023
1 parent 9d831c4 commit d731603
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 113 deletions.
1 change: 0 additions & 1 deletion src/manta/cfs/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ pub async fn get_configuration(
most_recent_opt: Option<bool>,
limit_number_opt: Option<&u8>,
) -> Vec<Value> {

let cfs_configuration_value_vec = shasta::cfs::configuration::http_client::get_all(
shasta_token,
shasta_base_url,
Expand Down
65 changes: 65 additions & 0 deletions src/mesa/bos/sessiontemplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ pub mod http_client {

pub mod utils {
use comfy_table::Table;
use serde_json::Value;

use crate::common::node_ops;

Expand Down Expand Up @@ -311,6 +312,70 @@ pub mod utils {
bos_sessiontemplate_vec.to_vec()
}

pub fn get_image_id_cfs_configuration_target_tuple_vec(
bos_sessiontemplate_value_vec: Vec<Value>,
) -> Vec<(String, String, Vec<String>)> {
let mut image_id_cfs_configuration_from_bos_sessiontemplate: Vec<(
String,
String,
Vec<String>,
)> = Vec::new();

bos_sessiontemplate_value_vec
.iter()
.for_each(|bos_sessiontemplate| {
let cfs_configuration = bos_sessiontemplate
.pointer("/cfs/configuration")
.unwrap()
.as_str()
.unwrap()
.to_string();

for (_, boot_set) in bos_sessiontemplate
.pointer("/boot_sets")
.unwrap()
.as_object()
.unwrap()
{
let path = boot_set["path"]
.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
.to_string();

let target: Vec<String> = if let Some(node_groups) = boot_set.get("node_groups")
{
node_groups
.as_array()
.unwrap()
.into_iter()
.map(|node_group| node_group.as_str().unwrap().to_string())
.collect()
} else if let Some(node_list) = boot_set.get("node_list") {
node_list
.as_array()
.unwrap()
.into_iter()
.map(|target_group| target_group.as_str().unwrap().to_string())
.collect()
} else {
vec![]
};

image_id_cfs_configuration_from_bos_sessiontemplate.push((
path.to_string(),
cfs_configuration.to_string(),
target,
));
}
});

image_id_cfs_configuration_from_bos_sessiontemplate
}

pub fn print_table_struct(bos_sessiontemplate_vec: Vec<SessionTemplate>) {
let mut table = Table::new();

Expand Down
46 changes: 42 additions & 4 deletions src/mesa/cfs/configuration/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,58 @@ pub mod http_client {
}

pub mod utils {
use crate::mesa::cfs::configuration::get_put_payload::CfsConfigurationResponse;
use crate::{mesa::cfs::configuration::get_put_payload::CfsConfigurationResponse, shasta};

pub fn filter(
pub async fn filter(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
cfs_configuration_vec: &mut Vec<CfsConfigurationResponse>,
cfs_configuration_name_opt: Option<&String>,
hsm_group_name_vec: &Vec<String>,
limit_number_opt: Option<&u8>,
) -> Vec<CfsConfigurationResponse> {
// We need BOS session templates to find an image created by SAT
let bos_sessiontemplates_value_vec = shasta::bos::template::http_client::filter(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_group_name_vec,
None,
None,
)
.await
.unwrap();

/* println!(
"DEBUG - BOS sessiontemplate:\n{:#?}",
bos_sessiontemplates_value_vec
); */

// We need CFS sessions to find images without a BOS session template
let cfs_session_value_vec = shasta::cfs::session::http_client::filter(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_group_name_vec,
None,
None,
Some(true),
)
.await
.unwrap();

// println!("DEBUG - CFS session:\n{:#?}", cfs_session_vec);

if let Some(cfs_configuration_name) = cfs_configuration_name_opt {
cfs_configuration_vec
.retain(|cfs_configuration| cfs_configuration.name.eq(cfs_configuration_name));
}

cfs_configuration_vec.sort_by(|cfs_session_1, cfs_session_2| {
cfs_session_1.last_updated.cmp(&cfs_session_2.last_updated)
cfs_configuration_vec.sort_by(|cfs_configuration_1, cfs_configuration_2| {
cfs_configuration_1
.last_updated
.cmp(&cfs_configuration_2.last_updated)
});

if let Some(limit_number) = limit_number_opt {
Expand Down
1 change: 1 addition & 0 deletions src/mesa/cfs/session.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod get_response_struct;
pub mod http_client;
pub mod post_request_struct;
pub mod utils;
48 changes: 48 additions & 0 deletions src/mesa/cfs/session/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use serde_json::Value;

pub fn get_image_id_cfs_configuration_target_tuple_vec(
cfs_session_value_vec: Vec<Value>,
) -> Vec<(String, String, Vec<String>)> {
let mut image_id_cfs_configuration_target_from_cfs_session: Vec<(String, String, Vec<String>)> =
Vec::new();

cfs_session_value_vec
.iter()
.for_each(|cfs_session| {
if let Some(result_id) = cfs_session.pointer("/status/artifacts/0/result_id") {
let target: Vec<String> =
if let Some(target_groups) = cfs_session.pointer("/target/groups") {
target_groups
.as_array()
.unwrap()
.iter()
.map(|group| group["name"].as_str().unwrap().to_string())
.collect()
} else if let Some(ansible_limit) = cfs_session.pointer("/ansible/limit") {
ansible_limit
.as_array()
.unwrap()
.iter()
.map(|xname| xname.as_str().unwrap().to_string())
.collect()
} else {
vec![]
};

image_id_cfs_configuration_target_from_cfs_session.push((
result_id.as_str().unwrap().to_string(),
cfs_session
.pointer("/configuration/name")
.unwrap()
.as_str()
.unwrap()
.to_string(),
target,
));
} else {
image_id_cfs_configuration_target_from_cfs_session.push(("".to_string(), "".to_string(), vec![]));
}
});

image_id_cfs_configuration_target_from_cfs_session
}
131 changes: 23 additions & 108 deletions src/mesa/image.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::shasta::{self, ims::image::Image};

use super::{bos, cfs};

pub async fn filter(
shasta_token: &str,
shasta_base_url: &str,
Expand All @@ -22,7 +24,7 @@ pub async fn filter(
.unwrap();

// We need BOS session templates to find an image created by SAT
let bos_sessiontemplates_value_vec = shasta::bos::template::http_client::filter(
let bos_sessiontemplate_value_vec = shasta::bos::template::http_client::filter(
shasta_token,
shasta_base_url,
shasta_root_cert,
Expand All @@ -39,7 +41,7 @@ pub async fn filter(
); */

// We need CFS sessions to find images without a BOS session template
let cfs_session_vec = shasta::cfs::session::http_client::filter(
let cfs_session_value_vec = shasta::cfs::session::http_client::filter(
shasta_token,
shasta_base_url,
shasta_root_cert,
Expand All @@ -52,68 +54,13 @@ pub async fn filter(
.unwrap();

// println!("DEBUG - CFS session:\n{:#?}", cfs_session_vec);

let mut image_id_cfs_configuration_from_bos_sessiontemplate: Vec<(&str, &str, Vec<&str>)> =
bos_sessiontemplates_value_vec
.iter()
.map(|bos_sessiontemplate| {
if let Some(path) = bos_sessiontemplate.pointer("/boot_sets/compute/path") {
let target: Vec<&str> = if let Some(node_groups) =
bos_sessiontemplate.pointer("/boot_sets/compute/node_groups")
{
node_groups
.as_array()
.unwrap()
.into_iter()
.map(|target_group| target_group.as_str().unwrap())
.collect()
} else if let Some(node_list) =
bos_sessiontemplate.pointer("/boot_sets/compute/node_list")
{
node_list
.as_array()
.unwrap()
.into_iter()
.map(|target_group| target_group.as_str().unwrap())
.collect()
} else {
vec![]
};

(
path.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap(),
bos_sessiontemplate
.pointer("/cfs/configuration")
.unwrap()
.as_str()
.unwrap(),
target,
)
} else if let Some(path) = bos_sessiontemplate.pointer("/boot_sets/uan/path") {
(
path.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap(),
bos_sessiontemplate
.pointer("/cfs/configuration")
.unwrap()
.as_str()
.unwrap(),
vec![],
)
} else {
("", "", vec![])
}
})
.collect();
let mut image_id_cfs_configuration_from_bos_sessiontemplate: Vec<(
String,
String,
Vec<String>,
)> = bos::sessiontemplate::utils::get_image_id_cfs_configuration_target_tuple_vec(
bos_sessiontemplate_value_vec,
);

image_id_cfs_configuration_from_bos_sessiontemplate
.retain(|(image_id, _cfs_configuration, _hsm_groups)| !image_id.is_empty());
Expand All @@ -123,44 +70,8 @@ pub async fn filter(
image_id_cfs_configuration_from_bos_sessiontemplate
); */

let mut image_id_cfs_configuration_from_cfs_session_vec: Vec<(&str, &str, Vec<&str>)> =
cfs_session_vec
.iter()
.map(|cfs_session| {
if let Some(result_id) = cfs_session.pointer("/status/artifacts/0/result_id") {
let target: Vec<&str> =
if let Some(target_groups) = cfs_session.pointer("/target/groups") {
target_groups
.as_array()
.unwrap()
.iter()
.map(|group| group["name"].as_str().unwrap())
.collect()
} else if let Some(ansible_limit) = cfs_session.pointer("/ansible/limit") {
ansible_limit
.as_array()
.unwrap()
.iter()
.map(|xname| xname.as_str().unwrap())
.collect()
} else {
vec![]
};

(
result_id.as_str().unwrap(),
cfs_session
.pointer("/configuration/name")
.unwrap()
.as_str()
.unwrap(),
target,
)
} else {
("", "", vec![])
}
})
.collect();
let mut image_id_cfs_configuration_from_cfs_session_vec: Vec<(String, String, Vec<String>)> =
cfs::session::utils::get_image_id_cfs_configuration_target_tuple_vec(cfs_session_value_vec);

image_id_cfs_configuration_from_cfs_session_vec
.retain(|(image_id, _cfs_confguration, _hsm_groups)| !image_id.is_empty());
Expand All @@ -177,34 +88,38 @@ pub async fn filter(
for image in &image_vec {
let image_id = image.id.as_ref().unwrap();

let target_group_name_vec: Vec<&str>;
let cfs_configuration: &str;
let target_group_name_vec: Vec<String>;
let cfs_configuration: String;

if let Some(tuple) = image_id_cfs_configuration_from_bos_sessiontemplate
.iter()
.find(|tuple| tuple.0.eq(image_id))
{
cfs_configuration = tuple.1;
cfs_configuration = tuple.clone().1;
target_group_name_vec = tuple.2.clone();
} else if let Some(tuple) = image_id_cfs_configuration_from_cfs_session_vec
.iter()
.find(|tuple| tuple.0.eq(image_id))
{
cfs_configuration = tuple.1;
cfs_configuration = tuple.clone().1;
target_group_name_vec = tuple.2.clone();
} else if hsm_group_name_vec
.iter()
.any(|hsm_group_name| image.name.contains(hsm_group_name))
{
cfs_configuration = "";
cfs_configuration = "".to_string();
target_group_name_vec = vec![];
} else {
continue;
}

let target_groups = target_group_name_vec.join(", ");

image_detail_vec.push((image.clone(), cfs_configuration.to_string(), target_groups.clone()));
image_detail_vec.push((
image.clone(),
cfs_configuration.to_string(),
target_groups.clone(),
));
}

image_detail_vec
Expand Down

0 comments on commit d731603

Please sign in to comment.