Skip to main content

nv_redfish/manager/
item.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
16use crate::schema::redfish::manager::Manager as ManagerSchema;
17use crate::Error;
18use crate::NvBmc;
19use crate::Resource;
20use crate::ResourceSchema;
21use nv_redfish_core::Bmc;
22use nv_redfish_core::NavProperty;
23use std::sync::Arc;
24
25#[cfg(feature = "ethernet-interfaces")]
26use crate::ethernet_interface::EthernetInterfaceCollection;
27#[cfg(feature = "host-interfaces")]
28use crate::host_interface::HostInterfaceCollection;
29#[cfg(feature = "log-services")]
30use crate::log_service::LogService;
31#[cfg(feature = "oem-dell-attributes")]
32use crate::oem::dell::attributes::DellAttributes;
33#[cfg(feature = "oem-hpe")]
34use crate::oem::hpe::manager::HpeManager;
35#[cfg(feature = "oem-lenovo")]
36use crate::oem::lenovo::manager::LenovoManager;
37#[cfg(feature = "oem-supermicro")]
38use crate::oem::supermicro::manager::SupermicroManager;
39
40/// Represents a manager (BMC) in the system.
41///
42/// Provides access to manager information and associated services.
43pub struct Manager<B: Bmc> {
44    #[allow(dead_code)] // enabled by features
45    bmc: NvBmc<B>,
46    data: Arc<ManagerSchema>,
47}
48
49impl<B: Bmc> Manager<B> {
50    /// Create a new manager handle.
51    pub(crate) async fn new(
52        bmc: &NvBmc<B>,
53        nav: &NavProperty<ManagerSchema>,
54    ) -> Result<Self, Error<B>> {
55        nav.get(bmc.as_ref())
56            .await
57            .map_err(Error::Bmc)
58            .map(|data| Self {
59                bmc: bmc.clone(),
60                data,
61            })
62    }
63
64    /// Get the raw schema data for this manager.
65    ///
66    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
67    /// and sharing of the data.
68    #[must_use]
69    pub fn raw(&self) -> Arc<ManagerSchema> {
70        self.data.clone()
71    }
72
73    /// Get ethernet interfaces for this manager.
74    ///
75    /// Returns `Ok(None)` when the ethernet interfaces link is absent.
76    ///
77    /// # Errors
78    ///
79    /// Returns an error if fetching ethernet interfaces data fails.
80    #[cfg(feature = "ethernet-interfaces")]
81    pub async fn ethernet_interfaces(
82        &self,
83    ) -> Result<Option<EthernetInterfaceCollection<B>>, crate::Error<B>> {
84        if let Some(p) = &self.data.ethernet_interfaces {
85            EthernetInterfaceCollection::new(&self.bmc, p)
86                .await
87                .map(Some)
88        } else {
89            Ok(None)
90        }
91    }
92
93    /// Get host interfaces for this manager.
94    ///
95    /// Returns `Ok(None)` when the host interfaces link is absent.
96    ///
97    /// # Errors
98    ///
99    /// Returns an error if fetching host interfaces data fails.
100    #[cfg(feature = "host-interfaces")]
101    pub async fn host_interfaces(
102        &self,
103    ) -> Result<Option<HostInterfaceCollection<B>>, crate::Error<B>> {
104        if let Some(p) = &self.data.host_interfaces {
105            HostInterfaceCollection::new(&self.bmc, p).await.map(Some)
106        } else {
107            Ok(None)
108        }
109    }
110
111    /// Get log services for this manager.
112    ///
113    /// Returns `Ok(None)` when the log services link is absent.
114    ///
115    /// # Errors
116    ///
117    /// Returns an error if fetching log service data fails.
118    #[cfg(feature = "log-services")]
119    pub async fn log_services(&self) -> Result<Option<Vec<LogService<B>>>, crate::Error<B>> {
120        if let Some(log_services_ref) = &self.data.log_services {
121            let log_services_collection = log_services_ref
122                .get(self.bmc.as_ref())
123                .await
124                .map_err(crate::Error::Bmc)?;
125
126            let mut log_services = Vec::new();
127            for m in &log_services_collection.members {
128                log_services.push(LogService::new(&self.bmc, m).await?);
129            }
130
131            Ok(Some(log_services))
132        } else {
133            Ok(None)
134        }
135    }
136
137    /// Get Dell Manager attributes for this manager.
138    ///
139    /// Returns `Ok(None)` when the manager does not include `Oem.Dell`.
140    ///
141    /// # Errors
142    ///
143    /// Returns an error if fetching manager attributes data fails.
144    #[cfg(feature = "oem-dell-attributes")]
145    pub async fn oem_dell_attributes(&self) -> Result<Option<DellAttributes<B>>, Error<B>> {
146        DellAttributes::manager_attributes(&self.bmc, &self.data).await
147    }
148
149    /// Get Lenovo Manager OEM.
150    ///
151    /// Returns `Ok(None)` when the manager does not include `Oem.Lenovo`.
152    ///
153    /// # Errors
154    ///
155    /// Returns an error if parsing Lenovo manager OEM data fails.
156    #[cfg(feature = "oem-lenovo")]
157    pub fn oem_lenovo(&self) -> Result<Option<LenovoManager<B>>, Error<B>> {
158        LenovoManager::new(&self.bmc, &self.data)
159    }
160
161    /// Get HPE Manager OEM.
162    ///
163    /// Returns `Ok(None)` when the manager does not include `Oem.Hpe`.
164    ///
165    /// # Errors
166    ///
167    /// Returns an error if parsing HPE manager OEM data fails.
168    #[cfg(feature = "oem-hpe")]
169    pub fn oem_hpe(&self) -> Result<Option<HpeManager<B>>, Error<B>> {
170        HpeManager::new(&self.bmc, &self.data)
171    }
172
173    /// Get Supermicro Manager OEM.
174    ///
175    /// Returns `Ok(None)` when the manager does not include `Oem.Supermicro`.
176    ///
177    /// # Errors
178    ///
179    /// Returns an error if parsing Supermicro manager OEM data fails.
180    #[cfg(feature = "oem-supermicro")]
181    pub fn oem_supermicro(&self) -> Result<Option<SupermicroManager<B>>, Error<B>> {
182        SupermicroManager::new(&self.bmc, &self.data)
183    }
184}
185
186impl<B: Bmc> Resource for Manager<B> {
187    fn resource_ref(&self) -> &ResourceSchema {
188        &self.data.as_ref().base
189    }
190}