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