use anyhow::{Context, Error};
use clap::ArgMatches;
use crate::cli::common::clap_ext::ArgMatchesExt;
use crate::cli::http_client::MantaClient;
use crate::cli::output::action_result;
use manta_shared::common::app_context::AppContext;
use manta_shared::shared::params::hw_cluster::HwClusterMode;
pub async fn exec(
cli_apply_hw_cluster: &ArgMatches,
ctx: &AppContext<'_>,
token: &str,
) -> Result<(), Error> {
let settings_hsm_group_name_opt = ctx.settings_hsm_group_name_opt;
let target_hsm_group_name_arg_opt =
cli_apply_hw_cluster.opt_str("target-cluster");
let parent_hsm_group_name_arg_opt =
cli_apply_hw_cluster.opt_str("parent-cluster");
let dryrun = cli_apply_hw_cluster.get_flag("dry-run");
let create_target_hsm_group = *cli_apply_hw_cluster
.get_one::<bool>("create-target-hsm-group")
.unwrap_or(&true);
let delete_empty_parent_hsm_group = *cli_apply_hw_cluster
.get_one::<bool>("delete-empty-parent-hsm-group")
.unwrap_or(&true);
let is_unpin = cli_apply_hw_cluster
.get_one::<bool>("unpin-nodes")
.unwrap_or(&false);
let mode = if *is_unpin {
HwClusterMode::Unpin
} else {
HwClusterMode::Pin
};
let pattern = cli_apply_hw_cluster.req_str("pattern")?;
let server_url = ctx.manta_server_url;
let target = target_hsm_group_name_arg_opt
.or(settings_hsm_group_name_opt)
.context("No target HSM group specified")?;
let parent = parent_hsm_group_name_arg_opt
.or(settings_hsm_group_name_opt)
.context("No parent HSM group specified")?;
let mode_str = match mode {
HwClusterMode::Pin => "pin",
HwClusterMode::Unpin => "unpin",
};
let result = MantaClient::new(server_url, ctx.site_name)?
.apply_hw_configuration(
token,
target,
parent,
pattern,
mode_str,
dryrun,
create_target_hsm_group,
delete_empty_parent_hsm_group,
)
.await?;
let output_opt = cli_apply_hw_cluster.opt_str("output");
let message = if dryrun {
"Dry run enabled, not modifying the HSM groups on the system."
} else {
"Hardware configuration applied."
};
action_result::print_with_data(message, &result, output_opt)?;
Ok(())
}