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