use manta_backend_dispatcher::{
error::Error, interfaces::cfs::CfsTrait as _,
};
use crate::server::common::app_context::InfraContext;
use super::{authorization::validate_user_group_members_access, node_ops};
pub(crate) async fn apply_runtime_configuration(
infra: &InfraContext<'_>,
token: &str,
cfs_configuration_name: &str,
hosts_expression: &str,
enabled: bool,
dry_run: bool,
) -> Result<Vec<String>, Error> {
if cfs_configuration_name.trim().is_empty() {
return Err(Error::BadRequest(
"cfs_configuration_name must not be empty".into(),
));
}
if hosts_expression.trim().is_empty() {
return Err(Error::BadRequest(
"hosts_expression must not be empty".into(),
));
}
let xnames = node_ops::from_user_hosts_expression_to_xname_vec(
infra,
token,
hosts_expression,
false,
)
.await?;
if xnames.is_empty() {
return Err(Error::BadRequest(format!(
"hosts_expression '{hosts_expression}' resolved to zero nodes"
)));
}
validate_user_group_members_access(infra, token, &xnames).await?;
let configuration_name_owned = cfs_configuration_name.to_string();
let configs = infra
.backend
.get_configuration(token, Some(&configuration_name_owned))
.await?;
if configs.is_empty() {
return Err(Error::NotFound(format!(
"CFS configuration '{cfs_configuration_name}'"
)));
}
if dry_run {
tracing::info!(
"dry_run: skipping CFS component PATCH for {} nodes",
xnames.len()
);
return Ok(xnames);
}
infra
.backend
.update_runtime_configuration(
token,
&xnames,
cfs_configuration_name,
enabled,
)
.await?;
Ok(xnames)
}