use crate::schema::log_entry::LogEntry;
use crate::schema::log_service::LogService as LogServiceSchema;
use crate::Error;
use crate::NvBmc;
use crate::Resource;
use crate::ResourceSchema;
use nv_redfish_core::Bmc;
use nv_redfish_core::ModificationResponse;
use nv_redfish_core::NavProperty;
use std::sync::Arc;
pub struct LogService<B: Bmc> {
bmc: NvBmc<B>,
data: Arc<LogServiceSchema>,
}
impl<B: Bmc> LogService<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<LogServiceSchema>,
) -> Result<Self, Error<B>> {
nav.get(bmc.as_ref())
.await
.map_err(crate::Error::Bmc)
.map(|data| Self {
bmc: bmc.clone(),
data,
})
}
#[must_use]
pub fn raw(&self) -> Arc<LogServiceSchema> {
self.data.clone()
}
pub async fn entries(&self) -> Result<Option<Vec<Arc<LogEntry>>>, Error<B>> {
if let Some(entries_ref) = &self.data.entries {
let entries_collection = self.bmc.expand_property(entries_ref).await?;
self.expand_entries(&entries_collection.members)
.await
.map(Some)
} else {
Ok(None)
}
}
pub async fn filter_entries(
&self,
filter: nv_redfish_core::FilterQuery,
) -> Result<Option<Vec<Arc<LogEntry>>>, Error<B>> {
if let Some(entries_ref) = &self.data.entries {
let entries_collection = entries_ref
.filter(self.bmc.as_ref(), filter)
.await
.map_err(Error::Bmc)?;
self.expand_entries(&entries_collection.members)
.await
.map(Some)
} else {
Ok(None)
}
}
pub async fn clear_log(
&self,
log_entry_codes: Option<String>,
) -> Result<ModificationResponse<()>, Error<B>>
where
B::Error: nv_redfish_core::ActionError,
{
let actions = self
.data
.actions
.as_ref()
.ok_or(Error::ActionNotAvailable)?;
actions
.clear_log(self.bmc.as_ref(), log_entry_codes)
.await
.map_err(Error::Bmc)
}
async fn expand_entries(
&self,
entry_refs: &[NavProperty<LogEntry>],
) -> Result<Vec<Arc<LogEntry>>, Error<B>> {
let mut entries = Vec::new();
for entry_ref in entry_refs {
let entry = entry_ref.get(self.bmc.as_ref()).await.map_err(Error::Bmc)?;
entries.push(entry);
}
Ok(entries)
}
}
impl<B: Bmc> Resource for LogService<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}