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