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

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

/// An `address` of an outbound-https client.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OutboundHttpsAddress {
    pub address: String,
    pub port: Option<String>,
    pub trusted_cert: Option<String>,
}

/// An `outbound-https client <id>`. The `secret` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct OutboundHttpsClient {
    pub id: String,
    pub addresses: Vec<OutboundHttpsAddress>,
    pub device_id: Option<String>,
    /// `in-order` or `sticky`.
    pub reconnect_strategy: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub secret: Option<Secret>,
    /// Seconds.
    pub waittime: Option<String>,
}

pub(super) fn parse_outbound_https(outbound: &ConfigLine) -> Vec<OutboundHttpsClient> {
    named_children(outbound, "client")
        .map(|(id, client)| OutboundHttpsClient {
            id: id.to_string(),
            addresses: named_children(client, "address")
                .map(|(address, block)| OutboundHttpsAddress {
                    address: address.to_string(),
                    port: arg_of(block, "port").map(str::to_string),
                    trusted_cert: arg_of(block, "trusted-cert").map(str::to_string),
                })
                .collect(),
            device_id: arg_of(client, "device-id").map(str::to_string),
            reconnect_strategy: arg_of(client, "reconnect-strategy").map(str::to_string),
            secret: secret_value(arg_of(client, "secret")),
            waittime: arg_of(client, "waittime").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_outbound_https_clients_without_the_secret() {
        let clients = system(&juniper::parse_braces(
            r#"
system {
    services {
        outbound-https {
            client mgr1 {
                address 192.0.2.50 {
                    port 443;
                    trusted-cert mgr-ca;
                }
                device-id pe1;
                reconnect-strategy sticky;
                secret "$9$secret"; ## SECRET-DATA
                waittime 15;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .outbound_https;
        assert_eq!(clients.len(), 1);
        let client = &clients[0];
        assert_eq!(client.id, "mgr1");
        assert_eq!(client.addresses[0].address, "192.0.2.50");
        assert_eq!(client.addresses[0].port.as_deref(), Some("443"));
        assert_eq!(client.addresses[0].trusted_cert.as_deref(), Some("mgr-ca"));
        assert_eq!(client.device_id.as_deref(), Some("pe1"));
        assert_eq!(client.reconnect_strategy.as_deref(), Some("sticky"));
        assert_eq!(client.waittime.as_deref(), Some("15"));
        assert_eq!(
            client.secret.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
    }

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