ferroclass 0.12.0

Hierarchical inventory management tool (reclass compatible)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// SPDX-FileCopyrightText: 2026 Michael Jansen <ferroclass@michael-jansen.biz>
// SPDX-License-Identifier: MPL-2.0

//! Reclass-compatible output adapter.
//!
//! Produces [`InventoryOutput`] (full inventory listing) and
//! [`NodeInfoOutput`] (single-node detail) in the same YAML/JSON format as
//! Python reclass. Includes `__reclass__` metadata, timestamps, and
//! application/class lists per node.

use crate::inventory as inv;
use crate::inventory::value::{Key, Value};
use hashlink::LinkedHashMap;
use serde::ser::{Serialize, SerializeMap, Serializer};
use snafu::prelude::*;
use std::rc::Rc;
use yaml_rust2::Yaml;

use super::{ReclassMap, YamlOutput};

/// Errors that can occur while building a reclass inventory or node-info output.
#[derive(Debug, Snafu)]
pub enum InventoryError {
    #[snafu(transparent)]
    InventoryLoad { source: inv::Error },
}

/// A timestamp wrapper that serializes as `{timestamp: "..."}` in JSON/YAML.
#[derive(Debug, serde::Serialize, Default)]
pub struct ReclassTimestamp {
    timestamp: String,
}

#[derive(Debug)]
struct NodeReclassInfo {
    node: String,
    name: String,
    uri: String,
    environment: String,
    timestamp: String,
}

impl Serialize for NodeReclassInfo {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(5))?;
        map.serialize_entry("node", &self.node)?;
        map.serialize_entry("name", &self.name)?;
        map.serialize_entry("uri", &self.uri)?;
        map.serialize_entry("environment", &self.environment)?;
        map.serialize_entry("timestamp", &self.timestamp)?;
        map.end()
    }
}

fn build_reclass_parameter(
    short_name: &str,
    full_name: &str,
    environment: &dyn std::fmt::Display,
) -> (Key, Value) {
    let mut name_hash: LinkedHashMap<Key, Value> = LinkedHashMap::new();
    name_hash.insert(
        Key::String("short".to_string()),
        Value::String(short_name.to_string()),
    );
    name_hash.insert(
        Key::String("full".to_string()),
        Value::String(full_name.to_string()),
    );
    let mut reclass_hash: LinkedHashMap<Key, Value> = LinkedHashMap::new();
    reclass_hash.insert(
        Key::String("name".to_string()),
        Value::Hash(Rc::new(name_hash)),
    );
    reclass_hash.insert(
        Key::String("environment".to_string()),
        Value::String(environment.to_string()),
    );
    (
        Key::String("_reclass_".to_string()),
        Value::Hash(Rc::new(reclass_hash)),
    )
}

/// A merged node together with a timestamp, serializable in reclass-compatible format.
#[derive(Debug)]
pub struct NodeWithMetadata {
    pub node: inv::Node,
    pub timestamp: String,
}

impl Serialize for NodeWithMetadata {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut parameters: LinkedHashMap<Key, Value> = LinkedHashMap::new();
        let (reclass_key, reclass_value) = build_reclass_parameter(
            self.node.short_name(),
            self.node.name(),
            self.node.environment(),
        );
        parameters.insert(reclass_key, reclass_value);
        for (k, v) in self.node.parameters() {
            parameters.insert(k.clone(), v.clone());
        }
        let mut map = serializer.serialize_map(None)?;
        map.serialize_entry(
            "__reclass__",
            &NodeReclassInfo {
                node: self.node.name().to_string(),
                name: self.node.name().to_string(),
                uri: self.node.uri().unwrap_or("").to_string(),
                environment: self.node.environment().to_string(),
                timestamp: self.timestamp.clone(),
            },
        )?;
        map.serialize_entry("environment", self.node.environment())?;
        map.serialize_entry("classes", self.node.classes())?;
        map.serialize_entry("applications", self.node.applications())?;
        map.serialize_entry("parameters", &ReclassMap(&parameters))?;
        map.serialize_entry("exports", &ReclassMap(self.node.exports()))?;
        map.end()
    }
}

