use cosmian_kms_client::{
KmsClient,
cosmian_kmip::kmip_2_1::{kmip_operations::Destroy, kmip_types::UniqueIdentifier},
};
use crate::{
actions::kms::console,
cli_bail,
error::result::{KmsCliResult, KmsCliResultHelper},
};
pub(crate) async fn destroy(
kms_rest_client: KmsClient,
uid: &str,
remove: bool,
) -> KmsCliResult<UniqueIdentifier> {
let uid = UniqueIdentifier::TextString(uid.to_owned());
let destroy_query = Destroy {
unique_identifier: Some(uid.clone()),
remove,
cascade: true,
};
let destroy_response = kms_rest_client
.destroy(destroy_query)
.await
.with_context(|| format!("destroying the object {} failed", &uid))?;
if uid == destroy_response.unique_identifier {
let verb = if remove { "removed" } else { "destroyed" };
let mut stdout = console::Stdout::new(format!("Successfully {verb} the object.").as_str());
stdout.set_unique_identifier(&uid);
stdout.write()?;
Ok(uid)
} else {
cli_bail!("Something went wrong when destroying the object.")
}
}