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