Skip to content

Commit

Permalink
feat: filter bos sessiontemplates now can filter by cfs configuration
Browse files Browse the repository at this point in the history
name
feat: fetch cfs configuration now takes includes desired configurations
in the result
refactor: refactor code
  • Loading branch information
ManuelSopenaBallesteros committed Dec 10, 2023
1 parent 0357aac commit b21572d
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 33 deletions.
98 changes: 97 additions & 1 deletion src/manta/bos/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde_json::Value;

pub fn get_image_id_from_bos_sessiontemplate_related_to_cfs_configuration(
/* pub fn get_image_id_from_bos_sessiontemplate_related_to_cfs_configuration(
bos_sessiontemplate_value_vec: &[Value],
// cfs_configuration: &str,
) -> Vec<String> {
Expand Down Expand Up @@ -55,4 +55,100 @@ pub fn get_image_id_from_bos_sessiontemplate_related_to_cfs_configuration(
image_id_compute_iter
.chain(image_id_uan_iter)
.collect::<Vec<String>>()
} */

pub fn get_image_id_from_bos_sessiontemplate_related_to_cfs_configuration(
bos_sessiontemplate_value_vec: &[Value],
) -> Vec<String> {
bos_sessiontemplate_value_vec
.into_iter()
.map(|bos_sessiontemplate_value| {
bos_sessiontemplate_value["boot_sets"]
.as_object()
.unwrap()
.into_iter()
.map(|(_, boot_set_param_value)| {
boot_set_param_value["path"]
.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
.to_string()
})
})
.flatten()
.collect()

/* let aux3: Vec<&str> = aux
.iter()
.copied()
.map(|boot_set_param_value| {
boot_set_param_value
.iter()
.map(|(_, boot_set_param_value)| {
boot_set_param_value["path"]
.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
})
})
.flatten()
.collect(); */

/* let image_id_compute_iter = bos_sessiontemplate_value_vec
.iter()
.filter(|bos_sessiontemplate| {
/* bos_sessiontemplate
.pointer("/cfs/configuration")
.unwrap()
.eq(cfs_configuration)
&& */
bos_sessiontemplate
.pointer("/boot_sets/compute/path")
.is_some()
})
.map(|bos_sessiontemplate| {
bos_sessiontemplate
.pointer("/boot_sets/compute/path")
.unwrap()
.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
.to_string()
});
let image_id_uan_iter = bos_sessiontemplate_value_vec
.iter()
.filter(|bos_sessiontemplate| {
/* bos_sessiontemplate
.pointer("/cfs/configuration")
.unwrap()
.eq(cfs_configuration)
&& */
bos_sessiontemplate.pointer("/boot_sets/uan/path").is_some()
})
.map(|bos_sessiontemplate| {
bos_sessiontemplate
.pointer("/boot_sets/uan/path")
.unwrap()
.as_str()
.unwrap()
.strip_prefix("s3://boot-images/")
.unwrap()
.strip_suffix("/manifest.json")
.unwrap()
.to_string()
});
let aux2 = image_id_compute_iter
.chain(image_id_uan_iter)
.collect::<Vec<String>>(); */
}
1 change: 1 addition & 0 deletions src/manta/cfs/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub async fn get_sessions(
hsm_group_name_vec,
None,
None,
None,
)
.await
.unwrap();
Expand Down
12 changes: 12 additions & 0 deletions src/mesa/bos/sessiontemplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ pub mod utils {
hsm_member_vec: &Vec<String>,
bos_sessiontemplate_name_opt: Option<&String>,
limit_number_opt: Option<&u8>,
cfs_configuration_name_opt: Option<&str>,
) -> Vec<SessionTemplate> {
// Filter by target (hsm group name or xnames)
bos_sessiontemplate_vec.retain(|bos_sessiontemplate| {
bos_sessiontemplate
.boot_sets
Expand All @@ -290,6 +292,16 @@ pub mod utils {
})
});

if let Some(cfs_configuration_name) = cfs_configuration_name_opt {
bos_sessiontemplate_vec.retain(|bos_sessiontemplate| {
bos_sessiontemplate.cfs.as_ref().is_some_and(|cfs| {
cfs.configuration
.as_ref()
.is_some_and(|configuration| configuration.eq(cfs_configuration_name))
})
})
}

