Skip to main content

nv_redfish/computer_system/
storage.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::computer_system::Drive;
17use crate::schema::redfish::storage::Storage as StorageSchema;
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/// Represents a storage controller in a computer system.
27///
28/// Provides access to storage controller information and associated drives.
29pub struct Storage<B: Bmc> {
30    bmc: NvBmc<B>,
31    data: Arc<StorageSchema>,
32}
33
34impl<B: Bmc> Storage<B> {
35    /// Create a new storage handle.
36    pub(crate) async fn new(
37        bmc: &NvBmc<B>,
38        nav: &NavProperty<StorageSchema>,
39    ) -> Result<Self, Error<B>> {
40        nav.get(bmc.as_ref())
41            .await
42            .map_err(Error::Bmc)
43            .map(|data| Self {
44                bmc: bmc.clone(),
45                data,
46            })
47    }
48
49    /// Get the raw schema data for this storage controller.
50    ///
51    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
52    /// and sharing of the data.
53    #[must_use]
54    pub fn raw(&self) -> Arc<StorageSchema> {
55        self.data.clone()
56    }
57
58    /// Get drives associated with this storage controller.
59    ///
60    /// Fetches the drive collection and returns a list of [`Drive`] handles.
61    ///
62    /// # Errors
63    ///
64    /// Returns an error if:
65    /// - The storage controller does not have drives
66    /// - Fetching drive data fails
67    pub async fn drives(&self) -> Result<Vec<Drive<B>>, Error<B>> {
68        let drives_ref = self
69            .data
70            .drives
71            .as_ref()
72            .ok_or(Error::StorageNotAvailable)?;
73
74        let mut drives = Vec::new();
75        for d in drives_ref {
76            drives.push(Drive::new(&self.bmc, d).await?);
77        }
78
79        Ok(drives)
80    }
81}
82
83impl<B: Bmc> Resource for Storage<B> {
84    fn resource_ref(&self) -> &ResourceSchema {
85        &self.data.as_ref().base
86    }
87}