asterust-junos 0.5.10

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `management-traffic`
//! `system { management-traffic { protocol <name> { priority <priority>; } } }`.
//!
//! # See also
//!
//! - [priority (Management Traffic)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-management-traffic-protocol-priority.html)

use crate::{arg_of, child_block, named_children};
use asterust::config::ConfigLine;

/// A `protocol <name>` of `system management-traffic`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ManagementTrafficProtocol {
    pub name: String,
    /// `low`, `medium` or `high`.
    pub priority: Option<String>,
}

pub(super) fn management_traffic(system: &ConfigLine) -> Vec<ManagementTrafficProtocol> {
    child_block(system, "management-traffic")
        .map(|traffic| {
            named_children(traffic, "protocol")
                .map(|(name, protocol)| ManagementTrafficProtocol {
                    name: name.to_string(),
                    priority: arg_of(protocol, "priority").map(str::to_string),
                })
                .collect()
        })
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_management_traffic_priorities() {
        let protocols = system(&juniper::parse_braces(
            "system {\n    management-traffic {\n        protocol ssh {\n            priority high;\n        }\n        protocol snmp {\n            priority low;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .management_traffic;
        assert_eq!(protocols.len(), 2);
        assert_eq!(protocols[0].name, "ssh");
        assert_eq!(protocols[0].priority.as_deref(), Some("high"));
        assert_eq!(protocols[1].name, "snmp");
        assert_eq!(protocols[1].priority.as_deref(), Some("low"));
    }

    #[test]
    fn no_management_traffic_is_empty() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.management_traffic.is_empty());
    }
}