/// Full reclass-compatible inventory output.
///
/// Contains all nodes, classes, and applications with `__reclass__` metadata.
/// Use [`InventoryOutput::from_inventory`] to construct and
/// [`crate::output::format_output`] to serialize.
#[derive(Debug, serde::Serialize, Default)]
pub struct InventoryOutput {
    #[serde(serialize_with = "serialize_reclass_timestamp")]
    pub __reclass__: ReclassTimestamp,
    #[serde(serialize_with = "serialize_nodes")]
    pub nodes: LinkedHashMap<String, NodeWithMetadata>,
    #[serde(serialize_with = "serialize_classes_map")]
    pub classes: LinkedHashMap<String, Vec<String>>,
    #[serde(serialize_with = "serialize_classes_map")]
    pub applications: LinkedHashMap<String, Vec<String>>,
}

fn serialize_reclass_timestamp<S: Serializer>(
    value: &ReclassTimestamp,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(1))?;
    map.serialize_entry("timestamp", &value.timestamp)?;
    map.end()
}

fn serialize_nodes<S: Serializer>(
    value: &LinkedHashMap<String, NodeWithMetadata>,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(value.len()))?;
    for (k, v) in value {
        map.serialize_entry(k, v)?;
    }
    map.end()
}

fn serialize_classes_map<S: Serializer>(
    value: &LinkedHashMap<String, Vec<String>>,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(value.len()))?;
    for (k, v) in value {
        map.serialize_entry(k, v)?;
    }
    map.end()
}

impl InventoryOutput {
    /// Build a full inventory output from the given [`crate::inventory::Inventory`].
    ///
    /// Merges all nodes, collects classes and applications, and resolves
    /// inventory queries. Failed nodes are skipped when `ignore_failed_node`
    /// is true; failed inv-query renders are skipped when
    /// `ignore_failed_render` is true.
    pub fn from_inventory(
        inventory: &inv::Inventory,
        timestamp: &str,
        ignore_failed_node: bool,
        ignore_failed_render: bool,
    ) -> Result<Self, InventoryError> {
        let inv_map = inventory
            .build_inventory_map()
            .map_err(InventoryError::from)?;

        let mut nodes = LinkedHashMap::new();
        let mut classes: LinkedHashMap<String, Vec<String>> = LinkedHashMap::new();
        let mut applications: LinkedHashMap<String, Vec<String>> = LinkedHashMap::new();

        for node in inventory.nodes_iter() {
            let merged = match inventory.merge_node(node.name()) {
                Ok(n) => n,
                Err(e) => {
                    if ignore_failed_node {
                        tracing::warn!("Skipping node '{}': {}", node.name(), e);
                        continue;
                    }
                    return Err(InventoryError::InventoryLoad { source: e });
                }
            };

            let has_inv_query = {
                let params = merged.parameters();
                params.values().any(|v| v.has_inv_query())
                    || merged.exports().values().any(|v| v.has_inv_query())
            };

            let merged = if has_inv_query {
                match inventory.merge_node_with_inventory(node.name(), &inv_map) {
                    Ok(n) => n,
                    Err(e) => {
                        if ignore_failed_render {
                            tracing::warn!(
                                "Skipping node '{}': inv query render failed: {}",
                                node.name(),
                                e
                            );
                            continue;
                        }
                        return Err(InventoryError::InventoryLoad { source: e });
                    }
                }
            } else {
                merged
            };

            tracing::debug!("merged node {}", merged.name());
            for class_name in merged.classes() {
                classes
                    .entry(class_name.clone())
                    .or_insert_with(Vec::new)
                    .push(merged.name().to_string());
            }
            for app_name in merged.applications().as_list() {
                applications
                    .entry(app_name.clone())
                    .or_insert_with(Vec::new)
                    .push(merged.name().to_string());
            }
            nodes.insert(
                merged.name().to_string(),
                NodeWithMetadata {
                    node: merged,
                    timestamp: timestamp.to_string(),
                },
            );
        }
        Ok(Self {
            __reclass__: ReclassTimestamp {
                timestamp: timestamp.to_string(),
            },
            nodes,
            classes,
            applications,
        })
    }

