Skip to main content

nv_redfish/session_service/
item.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Redfish Session - high-level wrapper.
17
18use crate::schema::redfish::session::Session as SessionSchema;
19use crate::Error;
20use crate::NvBmc;
21use crate::Resource;
22use crate::ResourceSchema;
23use nv_redfish_core::Bmc;
24use nv_redfish_core::EntityTypeRef as _;
25use nv_redfish_core::ModificationResponse;
26use nv_redfish_core::NavProperty;
27use std::sync::Arc;
28
29/// Represents a Redfish `Session`.
30pub struct Session<B: Bmc> {
31    bmc: NvBmc<B>,
32    data: Arc<SessionSchema>,
33}
34
35impl<B: Bmc> Session<B> {
36    pub(crate) async fn new(
37        bmc: &NvBmc<B>,
38        nav: &NavProperty<SessionSchema>,
39    ) -> Result<Self, Error<B>> {
40        nav.get(bmc.as_ref())
41            .await
42            .map_err(Error::Bmc)
43            .map(|data| Self {
44                bmc: bmc.clone(),
45                data,
46            })
47    }
48
49    pub(crate) fn from_data(bmc: NvBmc<B>, data: SessionSchema) -> Self {
50        Self {
51            bmc,
52            data: Arc::new(data),
53        }
54    }
55
56    /// Get the raw schema data for this session.
57    #[must_use]
58    pub fn raw(&self) -> Arc<SessionSchema> {
59        self.data.clone()
60    }
61
62    /// Delete the current session.
63    ///
64    /// # Errors
65    ///
66    /// Returns an error if deletion fails.
67    pub async fn delete(&self) -> Result<Option<Self>, Error<B>> {
68        match self
69            .bmc
70            .as_ref()
71            .delete::<NavProperty<SessionSchema>>(self.data.odata_id())
72            .await
73            .map_err(Error::Bmc)?
74        {
75            ModificationResponse::Entity(nav) => Self::new(&self.bmc, &nav).await.map(Some),
76            ModificationResponse::Task(_) | ModificationResponse::Empty => Ok(None),
77        }
78    }
79}
80
81impl<B: Bmc> Resource for Session<B> {
82    fn resource_ref(&self) -> &ResourceSchema {
83        &self.data.as_ref().base
84    }
85}