Skip to main content

nv_redfish/log_service/
mod.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//! Log Service entities and collections.
17//!
18//! This module provides types for working with Redfish LogService resources
19//! and their log entries.
20
21use crate::schema::redfish::log_entry::LogEntry;
22use crate::schema::redfish::log_service::LogService as LogServiceSchema;
23use crate::Error;
24use crate::NvBmc;
25use crate::Resource;
26use crate::ResourceSchema;
27use nv_redfish_core::Bmc;
28use nv_redfish_core::NavProperty;
29use std::sync::Arc;
30
31/// Log service.
32///
33/// Provides functions to access log entries and perform log operations.
34pub struct LogService<B: Bmc> {
35    bmc: NvBmc<B>,
36    data: Arc<LogServiceSchema>,
37}
38
39impl<B: Bmc> LogService<B> {
40    /// Create a new log service handle.
41    pub(crate) async fn new(
42        bmc: &NvBmc<B>,
43        nav: &NavProperty<LogServiceSchema>,
44    ) -> Result<Self, Error<B>> {
45        nav.get(bmc.as_ref())
46            .await
47            .map_err(crate::Error::Bmc)
48            .map(|data| Self {
49                bmc: bmc.clone(),
50                data,
51            })
52    }
53
54    /// Get the raw schema data for this log service.
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<LogServiceSchema> {
60        self.data.clone()
61    }
62
63    /// List all log entries.
64    ///
65    /// # Errors
66    ///
67    /// Returns an error if:
68    /// - The log service does not have a log entries collection
69    /// - Fetching log entries data fails
70    pub async fn entries(&self) -> Result<Option<Vec<Arc<LogEntry>>>, Error<B>> {
71        if let Some(entries_ref) = &self.data.entries {
72            let entries_collection = self.bmc.expand_property(entries_ref).await?;
73            self.expand_entries(&entries_collection.members)
74                .await
75                .map(Some)
76        } else {
77            Ok(None)
78        }
79    }
80
81    /// Filter log entries using `OData` filter query.
82    ///
83    /// # Errors
84    ///
85    /// Returns an error if:
86    /// - The log service does not have a log entries collection
87    /// - Filtering log entries data fails
88    pub async fn filter_entries(
89        &self,
90        filter: nv_redfish_core::FilterQuery,
91    ) -> Result<Option<Vec<Arc<LogEntry>>>, Error<B>> {
92        if let Some(entries_ref) = &self.data.entries {
93            let entries_collection = entries_ref
94                .filter(self.bmc.as_ref(), filter)
95                .await
96                .map_err(Error::Bmc)?;
97
98            self.expand_entries(&entries_collection.members)
99                .await
100                .map(Some)
101        } else {
102            Ok(None)
103        }
104    }
105
106    /// Clear all log entries.
107    ///
108    /// # Arguments
109    ///
110    /// * `log_entry_codes` - Optional log entry codes to clear specific entries
111    ///
112    /// # Errors
113    ///
114    /// Returns an error if:
115    /// - The log service does not support the `ClearLog` action
116    /// - The action execution fails
117    pub async fn clear_log(&self, log_entry_codes: Option<String>) -> Result<(), Error<B>>
118    where
119        B::Error: nv_redfish_core::ActionError,
120    {
121        let actions = self
122            .data
123            .actions
124            .as_ref()
125            .ok_or(Error::ActionNotAvailable)?;
126
127        actions
128            .clear_log(self.bmc.as_ref(), log_entry_codes)
129            .await
130            .map_err(Error::Bmc)?;
131
132        Ok(())
133    }
134
135    /// This unwraps `NavProperty`, usually all BMC already have them expanded, so we do not expect network IO here
136    async fn expand_entries(
137        &self,
138        entry_refs: &[NavProperty<LogEntry>],
139    ) -> Result<Vec<Arc<LogEntry>>, Error<B>> {
140        let mut entries = Vec::new();
141        for entry_ref in entry_refs {
142            let entry = entry_ref.get(self.bmc.as_ref()).await.map_err(Error::Bmc)?;
143            entries.push(entry);
144        }
145        Ok(entries)
146    }
147}
148
149impl<B: Bmc> Resource for LogService<B> {
150    fn resource_ref(&self) -> &ResourceSchema {
151        &self.data.as_ref().base
152    }
153}