Skip to main content

nv_redfish/
assembly.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//! Assembly
17//!
18
19use crate::hardware_id::HardwareIdRef;
20use crate::hardware_id::Manufacturer as HardwareIdManufacturer;
21use crate::hardware_id::Model as HardwareIdModel;
22use crate::hardware_id::PartNumber as HardwareIdPartNumber;
23use crate::hardware_id::SerialNumber as HardwareIdSerialNumber;
24use crate::schema::redfish::assembly::Assembly as AssemblySchema;
25use crate::schema::redfish::assembly::AssemblyData as AssemblyDataSchema;
26use crate::Error;
27use crate::NvBmc;
28use crate::Resource;
29use crate::ResourceSchema;
30use nv_redfish_core::Bmc;
31use nv_redfish_core::NavProperty;
32use std::marker::PhantomData;
33use std::sync::Arc;
34
35#[doc(hidden)]
36pub enum AssemblyTag {}
37
38/// Assembly manufacturer (AKA Producer).
39pub type Manufacturer<T> = HardwareIdManufacturer<T, AssemblyTag>;
40
41/// Assembly model.
42pub type Model<T> = HardwareIdModel<T, AssemblyTag>;
43
44/// Assembly part number.
45pub type PartNumber<T> = HardwareIdPartNumber<T, AssemblyTag>;
46
47/// Assembly number.
48pub type SerialNumber<T> = HardwareIdSerialNumber<T, AssemblyTag>;
49
50/// Assembly.
51///
52/// Provides functions to access assembly.
53pub struct Assembly<B: Bmc> {
54    bmc: NvBmc<B>,
55    data: Arc<AssemblySchema>,
56}
57
58impl<B: Bmc> Assembly<B> {
59    /// Create a new log service handle.
60    pub(crate) async fn new(
61        bmc: &NvBmc<B>,
62        nav: &NavProperty<AssemblySchema>,
63    ) -> Result<Self, Error<B>> {
64        // We use expand here becuase Assembly/Assemblies are
65        // navigation properties, so we want to take them using one
66        // get.
67        bmc.expand_property(nav).await.map(|data| Self {
68            bmc: bmc.clone(),
69            data,
70        })
71    }
72
73    /// Get the raw schema data for this assembly.
74    #[must_use]
75    pub fn raw(&self) -> Arc<AssemblySchema> {
76        self.data.clone()
77    }
78
79    /// Get assemblies.
80    ///
81    /// # Errors
82    ///
83    /// Returns error if this assembly was not expanded by initial get
84    /// and then function failed to get data of the assembly.
85    pub async fn assemblies(&self) -> Result<Vec<AssemblyData<B>>, Error<B>> {
86        let mut result = Vec::new();
87        if let Some(assemblies) = &self.data.assemblies {
88            for m in assemblies {
89                result.push(AssemblyData::new(&self.bmc, m).await?);
90            }
91        }
92        Ok(result)
93    }
94}
95
96impl<B: Bmc> Resource for Assembly<B> {
97    fn resource_ref(&self) -> &ResourceSchema {
98        &self.data.as_ref().base
99    }
100}
101
102/// Assembly data.
103pub struct AssemblyData<B: Bmc> {
104    data: Arc<AssemblyDataSchema>,
105    _marker: PhantomData<B>,
106}
107
108impl<B: Bmc> AssemblyData<B> {
109    /// Create a new log service handle.
110    pub(crate) async fn new(
111        bmc: &NvBmc<B>,
112        nav: &NavProperty<AssemblyDataSchema>,
113    ) -> Result<Self, Error<B>> {
114        nav.get(bmc.as_ref())
115            .await
116            .map_err(crate::Error::Bmc)
117            .map(|data| Self {
118                data,
119                _marker: PhantomData,
120            })
121    }
122
123    /// Get the raw schema data for this assembly.
124    #[must_use]
125    pub fn raw(&self) -> Arc<AssemblyDataSchema> {
126        self.data.clone()
127    }
128
129    /// Get hardware identifier of the network adpater.
130    #[must_use]
131    pub fn hardware_id(&self) -> HardwareIdRef<'_, AssemblyTag> {
132        HardwareIdRef {
133            manufacturer: self
134                .data
135                .producer
136                .as_ref()
137                .and_then(Option::as_deref)
138                .map(Manufacturer::new),
139            model: self
140                .data
141                .model
142                .as_ref()
143                .and_then(Option::as_deref)
144                .map(Model::new),
145            part_number: self
146                .data
147                .part_number
148                .as_ref()
149                .and_then(Option::as_deref)
150                .map(PartNumber::new),
151            serial_number: self
152                .data
153                .serial_number
154                .as_ref()
155                .and_then(Option::as_deref)
156                .map(SerialNumber::new),
157        }
158    }
159}