use anyhow::{Error, anyhow, bail};
use crate::common::app_context::AppContext;
use crate::http_client::{MantaClient, OpenApiResultExt};
use crate::openapi_client::types::ApplyRuntimeConfigurationRequest;
pub struct ExecParams<'a> {
pub configuration_name: &'a str,
pub group_name: &'a str,
pub disable: bool,
pub dry_run: bool,
}
pub async fn exec(
ctx: &AppContext<'_>,
token: &str,
p: ExecParams<'_>,
) -> Result<(), Error> {
let client = MantaClient::from_app_ctx(ctx, Some(token))?;
let groups = client
.openapi
.get_groups(Some(p.group_name), client.site_name())
.await
.into_anyhow()
.await?;
let group = groups
.into_iter()
.next()
.ok_or_else(|| anyhow!("HSM group '{}' not found", p.group_name))?;
let xnames = group.members.and_then(|m| m.ids).unwrap_or_default();
if xnames.is_empty() {
bail!("HSM group '{}' has no members", p.group_name);
}
let result = client
.openapi
.apply_runtime_configuration(
client.site_name(),
&ApplyRuntimeConfigurationRequest {
cfs_configuration_name: p.configuration_name.to_string(),
hosts_expression: xnames.join(","),
enabled: !p.disable,
dry_run: Some(p.dry_run),
},
)
.await
.into_anyhow()
.await?;
println!("{}", serde_json::to_string_pretty(&result)?);
Ok(())
}