Skip to main content

asterust_junos/system/services/
dynamic_dns.rs

1//! `dynamic-dns`
2//! `system { services { dynamic-dns { client <hostname> { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [dynamic-dns](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/edit-services-dynamic-dns.html)
7
8use crate::{arg_of, named_children, secret_value, Secret};
9use asterust::config::ConfigLine;
10
11/// A `client <hostname>` of dynamic DNS. The `password` is read only with the `secrets` feature.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct DynamicDnsClient {
15    pub hostname: String,
16    pub agent: Option<String>,
17    pub interface: Option<String>,
18    pub server: Option<String>,
19    pub username: Option<String>,
20    /// `None` unless the `secrets` feature is enabled.
21    pub password: Option<Secret>,
22}
23
24pub(super) fn parse_dynamic_dns(dns: &ConfigLine) -> Vec<DynamicDnsClient> {
25    named_children(dns, "client")
26        .map(|(hostname, client)| DynamicDnsClient {
27            hostname: hostname.to_string(),
28            agent: arg_of(client, "agent").map(str::to_string),
29            interface: arg_of(client, "interface").map(str::to_string),
30            server: arg_of(client, "server").map(str::to_string),
31            username: arg_of(client, "username").map(str::to_string),
32            password: secret_value(arg_of(client, "password")),
33        })
34        .collect()
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::system::system;
40    use asterust::config::juniper;
41
42    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
43    #[test]
44    fn extracts_dynamic_dns_clients_without_the_password() {
45        let clients = system(&juniper::parse_braces(
46            r#"
47system {
48    services {
49        dynamic-dns {
50            client pe1.example.net {
51                agent dyndns;
52                interface ge-0/0/0.0;
53                server ddns.example.net;
54                username ops;
55                password "$9$secret"; ## SECRET-DATA
56            }
57        }
58    }
59}
60"#,
61        ))
62        .unwrap()
63        .services
64        .unwrap()
65        .dynamic_dns;
66        assert_eq!(clients.len(), 1);
67        assert_eq!(clients[0].hostname, "pe1.example.net");
68        assert_eq!(clients[0].agent.as_deref(), Some("dyndns"));
69        assert_eq!(clients[0].interface.as_deref(), Some("ge-0/0/0.0"));
70        assert_eq!(clients[0].server.as_deref(), Some("ddns.example.net"));
71        assert_eq!(clients[0].username.as_deref(), Some("ops"));
72        assert_eq!(
73            clients[0].password.as_ref().map(crate::Secret::expose),
74            crate::secret::expected("$9$secret")
75        );
76    }
77
78    #[test]
79    fn no_dynamic_dns_is_empty() {
80        let services = system(&juniper::parse_braces(
81            "system {\n    services {\n        telnet;\n    }\n}\n",
82        ))
83        .unwrap()
84        .services
85        .unwrap();
86        assert!(services.dynamic_dns.is_empty());
87    }
88}