Skip to main content

nv_redfish/session_service/
collection.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//! Session collection utilities.
17
18use crate::schema::session::Session as SessionSchema;
19use crate::schema::session_collection::SessionCollection as SessionCollectionSchema;
20use crate::session_service::Session;
21use crate::session_service::SessionCreate;
22use crate::Error;
23use crate::NvBmc;
24use nv_redfish_core::Bmc;
25use nv_redfish_core::EntityTypeRef as _;
26use nv_redfish_core::NavProperty;
27use std::sync::Arc;
28
29/// Session collection.
30///
31/// Provides functions to list and create sessions.
32pub struct SessionCollection<B: Bmc> {
33    bmc: NvBmc<B>,
34    collection: Arc<SessionCollectionSchema>,
35}
36
37impl<B: Bmc> SessionCollection<B> {
38    pub(crate) async fn new(
39        bmc: NvBmc<B>,
40        collection_ref: &NavProperty<SessionCollectionSchema>,
41    ) -> Result<Self, Error<B>> {
42        let collection = bmc.expand_property(collection_ref).await?;
43        Ok(Self { bmc, collection })
44    }
45
46    /// List all sessions available in this BMC.
47    ///
48    /// # Errors
49    ///
50    /// Returns an error if fetching session data fails.
51    pub async fn members(&self) -> Result<Vec<Session<B>>, Error<B>> {
52        let mut members = Vec::with_capacity(self.collection.members.len());
53        for member in &self.collection.members {
54            members.push(Session::new(&self.bmc, member).await?);
55        }
56        Ok(members)
57    }
58
59    /// Create a new session.
60    ///
61    /// # Errors
62    ///
63    /// Returns an error if creating the session fails.
64    pub async fn create_session(&self, create: &SessionCreate) -> Result<Session<B>, Error<B>> {
65        let response = self
66            .bmc
67            .as_ref()
68            .create_session::<_, SessionSchema>(self.collection.as_ref().odata_id(), create)
69            .await
70            .map_err(Error::Bmc)?;
71        Ok(Session::from_data_with_session_metadata(
72            self.bmc.clone(),
73            response.entity,
74            Some(response.auth_token),
75            Some(response.location),
76        ))
77    }
78}