manta_server/dispatcher.rs
1//! Runtime backend selector — wraps either a CSM or an OpenCHAMI
2//! backend behind a single enum so the rest of the codebase is
3//! backend-agnostic.
4//!
5//! [`StaticBackendDispatcher`] is constructed once per configured site
6//! at startup (see [`crate::config::Site`]) and stored in the request
7//! `ServerState`. Each HTTP request resolves a borrowed `InfraContext`
8//! that holds a reference to the dispatcher for the requested site;
9//! service code calls trait methods on that reference, and the trait
10//! impls in [`crate::backend_dispatcher`] route the call to the active
11//! variant.
12
13use csm_rs::backend_connector::Csm;
14use manta_backend_dispatcher::error::Error;
15use ochami_rs::backend_connector::Ochami;
16
17use crate::config::TokenExchangeConfig;
18
19/// Routes API calls to either a CSM or OCHAMI backend.
20///
21/// Every backend trait (e.g. `CfsTrait`, `GroupTrait`,
22/// `BootParametersTrait`) is implemented for this enum under
23/// [`crate::backend_dispatcher`]. The impls use the `dispatch!` macro
24/// to forward the call to the wrapped client; both variants implement
25/// the same trait surface so service code never branches on backend
26/// kind.
27///
28/// Cloning is cheap — both inner clients are `Arc`-shaped internally.
29/// Service helpers that need to move the dispatcher into a `'static`
30/// spawned task call `InfraContext::backend_clone()` (see
31/// [`crate::service::infra_backend`]).
32#[derive(Clone, Debug)]
33#[allow(clippy::upper_case_acronyms)]
34pub enum StaticBackendDispatcher {
35 /// HPE Cray System Management (CSM) backend, used by Alps-style
36 /// deployments. Wraps a `csm-rs` `Csm` (which itself wraps a
37 /// `ShastaClient` HTTP client).
38 CSM(Csm),
39 /// OpenCHAMI backend, used by sites running the open-source CSM
40 /// alternative. Wraps an `ochami-rs` HTTP client.
41 OCHAMI(Ochami),
42}
43
44impl StaticBackendDispatcher {
45 /// Returns `"csm"` or `"ochami"` for the currently-selected variant.
46 /// Cheap, infallible — intended for use as a structured `tracing` field.
47 pub fn backend_kind(&self) -> &'static str {
48 match self {
49 Self::CSM(_) => "csm",
50 Self::OCHAMI(_) => "ochami",
51 }
52 }
53
54 /// Create a new dispatcher for the given backend type.
55 ///
56 /// `backend_type` must be `"csm"` or `"ochami"` (matching
57 /// [`crate::config::BackendTechnology::as_str`]); any other value
58 /// returns [`Error::UnsupportedBackend`]. `root_cert` is the PEM
59 /// bytes of the backend's root CA — used to verify TLS to
60 /// `base_url`.
61 ///
62 /// `token_exchange` holds the client credentials used to exchange
63 /// the caller's token at the site's identity provider (Keycloak for
64 /// CSM, Hydra for OpenCHAMI). A dispatcher without it fails every
65 /// `exchange_api_token` call.
66 ///
67 /// Called once per configured site during server startup.
68 ///
69 /// # Errors
70 ///
71 /// - [`Error::UnsupportedBackend`] when `backend_type` is not
72 /// `"csm"` or `"ochami"`.
73 /// - Any error surfaced by `Csm::new` (CSM variant) when
74 /// the supplied cert is unusable.
75 /// - [`Error::MissingField`] when `token_exchange` lacks a field this
76 /// backend needs.
77 pub fn new(
78 backend_type: &str,
79 base_url: &str,
80 root_cert: &[u8],
81 token_exchange: Option<&TokenExchangeConfig>,
82 ) -> Result<Self, Error> {
83 match backend_type {
84 "csm" => {
85 let csm = Csm::new(base_url, root_cert)?;
86 Ok(Self::CSM(match token_exchange {
87 Some(te) => csm.with_token_exchange(te.csm()?),
88 None => csm,
89 }))
90 }
91 "ochami" => {
92 let ochami = Ochami::new(base_url, root_cert);
93 Ok(Self::OCHAMI(match token_exchange {
94 Some(te) => ochami.with_token_exchange(te.ochami()?),
95 None => ochami,
96 }))
97 }
98 _ => Err(Error::UnsupportedBackend(format!(
99 "Backend '{backend_type}' not supported"
100 ))),
101 }
102 }
103}