use crate::{arg_of, args_of, child_block, list_values, named_children, secret_value, Secret};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct RemoteDeviceNetconf {
pub bulk_interval: Option<String>,
pub bulk_limit: Option<String>,
pub connection_retry_interval: Option<String>,
pub password: Option<Secret>,
pub port: Option<String>,
pub reconfigure_bulk_limit: Option<String>,
pub response_timeout: Option<String>,
pub response_timeout_count: Option<String>,
pub user_name: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServiceDevice {
pub name: String,
pub access_domain_vlan_ids: Vec<String>,
pub address: Option<String>,
pub dictionary: Option<String>,
pub netconf: Option<RemoteDeviceNetconf>,
}
pub(super) fn parse_remote_device_management(management: &ConfigLine) -> Vec<ServiceDevice> {
named_children(management, "service-device")
.map(|(name, device)| ServiceDevice {
name: name.to_string(),
access_domain_vlan_ids: child_block(device, "access-domain")
.map(|domain| {
args_of(domain, "vlan-id-list")
.into_iter()
.flat_map(list_values)
.collect()
})
.unwrap_or_default(),
address: arg_of(device, "address").map(str::to_string),
dictionary: arg_of(device, "dictionary").map(str::to_string),
netconf: child_block(device, "provisioning-method")
.and_then(|method| child_block(method, "netconf"))
.map(|netconf| RemoteDeviceNetconf {
bulk_interval: arg_of(netconf, "bulk-interval").map(str::to_string),
bulk_limit: arg_of(netconf, "bulk-limit").map(str::to_string),
connection_retry_interval: arg_of(netconf, "connection-retry-interval").map(str::to_string),
password: secret_value(arg_of(netconf, "password")),
port: arg_of(netconf, "port").map(str::to_string),
reconfigure_bulk_limit: arg_of(netconf, "reconfigure-bulk-limit").map(str::to_string),
response_timeout: arg_of(netconf, "response-timeout").map(str::to_string),
response_timeout_count: arg_of(netconf, "response-timeout-count").map(str::to_string),
user_name: arg_of(netconf, "user-name").map(str::to_string),
}),
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_service_devices_without_the_password() {
let devices = system(&juniper::parse_braces(
r#"
system {
services {
remote-device-management {
service-device olt1 {
access-domain {
vlan-id-list [ 100-200 300 ];
}
address 192.0.2.80;
dictionary /var/etc/olt.dict;
provisioning-method {
netconf {
bulk-interval 500;
bulk-limit 20;
connection-retry-interval 30;
password "$9$secret"; ## SECRET-DATA
port 830;
reconfigure-bulk-limit 10;
response-timeout 15;
response-timeout-count 3;
user-name provisioner;
}
}
}
service-device olt2;
}
}
}
"#,
))
.unwrap()
.services
.unwrap()
.remote_device_management;
assert_eq!(devices.len(), 2);
let olt = &devices[0];
assert_eq!(olt.name, "olt1");
assert_eq!(olt.access_domain_vlan_ids, ["100-200", "300"]);
assert_eq!(olt.address.as_deref(), Some("192.0.2.80"));
assert_eq!(olt.dictionary.as_deref(), Some("/var/etc/olt.dict"));
let netconf = olt.netconf.as_ref().unwrap();
assert_eq!(netconf.bulk_interval.as_deref(), Some("500"));
assert_eq!(netconf.bulk_limit.as_deref(), Some("20"));
assert_eq!(netconf.connection_retry_interval.as_deref(), Some("30"));
assert_eq!(netconf.port.as_deref(), Some("830"));
assert_eq!(netconf.reconfigure_bulk_limit.as_deref(), Some("10"));
assert_eq!(netconf.response_timeout.as_deref(), Some("15"));
assert_eq!(netconf.response_timeout_count.as_deref(), Some("3"));
assert_eq!(netconf.user_name.as_deref(), Some("provisioner"));
assert_eq!(
netconf.password.as_ref().map(crate::Secret::expose),
crate::secret::expected("$9$secret")
);
assert!(devices[1].netconf.is_none());
}
#[test]
fn no_remote_device_management_is_empty() {
let services = system(&juniper::parse_braces(
"system {\n services {\n telnet;\n }\n}\n",
))
.unwrap()
.services
.unwrap();
assert!(services.remote_device_management.is_empty());
}
}