if let Some(bos_sessiontemplate_name) = bos_sessiontemplate_name_opt {
bos_sessiontemplate_vec.retain(|bos_sessiontemplate| {
bos_sessiontemplate
Expand Down
90 changes: 71 additions & 19 deletions src/mesa/cfs/configuration/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ pub mod http_client {
use serde_json::Value;

use crate::shasta::cfs::configuration::{
http_client::{get_raw, put_raw}, r#struct::{get_put_payload::CfsConfigurationResponse, configuration::CfsConfigurationRequest},
http_client::{get_raw, put_raw},
r#struct::{
configuration::CfsConfigurationRequest, get_put_payload::CfsConfigurationResponse,
},
};

pub async fn get(
Expand Down Expand Up @@ -81,9 +84,18 @@ pub mod http_client {

pub mod utils {

use serde_json::Value;

use crate::{
common,
mesa::bos::sessiontemplate::utils::get_image_id_cfs_configuration_target_tuple_vec,
shasta::{self, cfs::configuration::r#struct::get_put_payload::CfsConfigurationResponse},
shasta::{
self,
cfs::{
component::http_client,
configuration::r#struct::get_put_payload::CfsConfigurationResponse,
},
},
};

pub async fn filter(
Expand All @@ -99,6 +111,36 @@ pub mod utils {
cfs_configuration_vec
.retain(|cfs_configuration| cfs_configuration.name.eq(cfs_configuration_name));
} else {
let cfs_components: Vec<Value> = if !hsm_group_name_vec.is_empty() {
let hsm_group_members = shasta::hsm::utils::get_member_vec_from_hsm_name_vec(
shasta_token,
shasta_base_url,
shasta_root_cert,
hsm_group_name_vec,
)
.await
.join(",");

// Note: nodes can be configured calling the component APi directly (bypassing BOS
// session API)
http_client::get_multiple_components(
shasta_token,
shasta_base_url,
shasta_root_cert,
Some(&hsm_group_members),
None,
)
.await
.unwrap()
} else {
Vec::new()
};

let desired_config: Vec<&str> = cfs_components
.iter()
.map(|cfs_component| cfs_component["desiredConfig"].as_str().unwrap())
.collect();

// We need BOS session templates to find an image created by SAT
let bos_sessiontemplate_value_vec = shasta::bos::template::http_client::filter(
shasta_token,
Expand All @@ -107,6 +149,7 @@ pub mod utils {
hsm_group_name_vec,
None,
None,
None,
)
.await
.unwrap();
Expand All @@ -129,27 +172,36 @@ pub mod utils {
.await
.unwrap();

let image_id_cfs_configuration_target_from_bos_sessiontemplate =
get_image_id_cfs_configuration_target_tuple_vec(bos_sessiontemplate_value_vec);

let image_id_cfs_configuration_target_from_cfs_session =
get_image_id_cfs_configuration_target_tuple_vec(cfs_session_value_vec);

let image_id_cfs_configuration_target = [
image_id_cfs_configuration_target_from_bos_sessiontemplate,
image_id_cfs_configuration_target_from_cfs_session,
let image_id_cfs_configuration_target_from_bos_sessiontemplate: Vec<(
String,
String,
Vec<String>,
)> = get_image_id_cfs_configuration_target_tuple_vec(bos_sessiontemplate_value_vec);

let image_id_cfs_configuration_target_from_cfs_session: Vec<(
String,
String,
Vec<String>,
)> = get_image_id_cfs_configuration_target_tuple_vec(cfs_session_value_vec);

let image_id_cfs_configuration_target: Vec<&str> = [
image_id_cfs_configuration_target_from_bos_sessiontemplate
.iter()
.map(|(_, config, _)| config.as_str())
.collect(),
image_id_cfs_configuration_target_from_cfs_session
.iter()
.map(|(_, config, _)| config.as_str())
.collect(),
desired_config,
]
.concat();

cfs_configuration_vec.retain(|cfs_configuration| {
hsm_group_name_vec.iter().any(|hsm_group| {
cfs_configuration.name.contains(hsm_group)
|| image_id_cfs_configuration_target
.iter()
.map(|(_, cfs_configuration, _)| cfs_configuration.clone())
.collect::<Vec<String>>()
.contains(&cfs_configuration.name)
})
hsm_group_name_vec
.iter()
.any(|hsm_group| cfs_configuration.name.contains(hsm_group))
|| image_id_cfs_configuration_target.contains(&cfs_configuration.name.as_str())
});

// println!("DEBUG - CFS session:\n{:#?}", cfs_session_vec);
Expand Down
1 change: 1 addition & 0 deletions src/mesa/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub async fn filter(
hsm_group_name_vec,
None,
None,
None,
)
.await
.unwrap();
Expand Down
70 changes: 57 additions & 13 deletions src/shasta/bos/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,38 @@ pub mod http_client {
shasta_base_url: &str,
shasta_root_cert: &[u8],
hsm_group_name_vec: &Vec<String>,
bos_template_name_opt: Option<&String>,
bos_sessiontemplate_name_opt: Option<&String>,
cfs_configuration_name_vec_opt: Option<Vec<&str>>,
limit_number_opt: Option<&u8>,
) -> Result<Vec<Value>, Box<dyn std::error::Error>> {
let mut cluster_bos_template = Vec::new();
// let mut cluster_bos_template = Vec::new();

let bos_template_vec: Vec<Value> = get_all(shasta_token, shasta_base_url, shasta_root_cert)
.await
.unwrap();
let mut bos_sessiontemplate_value_vec: Vec<Value> =
get_all(shasta_token, shasta_base_url, shasta_root_cert)
.await
.unwrap();

if !hsm_group_name_vec.is_empty() {
for bos_template in bos_template_vec.clone() {
bos_sessiontemplate_value_vec.retain(|bos_sessiontemplate_value| {
bos_sessiontemplate_value["boot_sets"]
.as_object()
.is_some_and(|boot_set_obj| {
boot_set_obj.iter().any(|(_property, boot_set_param)| {
boot_set_param["node_groups"]
.as_array()
.is_some_and(|node_group_vec| {
node_group_vec.iter().any(|node_group| {
hsm_group_name_vec
.contains(&node_group.as_str().unwrap().to_string())
})
})
})
})
});
}

/* if !hsm_group_name_vec.is_empty() {
for bos_template in bos_template_value_vec.clone() {
for (_, value) in bos_template["boot_sets"].as_object().unwrap() {
if value["node_groups"]
.as_array()
Expand All @@ -380,25 +401,48 @@ pub mod http_client {
}
}
}
} */

if let Some(cfs_configuration_name_vec) = cfs_configuration_name_vec_opt {
bos_sessiontemplate_value_vec.retain(|bos_sessiontemplate_value| {
cfs_configuration_name_vec.contains(
&bos_sessiontemplate_value
.pointer("/cfs/configuration")
.unwrap()
.as_str()
.unwrap(),
)
});
}

if let Some(bos_sessiontemplate_name) = bos_sessiontemplate_name_opt {
bos_sessiontemplate_value_vec.retain(|bos_sessiontemplate| {
bos_sessiontemplate["name"]
.as_str()
.unwrap()
.eq(bos_sessiontemplate_name)
});
}
if let Some(bos_template_name) = bos_template_name_opt {
for bos_template in bos_template_vec {

/* if let Some(bos_template_name) = bos_template_name_opt {
for bos_template in bos_template_value_vec {
if bos_template["name"].as_str().unwrap().eq(bos_template_name) {
cluster_bos_template.push(bos_template.clone());
}
}
}
} */

if let Some(limit_number) = limit_number_opt {
// Limiting the number of results to return to client

cluster_bos_template = cluster_bos_template[cluster_bos_template
.len()
.saturating_sub(*limit_number as usize)..]
bos_sessiontemplate_value_vec = bos_sessiontemplate_value_vec
[bos_sessiontemplate_value_vec
.len()
.saturating_sub(*limit_number as usize)..]
.to_vec();
}

Ok(cluster_bos_template)
Ok(bos_sessiontemplate_value_vec)
}

/// Get BOS session templates. Ref --> https://apidocs.svc.cscs.ch/paas/bos/operation/get_v1_sessiontemplates/
Expand Down

0 comments on commit b21572d

Please sign in to comment.