use crate::{arg_of, named_children, secret_value, Secret};
use asterust::config::ConfigLine;
#[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>,
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;
#[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());
}
}