Skip to main content

nv_redfish/
host_interface.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//! Host interfaces
17//!
18
19use crate::schema::redfish::host_interface::HostInterface as HostInterfaceSchema;
20use crate::schema::redfish::host_interface_collection::HostInterfaceCollection as HostInterfaceCollectionSchema;
21use crate::Error;
22use crate::NvBmc;
23use crate::Resource;
24use crate::ResourceSchema;
25use nv_redfish_core::Bmc;
26use nv_redfish_core::NavProperty;
27use std::marker::PhantomData;
28use std::sync::Arc;
29
30/// Host interfaces collection.
31///
32/// Provides functions to access collection members.
33pub struct HostInterfaceCollection<B: Bmc> {
34    bmc: NvBmc<B>,
35    collection: Arc<HostInterfaceCollectionSchema>,
36}
37
38impl<B: Bmc> HostInterfaceCollection<B> {
39    /// Create a new manager collection handle.
40    pub(crate) async fn new(
41        bmc: &NvBmc<B>,
42        nav: &NavProperty<HostInterfaceCollectionSchema>,
43    ) -> Result<Self, Error<B>> {
44        let collection = bmc.expand_property(nav).await?;
45        Ok(Self {
46            bmc: bmc.clone(),
47            collection,
48        })
49    }
50
51    /// List all managers available in this BMC.
52    ///
53    /// # Errors
54    ///
55    /// Returns an error if fetching manager data fails.
56    pub async fn members(&self) -> Result<Vec<HostInterface<B>>, Error<B>> {
57        let mut members = Vec::new();
58        for m in &self.collection.members {
59            members.push(HostInterface::new(&self.bmc, m).await?);
60        }
61        Ok(members)
62    }
63}
64
65/// Host Interface.
66///
67/// Provides functions to access host interface.
68pub struct HostInterface<B: Bmc> {
69    data: Arc<HostInterfaceSchema>,
70    _marker: PhantomData<B>,
71}
72
73impl<B: Bmc> HostInterface<B> {
74    /// Create a new log service handle.
75    pub(crate) async fn new(
76        bmc: &NvBmc<B>,
77        nav: &NavProperty<HostInterfaceSchema>,
78    ) -> Result<Self, Error<B>> {
79        nav.get(bmc.as_ref())
80            .await
81            .map_err(crate::Error::Bmc)
82            .map(|data| Self {
83                data,
84                _marker: PhantomData,
85            })
86    }
87
88    /// Get the raw schema data for this host interface.
89    #[must_use]
90    pub fn raw(&self) -> Arc<HostInterfaceSchema> {
91        self.data.clone()
92    }
93
94    /// State of the interface. `None` means that BMC hasn't reported
95    /// interface state or reported null.
96    #[must_use]
97    pub fn interface_enabled(&self) -> Option<bool> {
98        self.data
99            .interface_enabled
100            .as_ref()
101            .and_then(Option::as_ref)
102            .copied()
103    }
104}
105
106impl<B: Bmc> Resource for HostInterface<B> {
107    fn resource_ref(&self) -> &ResourceSchema {
108        &self.data.as_ref().base
109    }
110}