asterust-junos 0.5.13

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

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

/// A `client <hostname>` of dynamic DNS. The `password` is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct DynamicDnsClient {
    pub hostname: String,
    pub agent: Option<String>,
    pub interface: Option<String>,
    pub server: Option<String>,
    pub username: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub password: Option<Secret>,
}

pub(super) fn parse_dynamic_dns(dns: &ConfigLine) -> Vec<DynamicDnsClient> {
    named_children(dns, "client")
        .map(|(hostname, client)| DynamicDnsClient {
            hostname: hostname.to_string(),
            agent: arg_of(client, "agent").map(str::to_string),
            interface: arg_of(client, "interface").map(str::to_string),
            server: arg_of(client, "server").map(str::to_string),
            username: arg_of(client, "username").map(str::to_string),
            password: secret_value(arg_of(client, "password")),
        })
        .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_dynamic_dns_clients_without_the_password() {
        let clients = system(&juniper::parse_braces(
            r#"
system {
    services {
        dynamic-dns {
            client pe1.example.net {
                agent dyndns;
                interface ge-0/0/0.0;
                server ddns.example.net;
                username ops;
                password "$9$secret"; ## SECRET-DATA
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .services
        .unwrap()
        .dynamic_dns;
        assert_eq!(clients.len(), 1);
        assert_eq!(clients[0].hostname, "pe1.example.net");
        assert_eq!(clients[0].agent.as_deref(), Some("dyndns"));
        assert_eq!(clients[0].interface.as_deref(), Some("ge-0/0/0.0"));
        assert_eq!(clients[0].server.as_deref(), Some("ddns.example.net"));
        assert_eq!(clients[0].username.as_deref(), Some("ops"));
        assert_eq!(
            clients[0].password.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
    }

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