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::entity_link::EntityLink;
70use crate::patch_support::CollectionWithPatch;
71use crate::resource::Resource as _;
72use crate::schema::chassis::Chassis as ChassisSchema;
73use crate::schema::chassis_collection::ChassisCollection as ChassisCollectionSchema;
74use crate::schema::resource::ResourceCollection;
75use crate::Error;
76use crate::NvBmc;
77use crate::ServiceRoot;
78
79/// Link for accessing sensor.
80pub type ChassisLink<B> = EntityLink<B, ChassisSchema>;
81
82/// Chassis collection.
83///
84/// Provides functions to access collection members.
85pub struct ChassisCollection<B: Bmc> {
86    bmc: NvBmc<B>,
87    collection: Arc<ChassisCollectionSchema>,
88}
89
90impl<B: Bmc> ChassisCollection<B> {
91    pub(crate) async fn new(
92        bmc: &NvBmc<B>,
93        root: &ServiceRoot<B>,
94    ) -> Result<Option<Self>, Error<B>> {
95        let item_config = item::Config::new(&bmc.quirks);
96        if let Some(collection_ref) = &root.root.chassis {
97            Self::expand_collection(
98                bmc,
99                collection_ref,
100                item_config.read_patch_fn.as_ref(),
101                None,
102            )
103            .await
104            .map(Some)
105        } else if bmc.quirks.bug_missing_root_nav_properties() {
106            bmc.expand_property(&NavProperty::new_reference(
107                format!("{}/Chassis", root.odata_id()).into(),
108            ))
109            .await
110            .map(Some)
111        } else {
112            Ok(None)
113        }
114        .map(|c| {
115            c.map(|collection| Self {
116                bmc: bmc.clone(),
117                collection,
118            })
119        })
120    }
121
122    /// List all chassis avaiable in this BMC
123    ///
124    /// # Errors
125    ///
126    /// Returns an error if fetching collection data fails.
127    pub async fn members(&self) -> Result<Vec<Chassis<B>>, Error<B>> {
128        let mut chassis_members = Vec::new();
129        for chassis in &self.collection.members {
130            chassis_members.push(Chassis::new(&self.bmc, chassis).await?);
131        }
132
133        Ok(chassis_members)
134    }
135}
136
137impl<B: Bmc> CollectionWithPatch<ChassisCollectionSchema, ChassisSchema, B>
138    for ChassisCollection<B>
139{
140    fn convert_patched(
141        base: ResourceCollection,
142        members: Vec<NavProperty<ChassisSchema>>,
143    ) -> ChassisCollectionSchema {
144        ChassisCollectionSchema { base, members }
145    }
146}