use crate::account::ManagerAccountUpdate;
use crate::patch_support::Payload;
use crate::patch_support::ReadPatchFn;
use crate::patch_support::UpdateWithPatch;
use crate::schema::manager_account::ManagerAccount;
use crate::Error;
use crate::NvBmc;
use crate::Resource;
use crate::ResourceSchema;
use nv_redfish_core::Bmc;
use nv_redfish_core::EntityTypeRef as _;
use nv_redfish_core::ModificationResponse;
use nv_redfish_core::NavProperty;
use std::convert::identity;
use std::sync::Arc;
#[derive(Clone)]
pub struct Config {
pub read_patch_fn: Option<ReadPatchFn>,
pub disable_account_on_delete: bool,
}
pub struct Account<B: Bmc> {
config: Config,
bmc: NvBmc<B>,
data: Arc<ManagerAccount>,
}
impl<B: Bmc> UpdateWithPatch<ManagerAccount, ManagerAccountUpdate, B> for Account<B> {
fn entity_ref(&self) -> &ManagerAccount {
self.data.as_ref()
}
fn patch(&self) -> Option<&ReadPatchFn> {
self.config.read_patch_fn.as_ref()
}
fn bmc(&self) -> &B {
self.bmc.as_ref()
}
}
impl<B: Bmc> Account<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<ManagerAccount>,
config: &Config,
) -> Result<Self, Error<B>> {
if let Some(read_patch_fn) = &config.read_patch_fn {
Payload::get(bmc.as_ref(), nav, read_patch_fn.as_ref()).await
} else {
nav.get(bmc.as_ref()).await.map_err(Error::Bmc)
}
.map(|data| Self {
bmc: bmc.clone(),
data,
config: config.clone(),
})
}
pub(crate) fn from_data(bmc: NvBmc<B>, data: ManagerAccount, config: Config) -> Self {
Self {
bmc,
data: Arc::new(data),
config,
}
}
#[must_use]
pub fn raw(&self) -> Arc<ManagerAccount> {
self.data.clone()
}
#[must_use]
pub fn is_enabled(&self) -> bool {
self.data.enabled.is_none_or(identity)
}
pub async fn update(&self, update: &ManagerAccountUpdate) -> Result<Option<Self>, Error<B>> {
match self.update_with_patch(update).await? {
ModificationResponse::Entity(ma) => Ok(Some(Self::from_data(
self.bmc.clone(),
ma,
self.config.clone(),
))),
ModificationResponse::Task(_) | ModificationResponse::Empty => Ok(None),
}
}
pub async fn update_password(&self, password: String) -> Result<Option<Self>, Error<B>> {
self.update(
&ManagerAccountUpdate::builder()
.with_password(password)
.build(),
)
.await
}
pub async fn update_user_name(&self, user_name: String) -> Result<Option<Self>, Error<B>> {
self.update(
&ManagerAccountUpdate::builder()
.with_user_name(user_name)
.build(),
)
.await
}
pub async fn delete(&self) -> Result<Option<Self>, Error<B>> {
if self.config.disable_account_on_delete {
self.update(&ManagerAccountUpdate::builder().with_enabled(false).build())
.await
} else {
match self
.bmc
.as_ref()
.delete::<NavProperty<ManagerAccount>>(self.data.odata_id())
.await
.map_err(Error::Bmc)?
{
ModificationResponse::Entity(nav) => {
Self::new(&self.bmc, &nav, &self.config).await.map(Some)
}
ModificationResponse::Task(_) | ModificationResponse::Empty => Ok(None),
}
}
}
}
impl<B: Bmc> Resource for Account<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}