Skip to main content

nv_redfish/manager/
mod.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 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//! Manager entities and collections.
17//!
18//! This module provides types for working with Redfish Manager resources.
19
20mod item;
21
22use crate::core::NavProperty;
23use crate::patch_support::CollectionWithPatch;
24use crate::patch_support::FilterFn;
25use crate::patch_support::JsonValue;
26use crate::resource::Resource as _;
27use crate::schema::redfish::manager::Manager as ManagerSchema;
28use crate::schema::redfish::manager_collection::ManagerCollection as ManagerCollectionSchema;
29use crate::schema::redfish::resource::ResourceCollection;
30use crate::Error;
31use crate::NvBmc;
32use crate::ServiceRoot;
33use nv_redfish_core::Bmc;
34use std::convert::identity;
35use std::sync::Arc;
36
37pub use item::Manager;
38
39/// Manager collection.
40///
41/// Provides functions to access collection members.
42pub struct ManagerCollection<B: Bmc> {
43    bmc: NvBmc<B>,
44    collection: Arc<ManagerCollectionSchema>,
45}
46
47impl<B: Bmc> ManagerCollection<B> {
48    /// Create a new manager collection handle.
49    pub(crate) async fn new(
50        bmc: &NvBmc<B>,
51        root: &ServiceRoot<B>,
52    ) -> Result<Option<Self>, Error<B>> {
53        let mut filters = Vec::new();
54        if let Some(odata_id_filter) = bmc.quirks.filter_manager_odata_ids() {
55            filters.push(Box::new(move |js: &JsonValue| {
56                js.get("@odata.id")
57                    .and_then(|v| v.as_str())
58                    .map(odata_id_filter)
59                    .is_some_and(identity)
60            }));
61        }
62        let filters_fn = (!filters.is_empty())
63            .then(move || Arc::new(move |v: &JsonValue| filters.iter().any(|f| f(v))) as FilterFn);
64
65        if let Some(collection_ref) = &root.root.managers {
66            Self::expand_collection(bmc, collection_ref, None, filters_fn.as_ref())
67                .await
68                .map(Some)
69        } else if bmc.quirks.bug_missing_root_nav_properties() {
70            bmc.expand_property(&NavProperty::new_reference(
71                format!("{}/Managers", root.odata_id()).into(),
72            ))
73            .await
74            .map(Some)
75        } else {
76            Ok(None)
77        }
78        .map(|c| {
79            c.map(|collection| Self {
80                bmc: bmc.clone(),
81                collection,
82            })
83        })
84    }
85
86    /// List all managers available in this BMC.
87    ///
88    /// # Errors
89    ///
90    /// Returns an error if fetching manager data fails.
91    pub async fn members(&self) -> Result<Vec<Manager<B>>, Error<B>> {
92        let mut members = Vec::new();
93        for m in &self.collection.members {
94            members.push(Manager::new(&self.bmc, m).await?);
95        }
96        Ok(members)
97    }
98}
99
100impl<B: Bmc> CollectionWithPatch<ManagerCollectionSchema, ManagerSchema, B>
101    for ManagerCollection<B>
102{
103    fn convert_patched(
104        base: ResourceCollection,
105        members: Vec<NavProperty<ManagerSchema>>,
106    ) -> ManagerCollectionSchema {
107        ManagerCollectionSchema { base, members }
108    }
109}