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
//! `health-monitor`
//! `system { health-monitor { ... } }`.
//!
//! # See also
//!
//! - [health-monitor (KHMS)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/health-monitor-edit-system.html)

use crate::statements;
use asterust::config::ConfigLine;

/// The `system health-monitor` kernel health monitoring system (KHMS) settings.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HealthMonitor {
    /// `peer-stuck`, `non-peer-stuck` or `all-clients-stuck`.
    pub ifstate_clients: Option<String>,
    /// `low`, `medium` or `high`.
    pub threshold_level: Option<String>,
    /// `alarm`, `alarm-with-cores` or `restart`.
    pub action: Option<String>,
}

pub(super) fn parse_health_monitor(monitor: &ConfigLine) -> HealthMonitor {
    let first = |keyword: &str| statements(monitor, keyword).first().map(|s| s.to_string());
    HealthMonitor {
        ifstate_clients: first("ifstate-clients"),
        threshold_level: first("threshold-level"),
        action: first("action"),
    }
}

#[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_the_health_monitor() {
        let monitor = system(&juniper::parse_braces(
            "system {\n    health-monitor {\n        ifstate-clients {\n            peer-stuck;\n        }\n        threshold-level {\n            medium;\n        }\n        action {\n            alarm-with-cores;\n        }\n    }\n}\n",
        ))
        .unwrap()
        .health_monitor
        .unwrap();
        assert_eq!(monitor.ifstate_clients.as_deref(), Some("peer-stuck"));
        assert_eq!(monitor.threshold_level.as_deref(), Some("medium"));
        assert_eq!(monitor.action.as_deref(), Some("alarm-with-cores"));
    }

    #[test]
    fn health_monitor_accepts_the_inline_form() {
        let monitor = system(&juniper::parse_braces(
            "system {\n    health-monitor {\n        action restart;\n    }\n}\n",
        ))
        .unwrap()
        .health_monitor
        .unwrap();
        assert_eq!(monitor.action.as_deref(), Some("restart"));
        assert_eq!(monitor.ifstate_clients, None);
    }

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