use crate::schema::session::Session as SessionSchema;
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 nv_redfish_core::ODataId;
use std::sync::Arc;
pub struct Session<B: Bmc> {
bmc: NvBmc<B>,
data: Arc<SessionSchema>,
auth_token: Option<String>,
delete_location: Option<ODataId>,
}
impl<B: Bmc> Session<B> {
pub(crate) async fn new(
bmc: &NvBmc<B>,
nav: &NavProperty<SessionSchema>,
) -> Result<Self, Error<B>> {
nav.get(bmc.as_ref())
.await
.map_err(Error::Bmc)
.map(|data| Self {
bmc: bmc.clone(),
data,
auth_token: None,
delete_location: None,
})
}
pub(crate) fn from_data_with_session_metadata(
bmc: NvBmc<B>,
data: SessionSchema,
auth_token: Option<String>,
delete_location: Option<ODataId>,
) -> Self {
Self {
bmc,
data: Arc::new(data),
auth_token,
delete_location,
}
}
#[must_use]
pub fn raw(&self) -> Arc<SessionSchema> {
self.data.clone()
}
#[must_use]
pub fn auth_token(&self) -> Option<&str> {
self.auth_token.as_deref()
}
#[must_use]
pub const fn location(&self) -> Option<&ODataId> {
self.delete_location.as_ref()
}
pub async fn delete(&self) -> Result<Option<Self>, Error<B>> {
match self
.bmc
.as_ref()
.delete::<NavProperty<SessionSchema>>(
self.delete_location
.as_ref()
.unwrap_or_else(|| self.data.odata_id()),
)
.await
.map_err(Error::Bmc)?
{
ModificationResponse::Entity(nav) => Self::new(&self.bmc, &nav).await.map(Some),
ModificationResponse::Task(_) | ModificationResponse::Empty => Ok(None),
}
}
}
impl<B: Bmc> Resource for Session<B> {
fn resource_ref(&self) -> &ResourceSchema {
&self.data.as_ref().base
}
}