libredfish 0.2.0

A redfish library. Useful for querying server hardware from a BMC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
use std::{fmt, str::FromStr};

use serde::{Deserialize, Serialize};
pub mod manager;
pub mod resource;
pub use manager::*;
pub use resource::OData;
pub mod serial_interface;

pub mod system;
pub use system::*;

pub mod bios;
pub mod boot;
pub use bios::*;

use crate::RedfishError;

pub mod oem;
pub mod secure_boot;

pub mod account_service;
pub mod certificate;
pub mod chassis;
pub mod component_integrity;
pub mod error;
pub mod ethernet_interface;
pub mod job;
pub mod manager_network_protocol;
pub mod network_device_function;
pub mod port;
pub mod power;
pub mod sel;
pub mod sensor;
pub mod service_root;
pub mod software_inventory;
pub mod storage;
pub mod task;
pub mod thermal;
pub mod update_service;

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ODataLinks {
    #[serde(rename = "@odata.context")]
    pub odata_context: Option<String>,
    #[serde(rename = "@odata.id")]
    pub odata_id: String,
    #[serde(rename = "@odata.type")]
    pub odata_type: String,
    #[serde(rename = "@odata.etag")]
    pub odata_etag: Option<String>,
    #[serde(rename = "links")]
    pub links: Option<LinkType>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Href {
    pub href: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExtRef {
    pub extref: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged, rename_all = "PascalCase")]
pub enum LinkType {
    SelfLink {
        #[serde(rename = "self")]
        self_url: Href,
    },
    HpLink {
        fast_power_meter: Href,
        federated_group_capping: Href,
        power_meter: Href,
    },
    OemHpLink {
        active_health_system: Href,
        date_time_service: Href,
        embedded_media_service: Href,
        federation_dispatch: ExtRef,
        federation_groups: Href,
        federation_peers: Href,
        license_service: Href,
        security_service: Href,
        update_service: Href,
        #[serde(rename = "VSPLogLocation")]
        vsp_log_location: ExtRef,
    },
    SerdeJson {
        #[serde(rename = "links")]
        links: serde_json::Value,
    },
    EnclosuresLinks {
        member: Vec<Href>,
        #[serde(rename = "self")]
        self_url: Href,
    },
    ManagerLink {
        #[serde(rename = "EthernetNICs")]
        ethernet_nics: Href,
        logs: Href,
        manager_for_chassis: Vec<Href>,
        manager_for_servers: Vec<Href>,
        network_service: Href,
        virtual_media: Href,
        #[serde(rename = "self")]
        self_url: Href,
    },
    StorageLink {
        logical_drives: Href,
        physical_drives: Href,
        storage_enclosures: Href,
        unconfigured_drives: Href,
        #[serde(rename = "self")]
        self_url: Href,
    },
}

#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct ODataId {
    #[serde(rename = "@odata.id")]
    pub odata_id: String,
}

impl From<String> for ODataId {
    fn from(item: String) -> Self {
        ODataId { odata_id: item }
    }
}

impl From<&str> for ODataId {
    fn from(item: &str) -> Self {
        ODataId {
            odata_id: item.to_string(),
        }
    }
}

impl ODataId {
    // Gets last portion of the ID, not including uri path
    pub fn odata_id_get(&self) -> Result<&str, RedfishError> {
        self.odata_id
            .split('/')
            .next_back()
            .ok_or_else(|| RedfishError::GenericError {
                error: format!("odata_id have invalid format: {}", self.odata_id),
            })
    }
}

// This is Redfish spec defined object that is required to
// make changes to underlying resource
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct RedfishSettings {
    pub e_tag: Option<String>,
    #[serde(rename = "@odata.type")]
    pub odata_type: Option<String>,
    pub messages: Option<Vec<String>>,
    pub time: Option<String>,
    #[serde(rename = "SettingsObject")]
    pub settings_object: Option<ODataId>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ODataContext {
    #[serde(rename = "@odata.context")]
    pub odata_context: String,
    #[serde(rename = "links")]
    pub links: LinkType,
}

#[derive(Debug, Default, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
pub enum EnabledDisabled {
    #[default]
    Enabled,
    Disabled,
}

impl EnabledDisabled {
    pub fn is_enabled(self) -> bool {
        self == EnabledDisabled::Enabled
    }
}

impl fmt::Display for EnabledDisabled {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl FromStr for EnabledDisabled {
    type Err = InvalidValueError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Enabled" => Ok(Self::Enabled),
            "Disabled" => Ok(Self::Disabled),
            x => Err(InvalidValueError(format!(
                "Invalid EnabledDisabled value: {x}"
            ))),
        }
    }
}

