use crate::{
bos,
bss::http_client::get_multiple,
common,
error::Error,
hsm::group::utils::get_member_vec_from_hsm_name_vec,
ims::{self, image::http_client::types::Image},
};
pub async fn get_fuzzy(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
hsm_name_available_vec: &[String],
image_name_opt: Option<&str>,
limit_number_opt: Option<&u8>,
) -> Result<Vec<Image>, Error> {
let mut image_available_vec: Vec<Image> = get_image_available_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_name_available_vec,
None, )
.await?;
if let Some(image_name) = image_name_opt {
image_available_vec.retain(|image| image.name.contains(image_name));
}
if let Some(limit_number) = limit_number_opt {
image_available_vec = image_available_vec[image_available_vec
.len()
.saturating_sub(*limit_number as usize)..]
.to_vec();
}
Ok(image_available_vec.to_vec())
}
pub async fn get_by_name(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
hsm_name_available_vec: &[String],
image_name_opt: Option<&str>,
limit_number_opt: Option<&u8>,
) -> Result<Vec<Image>, Error> {
let mut image_available_vec: Vec<Image> = get_image_available_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_name_available_vec,
None, )
.await?;
if let Some(image_name) = image_name_opt {
image_available_vec.retain(|image| image.name.eq(image_name));
}
if let Some(limit_number) = limit_number_opt {
image_available_vec = image_available_vec[image_available_vec
.len()
.saturating_sub(*limit_number as usize)..]
.to_vec();
}
Ok(image_available_vec.to_vec())
}
pub fn filter(image_vec: &mut [Image]) {
image_vec.sort_by(|a, b| {
a.created.as_ref().unwrap().cmp(b.created.as_ref().unwrap())
});
}
pub async fn get_image_cfs_config_name_hsm_group_name(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
image_vec: &mut Vec<Image>,
hsm_group_name_vec: &[String],
limit_number_opt: Option<&u8>,
) -> Result<Vec<(Image, String, String, bool)>, Error> {
if let Some(limit_number) = limit_number_opt {
*image_vec = image_vec
[image_vec.len().saturating_sub(*limit_number as usize)..]
.to_vec();
}
let xname_vec = crate::hsm::group::utils::get_member_vec_from_hsm_name_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_group_name_vec,
)
.await?;
let mut bos_sessiontemplate_value_vec =
crate::bos::template::http_client::v2::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
)
.await?;
bos::template::utils::filter(
&mut bos_sessiontemplate_value_vec,
hsm_group_name_vec,
&xname_vec,
None,
);
let mut cfs_session_vec = crate::cfs::session::get_and_sort(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
None,
None,
None,
Some(true),
)
.await?;
crate::cfs::session::utils::filter_by_hsm(
shasta_token,
shasta_base_url,
shasta_root_cert,
&mut cfs_session_vec,
hsm_group_name_vec,
None,
common::jwt_ops::is_user_admin(shasta_token),
)
.await?;
let mut image_id_cfs_configuration_from_cfs_session: Vec<(String, String, Vec<String>)> =
crate::cfs::session::utils::get_image_id_cfs_configuration_target_for_existing_images_tuple_vec(
&cfs_session_vec,
);
image_id_cfs_configuration_from_cfs_session
.retain(|(image_id, _cfs_configuration, _hsm_groups)| !image_id.is_empty());
let hsm_member_vec = get_member_vec_from_hsm_name_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_group_name_vec,
)
.await?;
let boot_param_vec = get_multiple(
shasta_token,
shasta_base_url,
shasta_root_cert,
&hsm_member_vec,
)
.await
.unwrap_or_default();
let image_id_from_boot_params: Vec<String> = boot_param_vec
.iter()
.map(|boot_param| boot_param.get_boot_image())
.collect();
let mut image_detail_vec: Vec<(Image, String, String, bool)> = Vec::new();
for image in image_vec {
let image_id = image.id.as_ref().unwrap();
let target_group_name_vec: Vec<String>;
let cfs_configuration: String;
let target_groups: String;
let boot_image: bool;
if let Some(tuple) = image_id_cfs_configuration_from_cfs_session
.iter()
.find(|tuple| tuple.0.eq(image_id))
{
cfs_configuration = tuple.clone().1;
target_group_name_vec = tuple.2.clone();
target_groups = target_group_name_vec.join(", ");
} else if let Some(boot_params) = boot_param_vec
.iter()
.find(|boot_params| boot_params.get_boot_image().eq(image_id))
{
cfs_configuration = "Not found".to_string();
target_groups = boot_params.hosts.clone().join(",");
} else if hsm_group_name_vec
.iter()
.any(|hsm_group_name| image.name.contains(hsm_group_name))
{
cfs_configuration = "Not found".to_string();
target_groups = "Not found".to_string();
} else {
continue;
}
boot_image = if image_id_from_boot_params.contains(image_id) {
true
} else {
false
};
image_detail_vec.push((
image.clone(),
cfs_configuration.to_string(),
target_groups.clone(),
boot_image,
));
}
Ok(image_detail_vec)
}
pub async fn get_image_available_vec(
shasta_token: &str,
shasta_base_url: &str,
shasta_root_cert: &[u8],
hsm_name_available_vec: &[String],
limit_number_opt: Option<&u8>,
) -> Result<Vec<Image>, Error> {
let mut image_vec: Vec<Image> = super::http_client::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
)
.await?;
ims::image::utils::filter(&mut image_vec);
let mut bos_sessiontemplate_vec = bos::template::http_client::v2::get(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
)
.await?;
let xname_from_group_vec =
crate::hsm::group::utils::get_member_vec_from_hsm_name_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_name_available_vec,
)
.await?;
bos::template::utils::filter(
&mut bos_sessiontemplate_vec,
hsm_name_available_vec,
&xname_from_group_vec,
None,
);
let mut cfs_session_vec = crate::cfs::session::get_and_sort(
shasta_token,
shasta_base_url,
shasta_root_cert,
None,
None,
None,
None,
Some(true),
)
.await?;
crate::cfs::session::utils::filter_by_hsm(
shasta_token,
shasta_base_url,
shasta_root_cert,
&mut cfs_session_vec,
hsm_name_available_vec,
None,
true,
)
.await?;
let mut image_id_cfs_configuration_from_bos_sessiontemplate: Vec<(
String,
String,
Vec<String>,
)> = crate::bos::template::utils::get_image_id_cfs_configuration_target_tuple_vec(
bos_sessiontemplate_vec,
);
image_id_cfs_configuration_from_bos_sessiontemplate
.retain(|(image_id, _cfs_configuration, _hsm_groups)| !image_id.is_empty());
let mut image_id_cfs_configuration_from_cfs_session_vec: Vec<(String, String, Vec<String>)> =
crate::cfs::session::utils::get_image_id_cfs_configuration_target_for_existing_images_tuple_vec(
&cfs_session_vec,
);
image_id_cfs_configuration_from_cfs_session_vec
.retain(|(image_id, _cfs_confguration, _hsm_groups)| !image_id.is_empty());
let mut image_available_vec: Vec<Image> = Vec::new();
for image in &image_vec {
let image_id = image.id.as_ref().unwrap();
if image_id_cfs_configuration_from_bos_sessiontemplate
.iter()
.any(|tuple| tuple.0.eq(image_id))
{
image_available_vec.push(image.clone());
} else if image_id_cfs_configuration_from_cfs_session_vec
.iter()
.any(|tuple| tuple.0.eq(image_id))
{
image_available_vec.push(image.clone());
} else if hsm_name_available_vec
.iter()
.any(|hsm_group_name| image.name.contains(hsm_group_name))
{
image_available_vec.push(image.clone());
} else if image.name.to_lowercase().contains("generic") {
image_available_vec.push(image.clone())
} else {
continue;
}
}
if let Some(limit_number) = limit_number_opt {
image_available_vec = image_available_vec[image_available_vec
.len()
.saturating_sub(*limit_number as usize)..]
.to_vec();
}
Ok(image_available_vec)
}