Skip to main content

nv_redfish/chassis/
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
16mod item;
17
18#[cfg(feature = "network-adapters")]
19mod network_adapter;
20#[cfg(feature = "power")]
21mod power;
22#[cfg(feature = "power-supplies")]
23mod power_supply;
24#[cfg(feature = "thermal")]
25mod thermal;
26
27use nv_redfish_core::Bmc;
28use std::sync::Arc;
29
30#[doc(inline)]
31pub use item::Chassis;
32#[doc(inline)]
33pub use item::Manufacturer;
34#[doc(inline)]
35pub use item::Model;
36#[doc(inline)]
37pub use item::PartNumber;
38#[doc(inline)]
39pub use item::SerialNumber;
40
41#[doc(inline)]
42#[cfg(feature = "network-adapters")]
43pub use network_adapter::Manufacturer as NetworkAdapterManufacturer;
44#[doc(inline)]
45#[cfg(feature = "network-adapters")]
46pub use network_adapter::Model as NetworkAdapterModel;
47#[doc(inline)]
48#[cfg(feature = "network-adapters")]
49pub use network_adapter::NetworkAdapter;
50#[cfg(feature = "network-adapters")]
51pub use network_adapter::NetworkAdapterCollection;
52#[doc(inline)]
53#[cfg(feature = "network-adapters")]
54pub use network_adapter::PartNumber as NetworkAdapterPartNumber;
55#[doc(inline)]
56#[cfg(feature = "network-adapters")]
57pub use network_adapter::SerialNumber as NetworkAdapterSerialNumber;
58#[doc(inline)]
59#[cfg(feature = "power")]
60pub use power::Power;
61#[doc(inline)]
62#[cfg(feature = "power-supplies")]
63pub use power_supply::PowerSupply;
64#[doc(inline)]
65#[cfg(feature = "thermal")]
66pub use thermal::Thermal;
67
68use crate::core::NavProperty;
69use crate::patch_support::CollectionWithPatch;
70use crate::resource::Resource as _;
71use crate::schema::redfish::chassis::Chassis as ChassisSchema;
72use crate::schema::redfish::chassis_collection::ChassisCollection as ChassisCollectionSchema;
73use crate::schema::redfish::resource::ResourceCollection;
74use crate::Error;
75use crate::NvBmc;
76use crate::ServiceRoot;
77
78/// Chassis collection.
79///
80/// Provides functions to access collection members.
81pub struct ChassisCollection<B: Bmc> {
82    bmc: NvBmc<B>,
83    collection: Arc<ChassisCollectionSchema>,
84    item_config: Arc<item::Config>,
85}
86
87impl<B: Bmc> ChassisCollection<B> {
88    pub(crate) async fn new(
89        bmc: &NvBmc<B>,
90        root: &ServiceRoot<B>,
91    ) -> Result<Option<Self>, Error<B>> {
92        let item_config = item::Config::new(&bmc.quirks);
93        if let Some(collection_ref) = &root.root.chassis {
94            Self::expand_collection(
95                bmc,
96                collection_ref,
97                item_config.read_patch_fn.as_ref(),
98                None,
99            )
100            .await
101            .map(Some)
102        } else if bmc.quirks.bug_missing_root_nav_properties() {
103            bmc.expand_property(&NavProperty::new_reference(
104                format!("{}/Chassis", root.odata_id()).into(),
105            ))
106            .await
107            .map(Some)
108        } else {
109            Ok(None)
110        }
111        .map(|c| {
112            c.map(|collection| Self {
113                bmc: bmc.clone(),
114                collection,
115                item_config: item_config.into(),
116            })
117        })
118    }
119
120    /// List all chassis avaiable in this BMC
121    ///
122    /// # Errors
123    ///
124    /// Returns an error if fetching collection data fails.
125    pub async fn members(&self) -> Result<Vec<Chassis<B>>, Error<B>> {
126        let mut chassis_members = Vec::new();
127        for chassis in &self.collection.members {
128            chassis_members.push(Chassis::new(&self.bmc, chassis, self.item_config.clone()).await?);
129        }
130
131        Ok(chassis_members)
132    }
133}
134
135impl<B: Bmc> CollectionWithPatch<ChassisCollectionSchema, ChassisSchema, B>
136    for ChassisCollection<B>
137{
138    fn convert_patched(
139        base: ResourceCollection,
140        members: Vec<NavProperty<ChassisSchema>>,
141    ) -> ChassisCollectionSchema {
142        ChassisCollectionSchema { base, members }
143    }
144}