impl From<EnabledDisabled> for serde_json::Value {
    fn from(val: EnabledDisabled) -> Self {
        serde_json::Value::String(val.to_string())
    }
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
pub enum EnableDisable {
    Enable,
    Disable,
}

impl EnableDisable {
    pub fn is_enabled(self) -> bool {
        self == EnableDisable::Enable
    }
}

impl fmt::Display for EnableDisable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl FromStr for EnableDisable {
    type Err = InvalidValueError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Enable" => Ok(Self::Enable),
            "Disable" => Ok(Self::Disable),
            x => Err(InvalidValueError(format!(
                "Invalid EnableDisable value: {x}"
            ))),
        }
    }
}

impl From<EnableDisable> for serde_json::Value {
    fn from(val: EnableDisable) -> Self {
        serde_json::Value::String(val.to_string())
    }
}

#[derive(Debug, Default, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
pub enum YesNo {
    #[default]
    Yes,
    No,
}

impl YesNo {
    pub fn is_enabled(self) -> bool {
        self == YesNo::Yes
    }
}

impl fmt::Display for YesNo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl FromStr for YesNo {
    type Err = InvalidValueError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Yes" => Ok(Self::Yes),
            "No" => Ok(Self::No),
            x => Err(InvalidValueError(format!("Invalid YesNo value: {x}"))),
        }
    }
}

impl From<YesNo> for serde_json::Value {
    fn from(val: YesNo) -> Self {
        serde_json::Value::String(val.to_string())
    }
}

#[derive(Debug)]
pub struct InvalidValueError(pub String);

impl std::error::Error for InvalidValueError {}

impl fmt::Display for InvalidValueError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Default)]
pub enum OnOff {
    On,
    #[default]
    Off,
    Reset,
}

impl fmt::Display for OnOff {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
pub enum LinkStatus {
    LinkUp,
    NoLink,
    LinkDown,
}

impl fmt::Display for LinkStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FirmwareCurrent {
    #[serde(rename = "VersionString")]
    pub version: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Firmware {
    pub current: FirmwareCurrent,
}

pub trait StatusVec {
    fn get_vec(&self) -> Vec<ResourceStatus>;
}

#[derive(Default, Debug, Serialize, Deserialize, Copy, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct ResourceStatus {
    pub health: Option<ResourceHealth>,
    pub health_rollup: Option<ResourceHealth>,
    pub state: Option<ResourceState>,
}

/// Health and State of a disk drive, fan, power supply, etc
/// Defined in Resource_v1.xml
#[derive(Debug, Serialize, Deserialize, Copy, Clone, Default)]
pub enum ResourceHealth {
    #[serde(rename = "OK")]
    #[default]
    Ok,
    Warning,
    Critical,
    Informational, // HP only, non-standard
}

impl fmt::Display for ResourceHealth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

// Defined in Resource_v1.xml
#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
pub enum ResourceState {
    Enabled,
    Disabled,
    Degraded,
    Standby,
    StandbyOffline,
    StandbySpare,
    InTest,
    Starting,
    Absent,
    UnavailableOffline,
    Deferring,
    Quiesced,
    Updating,
    Qualified,
    Unknown,
}

impl fmt::Display for ResourceState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

/// https://redfish.dmtf.org/schemas/v1/Message.v1_1_2.json
/// The message that the Redfish service returns.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Message {
    pub message: String,
    #[serde(default)]
    pub message_args: Vec<String>,
    pub message_id: String,
    pub resolution: Option<String>,
    pub severity: Option<String>,
}