manta_server/server/common/
ims_ops.rs

1//! IMS image helpers shared by handlers that need to locate or
2//! cross-reference images by CFS configuration name (e.g. boot-config
3//! application, SAT-file rendering).
4
5use manta_backend_dispatcher::{
6  error::Error,
7  interfaces::{cfs::CfsTrait, ims::ImsTrait},
8  types::ims::Image,
9};
10
11use crate::manta_backend_dispatcher::StaticBackendDispatcher;
12
13/// This function retrieves the list of image IDs related to a CFS configuration name.
14/// It first checks the CFS sessions for any succeeded sessions that built an image related to the
15/// CFS configuration. Then, it checks if the image ID exists in the IMS.
16pub async fn get_image_vec_related_cfs_configuration_name(
17  backend: &StaticBackendDispatcher,
18  shasta_token: &str,
19  shasta_base_url: &str,
20  shasta_root_cert: &[u8],
21  cfs_configuration_name: String,
22) -> Result<Vec<Image>, Error> {
23  tracing::info!(
24    "Searching in CFS sessions for image ID related to CFS configuration '{}'",
25    cfs_configuration_name
26  );
27
28  let cfs_session_vec = backend
29    .get_sessions(
30      shasta_token,
31      shasta_base_url,
32      shasta_root_cert,
33      None,
34      None,
35      None,
36      None,
37      None,
38      None,
39      None,
40      Some(true),
41      None,
42    )
43    .await?;
44
45  // Filter to sessions related to the CFS configuration that built an image
46  let cfs_session_image_succeeded_vec =
47    cfs_session_vec.iter().filter(|cfs_session| {
48      cfs_session
49        .get_configuration_name()
50        .is_some_and(|name| name.eq(&cfs_configuration_name))
51        && cfs_session
52          .get_target_def()
53          .is_some_and(|def| def.eq("image"))
54        && cfs_session.get_first_result_id().is_some()
55    });
56
57  let mut boot_image_id_vec = Vec::new();
58
59  for cfs_session in cfs_session_image_succeeded_vec {
60    let cfs_session_name = cfs_session.name.clone();
61
62    for image_id in cfs_session.get_result_id_vec() {
63      tracing::info!(
64        "Checking if result_id {} in CFS session {} exists",
65        image_id,
66        cfs_session_name
67      );
68
69      let image_vec_rslt =
70        backend.get_images(shasta_token, Some(&image_id)).await;
71
72      match image_vec_rslt {
73        Ok(mut image_vec) => {
74          tracing::info!(
75            "Found the image ID '{}' related to CFS sesison '{}'",
76            image_id,
77            cfs_session_name,
78          );
79
80          boot_image_id_vec.append(&mut image_vec);
81        }
82        Err(e) => {
83          tracing::warn!(
84            "Failed to fetch image '{}' for CFS session '{}': {}",
85            image_id,
86            cfs_session_name,
87            e
88          );
89        }
90      }
91    }
92  }
93
94  Ok(boot_image_id_vec)
95}