    /// Sort node keys, class lists, and application lists alphabetically.
    ///
    /// Call this before serialization if you need deterministic output
    /// order (e.g. for snapshot tests or diff-friendly output).
    pub fn sort_keys(&mut self) {
        let mut sorted_nodes: Vec<(String, NodeWithMetadata)> = self.nodes.drain().collect();
        sorted_nodes.sort_by(|a, b| a.0.cmp(&b.0));
        for (k, v) in sorted_nodes {
            self.nodes.insert(k, v);
        }
        for nodes in self.classes.values_mut() {
            nodes.sort();
        }
        for nodes in self.applications.values_mut() {
            nodes.sort();
        }
    }
}

impl YamlOutput for InventoryOutput {
    fn to_yaml_value(&self, sorted: bool) -> Yaml {
        let mut reclass_map = LinkedHashMap::new();
        reclass_map.insert(
            Yaml::String("timestamp".to_string()),
            Yaml::String(self.__reclass__.timestamp.clone()),
        );
        let nodes_map: LinkedHashMap<Yaml, Yaml> = if sorted {
            let mut entries: Vec<_> = self.nodes.iter().collect();
            entries.sort_by(|a, b| a.0.cmp(b.0));
            entries
                .into_iter()
                .map(|(k, v)| {
                    (
                        Yaml::String(k.clone()),
                        v.node.to_yaml_value_with_reclass(&v.timestamp, sorted),
                    )
                })
                .collect()
        } else {
            self.nodes
                .iter()
                .map(|(k, v)| {
                    (
                        Yaml::String(k.clone()),
                        v.node.to_yaml_value_with_reclass(&v.timestamp, sorted),
                    )
                })
                .collect()
        };
        let classes_map: LinkedHashMap<Yaml, Yaml> = if sorted {
            let mut entries: Vec<_> = self.classes.iter().collect();
            entries.sort_by(|a, b| a.0.cmp(b.0));
            entries
                .into_iter()
                .map(|(k, v)| {
                    let node_list =
                        Yaml::Array(v.iter().map(|s| Yaml::String(s.clone())).collect());
                    (Yaml::String(k.clone()), node_list)
                })
                .collect()
        } else {
            self.classes
                .iter()
                .map(|(k, v)| {
                    let node_list =
                        Yaml::Array(v.iter().map(|s| Yaml::String(s.clone())).collect());
                    (Yaml::String(k.clone()), node_list)
                })
                .collect()
        };
        let applications_map: LinkedHashMap<Yaml, Yaml> = if sorted {
            let mut entries: Vec<_> = self.applications.iter().collect();
            entries.sort_by(|a, b| a.0.cmp(b.0));
            entries
                .into_iter()
                .map(|(k, v)| {
                    let node_list =
                        Yaml::Array(v.iter().map(|s| Yaml::String(s.clone())).collect());
                    (Yaml::String(k.clone()), node_list)
                })
                .collect()
        } else {
            self.applications
                .iter()
                .map(|(k, v)| {
                    let node_list =
                        Yaml::Array(v.iter().map(|s| Yaml::String(s.clone())).collect());
                    (Yaml::String(k.clone()), node_list)
                })
                .collect()
        };
        let mut map = LinkedHashMap::new();
        map.insert(
            Yaml::String("__reclass__".to_string()),
            Yaml::Hash(reclass_map),
        );
        map.insert(Yaml::String("nodes".to_string()), Yaml::Hash(nodes_map));
        map.insert(Yaml::String("classes".to_string()), Yaml::Hash(classes_map));
        map.insert(
            Yaml::String("applications".to_string()),
            Yaml::Hash(applications_map),
        );
        Yaml::Hash(map)
    }
}

#[derive(Debug, serde::Serialize)]
struct ReclassInfo {
    node: String,
    name: String,
    uri: String,
    environment: String,
    timestamp: String,
}

/// Single-node detail output in reclass-compatible format.
///
/// Contains the resolved node with `__reclass__` metadata, classes,
/// applications, parameters, and exports.
#[derive(Debug, serde::Serialize)]
pub struct NodeInfoOutput {
    #[serde(serialize_with = "serialize_reclass_info")]
    __reclass__: ReclassInfo,
    #[serde(serialize_with = "serialize_classes_list")]
    classes: Vec<String>,
    #[serde(serialize_with = "serialize_classes_list")]
    applications: Vec<String>,
    environment: String,
    #[serde(serialize_with = "serialize_parameters")]
    parameters: LinkedHashMap<Key, Value>,
    #[serde(serialize_with = "serialize_parameters")]
    exports: LinkedHashMap<Key, Value>,
}

