Skip to main content

asterust_junos/system/
static_host_mapping.rs

1//! `static-host-mapping`
2//! `system { static-host-mapping { <hostname> { ... } } }`.
3//!
4//! # See also
5//!
6//! - [static-host-mapping](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-host-mapping-edit-system.html)
7
8use crate::{active_children, arg_of, args_of, child_block, list_values, unquote};
9use asterust::config::ConfigLine;
10
11/// A `static-host-mapping <hostname>` entry.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct StaticHostMapping {
15    pub hostname: String,
16    pub aliases: Vec<String>,
17    pub inet: Vec<String>,
18    pub inet6: Vec<String>,
19    /// The ISO system identifier, e.g. `0100.2550.0001`.
20    pub sysid: Option<String>,
21}
22
23pub(super) fn static_host_mappings(system: &ConfigLine) -> Vec<StaticHostMapping> {
24    child_block(system, "static-host-mapping")
25        .map(|mappings| active_children(mappings).map(parse_static_host_mapping).collect())
26        .unwrap_or_default()
27}
28
29fn parse_static_host_mapping(mapping: &ConfigLine) -> StaticHostMapping {
30    let list = |keyword: &str| args_of(mapping, keyword).into_iter().flat_map(list_values).collect();
31    StaticHostMapping {
32        hostname: unquote(&mapping.text).to_string(),
33        aliases: list("alias"),
34        inet: list("inet"),
35        inet6: list("inet6"),
36        sysid: arg_of(mapping, "sysid").map(str::to_string),
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use crate::system::system;
43    use asterust::config::juniper;
44
45    #[test]
46    fn extracts_static_host_mappings() {
47        let system = system(&juniper::parse_braces(
48            r#"
49system {
50    static-host-mapping {
51        p1.example.net {
52            inet [ 10.255.0.2 10.255.1.2 ];
53            inet6 2001:db8::2;
54            alias [ p1 core1 ];
55            sysid 0100.2550.0002;
56        }
57        p2.example.net {
58            inet 10.255.0.3;
59        }
60    }
61}
62"#,
63        ))
64        .unwrap();
65        assert_eq!(system.static_host_mappings.len(), 2);
66
67        let p1 = &system.static_host_mappings[0];
68        assert_eq!(p1.hostname, "p1.example.net");
69        assert_eq!(p1.inet, ["10.255.0.2", "10.255.1.2"]);
70        assert_eq!(p1.inet6, ["2001:db8::2"]);
71        assert_eq!(p1.aliases, ["p1", "core1"]);
72        assert_eq!(p1.sysid.as_deref(), Some("0100.2550.0002"));
73
74        let p2 = &system.static_host_mappings[1];
75        assert_eq!(p2.inet, ["10.255.0.3"]);
76        assert!(p2.aliases.is_empty());
77        assert_eq!(p2.sysid, None);
78    }
79}