manta_server/service/
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::server::common::app_context::InfraContext;
12
13/// Return the IMS images produced by succeeded image-build CFS
14/// sessions that referenced `cfs_configuration_name`.
15///
16/// The CFS session list is filtered to entries whose configuration
17/// matches, whose target definition is `"image"`, and which carry at
18/// least one `result_id`. For each matching session every result id
19/// is looked up in IMS; misses are logged and skipped so a partially
20/// garbage-collected IMS doesn't break callers that just want
21/// whatever images still exist (boot-config application, SAT-file
22/// rendering, etc.).
23pub async fn get_image_vec_related_cfs_configuration_name(
24  infra: &InfraContext<'_>,
25  shasta_token: &str,
26  cfs_configuration_name: String,
27) -> Result<Vec<Image>, Error> {
28  tracing::info!(
29    "Searching in CFS sessions for image ID related to CFS configuration '{}'",
30    cfs_configuration_name
31  );
32
33  let cfs_session_vec = infra
34    .backend
35    .get_sessions(
36      shasta_token,
37      None,
38      None,
39      None,
40      None,
41      None,
42      None,
43      None,
44      Some(true),
45      None,
46    )
47    .await?;
48
49  // Filter to sessions related to the CFS configuration that built an image
50  let cfs_session_image_succeeded_vec =
51    cfs_session_vec.iter().filter(|cfs_session| {
52      cfs_session
53        .get_configuration_name()
54        .is_some_and(|name| name.eq(&cfs_configuration_name))
55        && cfs_session
56          .get_target_def()
57          .is_some_and(|def| def.eq("image"))
58        && cfs_session.get_first_result_id().is_some()
59    });
60
61  let mut boot_image_id_vec = Vec::new();
62
63  for cfs_session in cfs_session_image_succeeded_vec {
64    let cfs_session_name = cfs_session.name.clone();
65
66    for image_id in cfs_session.get_result_id_vec() {
67      tracing::info!(
68        "Checking if result_id {} in CFS session {} exists",
69        image_id,
70        cfs_session_name
71      );
72
73      let image_vec_rslt = infra
74        .backend
75        .get_images(shasta_token, Some(&image_id))
76        .await;
77
78      match image_vec_rslt {
79        Ok(mut image_vec) => {
80          tracing::info!(
81            "Found the image ID '{}' related to CFS sesison '{}'",
82            image_id,
83            cfs_session_name,
84          );
85
86          boot_image_id_vec.append(&mut image_vec);
87        }
88        Err(e) => {
89          tracing::warn!(
90            "Failed to fetch image '{}' for CFS session '{}': {}",
91            image_id,
92            cfs_session_name,
93            e
94          );
95        }
96      }
97    }
98  }
99
100  Ok(boot_image_id_vec)
101}