fn serialize_reclass_info<S: Serializer>(
    value: &ReclassInfo,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(5))?;
    map.serialize_entry("node", &value.node)?;
    map.serialize_entry("name", &value.name)?;
    map.serialize_entry("uri", &value.uri)?;
    map.serialize_entry("environment", &value.environment)?;
    map.serialize_entry("timestamp", &value.timestamp)?;
    map.end()
}

fn serialize_classes_list<S: Serializer>(
    value: &[String],
    serializer: S,
) -> Result<S::Ok, S::Error> {
    use serde::ser::SerializeSeq;
    let mut seq = serializer.serialize_seq(Some(value.len()))?;
    for item in value {
        seq.serialize_element(item)?;
    }
    seq.end()
}

fn serialize_parameters<S: Serializer>(
    value: &LinkedHashMap<Key, Value>,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let mut map = serializer.serialize_map(Some(value.len()))?;
    for (k, v) in value {
        map.serialize_entry(k, v)?;
    }
    map.end()
}

impl NodeInfoOutput {
    /// Build a node-info output from a single merged node.
    pub fn from_node(node: &inv::Node, timestamp: &str) -> Self {
        let mut parameters = LinkedHashMap::new();
        let (reclass_key, reclass_value) =
            build_reclass_parameter(node.short_name(), node.name(), node.environment());
        parameters.insert(reclass_key, reclass_value);
        for (k, v) in node.parameters() {
            parameters.insert(k.clone(), v.clone());
        }
        Self {
            __reclass__: ReclassInfo {
                node: node.name().to_string(),
                name: node.name().to_string(),
                uri: node.uri().unwrap_or("").to_string(),
                environment: node.environment().to_string(),
                timestamp: timestamp.to_string(),
            },
            classes: node.classes().clone(),
            applications: node.applications().as_list().to_vec(),
            environment: node.environment().to_string(),
            parameters,
            exports: node.exports().clone(),
        }
    }
}

impl YamlOutput for NodeInfoOutput {
    fn to_yaml_value(&self, sorted: bool) -> Yaml {
        use inv::value::Value;
        let mut reclass = LinkedHashMap::new();
        reclass.insert(
            Yaml::String("node".to_string()),
            Yaml::String(self.__reclass__.node.clone()),
        );
        reclass.insert(
            Yaml::String("name".to_string()),
            Yaml::String(self.__reclass__.name.clone()),
        );
        reclass.insert(
            Yaml::String("uri".to_string()),
            Yaml::String(self.__reclass__.uri.clone()),
        );
        reclass.insert(
            Yaml::String("environment".to_string()),
            Yaml::String(self.__reclass__.environment.clone()),
        );
        reclass.insert(
            Yaml::String("timestamp".to_string()),
            Yaml::String(self.__reclass__.timestamp.clone()),
        );
        let mut map = LinkedHashMap::new();
        map.insert(Yaml::String("__reclass__".to_string()), Yaml::Hash(reclass));
        map.insert(
            Yaml::String("classes".to_string()),
            Yaml::Array(
                self.classes
                    .iter()
                    .map(|s| Yaml::String(s.clone()))
                    .collect(),
            ),
        );
        map.insert(
            Yaml::String("applications".to_string()),
            Yaml::Array(
                self.applications
                    .iter()
                    .map(|s| Yaml::String(s.clone()))
                    .collect(),
            ),
        );
        map.insert(
            Yaml::String("environment".to_string()),
            Yaml::String(self.environment.clone()),
        );
        map.insert(
            Yaml::String("parameters".to_string()),
            Value::Hash(std::rc::Rc::new(self.parameters.clone())).to_yaml_value_sorted(sorted),
        );
        map.insert(
            Yaml::String("exports".to_string()),
            Value::Hash(std::rc::Rc::new(self.exports.clone())).to_yaml_value_sorted(sorted),
        );
        Yaml::Hash(map)
    }
}