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::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 nv_redfish_core::ODataId;
28use std::sync::Arc;
29
30/// Represents a Redfish `Session`.
31pub struct Session<B: Bmc> {
32    bmc: NvBmc<B>,
33    data: Arc<SessionSchema>,
34    auth_token: Option<String>,
35    delete_location: Option<ODataId>,
36}
37
38impl<B: Bmc> Session<B> {
39    pub(crate) async fn new(
40        bmc: &NvBmc<B>,
41        nav: &NavProperty<SessionSchema>,
42    ) -> Result<Self, Error<B>> {
43        nav.get(bmc.as_ref())
44            .await
45            .map_err(Error::Bmc)
46            .map(|data| Self {
47                bmc: bmc.clone(),
48                data,
49                auth_token: None,
50                delete_location: None,
51            })
52    }
53
54    pub(crate) fn from_data_with_session_metadata(
55        bmc: NvBmc<B>,
56        data: SessionSchema,
57        auth_token: Option<String>,
58        delete_location: Option<ODataId>,
59    ) -> Self {
60        Self {
61            bmc,
62            data: Arc::new(data),
63            auth_token,
64            delete_location,
65        }
66    }
67
68    /// Get the raw schema data for this session.
69    #[must_use]
70    pub fn raw(&self) -> Arc<SessionSchema> {
71        self.data.clone()
72    }
73
74    /// Get the authentication token returned when this session was created.
75    #[must_use]
76    pub fn auth_token(&self) -> Option<&str> {
77        self.auth_token.as_deref()
78    }
79
80    /// Get the session URI returned in the creation response `Location` header.
81    #[must_use]
82    pub const fn location(&self) -> Option<&ODataId> {
83        self.delete_location.as_ref()
84    }
85
86    /// Delete the current session.
87    ///
88    /// # Errors
89    ///
90    /// Returns an error if deletion fails.
91    pub async fn delete(&self) -> Result<Option<Self>, Error<B>> {
92        match self
93            .bmc
94            .as_ref()
95            .delete::<NavProperty<SessionSchema>>(
96                self.delete_location
97                    .as_ref()
98                    .unwrap_or_else(|| self.data.odata_id()),
99            )
100            .await
101            .map_err(Error::Bmc)?
102        {
103            ModificationResponse::Entity(nav) => Self::new(&self.bmc, &nav).await.map(Some),
104            ModificationResponse::Task(_) | ModificationResponse::Empty => Ok(None),
105        }
106    }
107}
108
109impl<B: Bmc> Resource for Session<B> {
110    fn resource_ref(&self) -> &ResourceSchema {
111        &self.data.as_ref().base
112    }
113}