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