mod collection;
mod item;
use crate::patch_support::JsonValue;
use crate::patch_support::ReadPatchFn;
use crate::schema::redfish::account_service::AccountService as SchemaAccountService;
use crate::Error;
use crate::NvBmc;
use crate::ServiceRoot;
use nv_redfish_core::Bmc;
use std::sync::Arc;
#[doc(inline)]
pub use crate::schema::redfish::manager_account::AccountTypes;
#[doc(inline)]
pub use crate::schema::redfish::manager_account::ManagerAccountCreate;
#[doc(inline)]
pub use crate::schema::redfish::manager_account::ManagerAccountUpdate;
#[doc(inline)]
pub use item::Account;
#[doc(inline)]
pub use collection::AccountCollection;
#[doc(inline)]
pub(crate) use collection::SlotDefinedConfig;
#[doc(inline)]
pub(crate) use item::Config as AccountConfig;
pub struct AccountService<B: Bmc> {
collection_config: collection::Config,
service: Arc<SchemaAccountService>,
bmc: NvBmc<B>,
}
impl<B: Bmc> AccountService<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
root: &ServiceRoot<B>,
) -> Result<Option<Self>, Error<B>> {
let Some(service_nav) = root.root.account_service.as_ref() else {
return Ok(None);
};
let service = service_nav.get(bmc.as_ref()).await.map_err(Error::Bmc)?;
let mut patches = Vec::new();
if bmc.quirks.bug_no_account_type_in_accounts() {
patches.push(append_default_account_type);
}
let account_read_patch_fn = if patches.is_empty() {
None
} else {
let account_read_patch_fn: ReadPatchFn =
Arc::new(move |v| patches.iter().fold(v, |acc, f| f(acc)));
Some(account_read_patch_fn)
};
let slot_defined_user_accounts = bmc.quirks.slot_defined_user_accounts();
Ok(Some(Self {
collection_config: collection::Config {
account: AccountConfig {
read_patch_fn: account_read_patch_fn,
disable_account_on_delete: slot_defined_user_accounts
.as_ref()
.is_some_and(|cfg| cfg.disable_account_on_delete),
},
slot_defined_user_accounts,
},
service,
bmc: bmc.clone(),
}))
}
#[must_use]
pub fn raw(&self) -> Arc<SchemaAccountService> {
self.service.clone()
}
pub async fn accounts(&self) -> Result<Option<AccountCollection<B>>, Error<B>> {
if let Some(collection_ref) = self.service.accounts.as_ref() {
AccountCollection::new(
self.bmc.clone(),
collection_ref,
self.collection_config.clone(),
)
.await
.map(Some)
} else {
Ok(None)
}
}
}
fn append_default_account_type(v: JsonValue) -> JsonValue {
if let JsonValue::Object(mut obj) = v {
obj.entry("AccountTypes")
.or_insert(JsonValue::Array(vec![JsonValue::String("Redfish".into())]));
JsonValue::Object(obj)
} else {
v
}
}