nv_redfish/
resource.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//! Redfish resource
17
18use crate::ResourceSchema;
19use tagged_types::TaggedType;
20
21#[cfg(feature = "oem")]
22use crate::oem::OemIdentifier;
23#[cfg(feature = "resource-status")]
24use crate::ResourceStatusSchema;
25#[cfg(feature = "resource-status")]
26use std::convert::identity;
27
28#[doc(inline)]
29#[cfg(feature = "resource-status")]
30pub use crate::schema::redfish::resource::Health;
31
32#[doc(inline)]
33#[cfg(feature = "resource-status")]
34pub use crate::schema::redfish::resource::State;
35
36#[doc(inline)]
37#[cfg(feature = "computer-systems")]
38pub use crate::schema::redfish::resource::PowerState;
39
40/// Redfish resource identifier.
41pub type ResourceId = TaggedType<String, ResourceIdTag>;
42/// Reference to Redfish resource identifier.
43pub type ResourceIdRef<'a> = TaggedType<&'a String, ResourceIdTag>;
44#[doc(hidden)]
45#[derive(tagged_types::Tag)]
46#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
47#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
48#[capability(inner_access, cloned)]
49pub enum ResourceIdTag {}
50
51/// Redfish resource name.
52pub type ResourceName = TaggedType<String, ResourceNameTag>;
53/// Reference to Redfish resource name.
54pub type ResourceNameRef<'a> = TaggedType<&'a String, ResourceNameTag>;
55#[doc(hidden)]
56#[derive(tagged_types::Tag)]
57#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
58#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
59#[capability(inner_access, cloned)]
60pub enum ResourceNameTag {}
61
62/// Redfish resource description.
63pub type ResourceDescription = TaggedType<String, ResourceDescriptionTag>;
64/// Reference to Redfish resource description.
65pub type ResourceDescriptionRef<'a> = TaggedType<&'a String, ResourceDescriptionTag>;
66#[doc(hidden)]
67#[derive(tagged_types::Tag)]
68#[implement(Clone)]
69#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
70#[capability(inner_access, cloned)]
71pub enum ResourceDescriptionTag {}
72
73/// Represents Redfish Resource base type.
74pub trait Resource {
75    /// Required function. Must be implemented for Redfish resources.
76    fn resource_ref(&self) -> &ResourceSchema;
77
78    /// Identifier of the resource.
79    fn id(&self) -> ResourceIdRef<'_> {
80        ResourceIdRef::new(&self.resource_ref().id)
81    }
82
83    /// Name of the resource.
84    fn name(&self) -> ResourceNameRef<'_> {
85        ResourceNameRef::new(&self.resource_ref().name)
86    }
87
88    /// Description of the resource.
89    fn description(&self) -> Option<ResourceDescriptionRef<'_>> {
90        self.resource_ref()
91            .description
92            .as_ref()
93            .and_then(|v| v.as_ref())
94            .map(ResourceDescriptionRef::new)
95    }
96
97    /// OEM identifier if present in the resource.
98    #[cfg(feature = "oem")]
99    fn oem_id(&self) -> Option<OemIdentifier<&String>> {
100        self.resource_ref()
101            .base
102            .oem
103            .as_ref()
104            .and_then(|v| v.additional_properties.as_object())
105            .and_then(|v| v.keys().next())
106            .map(OemIdentifier::new)
107    }
108}
109
110/// The status and health of a resource and its children.
111#[cfg(feature = "resource-status")]
112#[derive(Clone, Debug)]
113pub struct Status {
114    /// The state of the resource.
115    pub state: Option<State>,
116    /// The health state of this resource in the absence of its dependent resources.
117    pub health: Option<Health>,
118    /// The overall health state from the view of this resource.
119    pub health_rollup: Option<Health>,
120}
121
122/// Represents Redfish resource that provides it's status.
123#[cfg(feature = "resource-status")]
124pub trait ResourceProvidesStatus {
125    /// Required function. Must be implemented for Redfish resources
126    /// that provides resource status.
127    fn resource_status_ref(&self) -> Option<&ResourceStatusSchema>;
128
129    /// Status of the resource if it is provided.
130    fn status(&self) -> Option<Status> {
131        self.resource_status_ref().map(|status| Status {
132            state: status.state.and_then(identity),
133            health: status.health.and_then(identity),
134            health_rollup: status.health_rollup.and_then(identity),
135        })
136    }
137}