Skip to main content

nv_redfish/computer_system/
drive.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::drive::Drive as DriveSchema;
17use crate::schema::redfish::drive_metrics::DriveMetrics;
18use crate::Error;
19use crate::NvBmc;
20use crate::Resource;
21use crate::ResourceSchema;
22use nv_redfish_core::Bmc;
23use nv_redfish_core::NavProperty;
24use std::sync::Arc;
25
26#[cfg(feature = "sensors")]
27use crate::sensor::extract_environment_sensors;
28#[cfg(feature = "sensors")]
29use crate::sensor::SensorRef;
30
31/// Represents a drive (disk) in a storage controller.
32///
33/// Provides access to drive information and associated metrics/sensors.
34pub struct Drive<B: Bmc> {
35    bmc: NvBmc<B>,
36    data: Arc<DriveSchema>,
37}
38
39impl<B: Bmc> Drive<B> {
40    /// Create a new drive handle.
41    pub(crate) async fn new(
42        bmc: &NvBmc<B>,
43        nav: &NavProperty<DriveSchema>,
44    ) -> Result<Self, Error<B>> {
45        nav.get(bmc.as_ref())
46            .await
47            .map_err(Error::Bmc)
48            .map(|data| Self {
49                bmc: bmc.clone(),
50                data,
51            })
52    }
53
54    /// Get the raw schema data for this drive.
55    ///
56    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
57    /// and sharing of the data.
58    #[must_use]
59    pub fn raw(&self) -> Arc<DriveSchema> {
60        self.data.clone()
61    }
62
63    /// Get drive metrics.
64    ///
65    /// Returns the drive's performance and state metrics if available.
66    ///
67    /// # Errors
68    ///
69    /// Returns an error if:
70    /// - The drive does not have metrics
71    /// - Fetching metrics data fails
72    pub async fn metrics(&self) -> Result<Option<Arc<DriveMetrics>>, Error<B>> {
73        if let Some(metrics_ref) = &self.data.metrics {
74            metrics_ref
75                .get(self.bmc.as_ref())
76                .await
77                .map_err(Error::Bmc)
78                .map(Some)
79        } else {
80            Ok(None)
81        }
82    }
83
84    /// Get the environment sensors for this drive.
85    ///
86    /// Returns a vector of `Sensor<B>` obtained from environment metrics, if available.
87    ///
88    /// # Errors
89    ///
90    /// Returns an error if get of environment metrics failed.
91    #[cfg(feature = "sensors")]
92    pub async fn environment_sensors(&self) -> Result<Vec<SensorRef<B>>, Error<B>> {
93        let sensor_refs = if let Some(env_ref) = &self.data.environment_metrics {
94            extract_environment_sensors(env_ref, self.bmc.as_ref()).await?
95        } else {
96            Vec::new()
97        };
98
99        Ok(sensor_refs
100            .into_iter()
101            .map(|r| SensorRef::new(self.bmc.clone(), r))
102            .collect())
103    }
104}
105
106impl<B: Bmc> Resource for Drive<B> {
107    fn resource_ref(&self) -> &ResourceSchema {
108        &self.data.as_ref().base
109    }
110}