Skip to main content

nv_redfish/account/
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//! AccountService (Redfish) — high-level wrappers
17//!
18//! Feature: `accounts` (this module is compiled only when the feature is enabled).
19//!
20//! This module provides ergonomic wrappers around the generated Redfish
21//! AccountService model:
22//! - `AccountService`: entry point to manage accounts
23//! - `AccountCollection`: access and create `ManagerAccount` members
24//! - `Account`: operate on an individual `ManagerAccount`
25//!
26//! Vendor compatibility
27//! - Some implementations omit fields marked as `Redfish.Required`.
28//! - This crate can apply read/response patches (see `patch_support`) to keep
29//!   behavior compatible across vendors (for example, defaulting `AccountTypes`).
30//!
31
32/// Collection of accounts.
33mod collection;
34/// Account inside account service.
35mod item;
36
37use crate::patch_support::JsonValue;
38use crate::patch_support::ReadPatchFn;
39use crate::schema::redfish::account_service::AccountService as SchemaAccountService;
40use crate::Error;
41use crate::NvBmc;
42use crate::ServiceRoot;
43use nv_redfish_core::Bmc;
44use nv_redfish_core::EntityTypeRef as _;
45use nv_redfish_core::ODataId;
46use std::sync::Arc;
47
48#[doc(inline)]
49pub use crate::schema::redfish::manager_account::AccountTypes;
50#[doc(inline)]
51pub use crate::schema::redfish::manager_account::ManagerAccountCreate;
52#[doc(inline)]
53pub use crate::schema::redfish::manager_account::ManagerAccountUpdate;
54#[doc(inline)]
55pub use item::Account;
56
57#[doc(inline)]
58pub use collection::AccountCollection;
59#[doc(inline)]
60pub(crate) use collection::SlotDefinedConfig;
61#[doc(inline)]
62pub(crate) use item::Config as AccountConfig;
63
64/// Account service. Provides the ability to manage accounts via Redfish.
65pub struct AccountService<B: Bmc> {
66    collection_config: collection::Config,
67    service: Arc<SchemaAccountService>,
68    bmc: NvBmc<B>,
69}
70
71impl<B: Bmc> AccountService<B> {
72    /// Create a new account service. This is always done by
73    /// `ServiceRoot` object.
74    pub(crate) async fn new(bmc: &NvBmc<B>, root: &ServiceRoot<B>) -> Result<Self, Error<B>> {
75        let service = root
76            .root
77            .account_service
78            .as_ref()
79            .ok_or(Error::AccountServiceNotSupported)?
80            .get(bmc.as_ref())
81            .await
82            .map_err(Error::Bmc)?;
83
84        let mut patches = Vec::new();
85        if root.bug_no_account_type_in_accounts() {
86            patches.push(append_default_account_type);
87        }
88        let account_read_patch_fn = if patches.is_empty() {
89            None
90        } else {
91            let account_read_patch_fn: ReadPatchFn =
92                Arc::new(move |v| patches.iter().fold(v, |acc, f| f(acc)));
93            Some(account_read_patch_fn)
94        };
95        let slot_defined_user_accounts = root.slot_defined_user_accounts();
96        Ok(Self {
97            collection_config: collection::Config {
98                account: AccountConfig {
99                    read_patch_fn: account_read_patch_fn,
100                    disable_account_on_delete: slot_defined_user_accounts
101                        .as_ref()
102                        .is_some_and(|cfg| cfg.disable_account_on_delete),
103                },
104                slot_defined_user_accounts,
105            },
106            service,
107            bmc: bmc.clone(),
108        })
109    }
110
111    /// `OData` identifier of the `AccountService` in Redfish.
112    ///
113    /// Typically `/redfish/v1/AccountService`.
114    #[must_use]
115    pub fn odata_id(&self) -> &ODataId {
116        self.service.as_ref().id()
117    }
118
119    /// Get the accounts collection.
120    ///
121    /// Uses `$expand` to retrieve members in a single request when supported.
122    ///
123    /// # Errors
124    ///
125    /// Returns an error if expanding the collection fails.
126    pub async fn accounts(&self) -> Result<AccountCollection<B>, Error<B>> {
127        let collection_ref = self
128            .service
129            .accounts
130            .as_ref()
131            .ok_or(Error::AccountServiceNotSupported)?;
132
133        AccountCollection::new(
134            self.bmc.clone(),
135            collection_ref,
136            self.collection_config.clone(),
137        )
138        .await
139    }
140}
141
142// `AccountTypes` is marked as `Redfish.Required`, but some systems
143// ignore this requirement. The account service replaces its value with
144// a reasonable default (see below).
145//
146// Note quote from schema: "if this property is not provided by the client, the default value
147// shall be an array that contains the value `Redfish`".
148fn append_default_account_type(v: JsonValue) -> JsonValue {
149    if let JsonValue::Object(mut obj) = v {
150        obj.entry("AccountTypes")
151            .or_insert(JsonValue::Array(vec![JsonValue::String("Redfish".into())]));
152        JsonValue::Object(obj)
153    } else {
154        v
155    }
156}