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 /// Returns one of the following modification outcomes:
89 ///
90 /// - `ModificationResponse::Entity` contains the session returned by the
91 /// server.
92 /// - `ModificationResponse::Task` identifies an asynchronous operation.
93 /// - `ModificationResponse::Empty` reports synchronous success without a
94 /// response body.
95 ///
96 /// # Errors
97 ///
98 /// Returns an error if deletion fails.
99 pub async fn delete(&self) -> Result<ModificationResponse<Self>, Error<B>> {
100 self.bmc
101 .as_ref()
102 .delete::<NavProperty<SessionSchema>>(
103 self.delete_location
104 .as_ref()
105 .unwrap_or_else(|| self.data.odata_id()),
106 )
107 .await
108 .map_err(Error::Bmc)?
109 .try_map_entity_async(|nav| async move { Self::new(&self.bmc, &nav).await })
110 .await
111 }
112}
113
114impl<B: Bmc> Resource for Session<B> {
115 fn resource_ref(&self) -> &ResourceSchema {
116 &self.data.as_ref().base
117 }
118}