asterust-junos 0.5.11

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
//! `tcp-forwarding`
//! `system { services { tcp-forwarding { listening-port <port> listening-address <address> { ... } } } }`.
//!
//! # See also
//!
//! - [tcp-forwarding (Remote Device Management)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/tcp-forwarding-edit-system-services.html)

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

/// A `listening-port <port> listening-address <address>` mapping of TCP port forwarding.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TcpForwardingEntry {
    pub listening_port: String,
    pub listening_address: Option<String>,
    pub allowed_source: Option<String>,
    pub forwarding_address: Option<String>,
    pub forwarding_port: Option<String>,
    pub max_connections: Option<String>,
}

pub(super) fn parse_tcp_forwarding(forwarding: &ConfigLine) -> Vec<TcpForwardingEntry> {
    named_children(forwarding, "listening-port")
        .map(|(arg, entry)| {
            let words: Vec<&str> = arg.split_whitespace().collect();
            TcpForwardingEntry {
                listening_port: words.first().unwrap_or(&"").to_string(),
                listening_address: value_after(&words, "listening-address"),
                allowed_source: arg_of(entry, "allowed-source").map(str::to_string),
                forwarding_address: arg_of(entry, "forwarding-address").map(str::to_string),
                forwarding_port: arg_of(entry, "forwarding-port").map(str::to_string),
                max_connections: arg_of(entry, "max-connections").map(str::to_string),
            }
        })
        .collect()
}

#[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_tcp_forwarding() {
        let entries = system(&juniper::parse_braces(
            r#"
system {
    services {
        tcp-forwarding {
            listening-port 8022 listening-address 192.0.2.1 {
                allowed-source 192.0.2.0/24;
                forwarding-address 10.0.0.5;
                forwarding-port 22;
                max-connections 8;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .tcp_forwarding;
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].listening_port, "8022");
        assert_eq!(entries[0].listening_address.as_deref(), Some("192.0.2.1"));
        assert_eq!(entries[0].allowed_source.as_deref(), Some("192.0.2.0/24"));
        assert_eq!(entries[0].forwarding_address.as_deref(), Some("10.0.0.5"));
        assert_eq!(entries[0].forwarding_port.as_deref(), Some("22"));
        assert_eq!(entries[0].max_connections.as_deref(), Some("8"));
    }

    #[test]
    fn no_tcp_forwarding_is_empty() {
        let services = system(&juniper::parse_braces(
            "system {\n    services {\n        telnet;\n    }\n}\n",
        ))
        .unwrap()
        .services
        .unwrap();
        assert!(services.tcp_forwarding.is_empty());
    }
}