asterust_junos/system/services/
remote_device_management.rs1use crate::{arg_of, args_of, child_block, list_values, named_children, secret_value, Secret};
13use asterust::config::ConfigLine;
14
15#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct RemoteDeviceNetconf {
20 pub bulk_interval: Option<String>,
22 pub bulk_limit: Option<String>,
23 pub connection_retry_interval: Option<String>,
25 pub password: Option<Secret>,
27 pub port: Option<String>,
28 pub reconfigure_bulk_limit: Option<String>,
29 pub response_timeout: Option<String>,
31 pub response_timeout_count: Option<String>,
32 pub user_name: Option<String>,
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct ServiceDevice {
39 pub name: String,
40 pub access_domain_vlan_ids: Vec<String>,
42 pub address: Option<String>,
43 pub dictionary: Option<String>,
44 pub netconf: Option<RemoteDeviceNetconf>,
45}
46
47pub(super) fn parse_remote_device_management(management: &ConfigLine) -> Vec<ServiceDevice> {
48 named_children(management, "service-device")
49 .map(|(name, device)| ServiceDevice {
50 name: name.to_string(),
51 access_domain_vlan_ids: child_block(device, "access-domain")
52 .map(|domain| {
53 args_of(domain, "vlan-id-list")
54 .into_iter()
55 .flat_map(list_values)
56 .collect()
57 })
58 .unwrap_or_default(),
59 address: arg_of(device, "address").map(str::to_string),
60 dictionary: arg_of(device, "dictionary").map(str::to_string),
61 netconf: child_block(device, "provisioning-method")
62 .and_then(|method| child_block(method, "netconf"))
63 .map(|netconf| RemoteDeviceNetconf {
64 bulk_interval: arg_of(netconf, "bulk-interval").map(str::to_string),
65 bulk_limit: arg_of(netconf, "bulk-limit").map(str::to_string),
66 connection_retry_interval: arg_of(netconf, "connection-retry-interval").map(str::to_string),
67 password: secret_value(arg_of(netconf, "password")),
68 port: arg_of(netconf, "port").map(str::to_string),
69 reconfigure_bulk_limit: arg_of(netconf, "reconfigure-bulk-limit").map(str::to_string),
70 response_timeout: arg_of(netconf, "response-timeout").map(str::to_string),
71 response_timeout_count: arg_of(netconf, "response-timeout-count").map(str::to_string),
72 user_name: arg_of(netconf, "user-name").map(str::to_string),
73 }),
74 })
75 .collect()
76}
77
78#[cfg(test)]
79mod tests {
80 use crate::system::system;
81 use asterust::config::juniper;
82
83 #[test]
85 fn extracts_service_devices_without_the_password() {
86 let devices = system(&juniper::parse_braces(
87 r#"
88system {
89 services {
90 remote-device-management {
91 service-device olt1 {
92 access-domain {
93 vlan-id-list [ 100-200 300 ];
94 }
95 address 192.0.2.80;
96 dictionary /var/etc/olt.dict;
97 provisioning-method {
98 netconf {
99 bulk-interval 500;
100 bulk-limit 20;
101 connection-retry-interval 30;
102 password "$9$secret"; ## SECRET-DATA
103 port 830;
104 reconfigure-bulk-limit 10;
105 response-timeout 15;
106 response-timeout-count 3;
107 user-name provisioner;
108 }
109 }
110 }
111 service-device olt2;
112 }
113 }
114}
115"#,
116 ))
117 .unwrap()
118 .services
119 .unwrap()
120 .remote_device_management;
121 assert_eq!(devices.len(), 2);
122 let olt = &devices[0];
123 assert_eq!(olt.name, "olt1");
124 assert_eq!(olt.access_domain_vlan_ids, ["100-200", "300"]);
125 assert_eq!(olt.address.as_deref(), Some("192.0.2.80"));
126 assert_eq!(olt.dictionary.as_deref(), Some("/var/etc/olt.dict"));
127 let netconf = olt.netconf.as_ref().unwrap();
128 assert_eq!(netconf.bulk_interval.as_deref(), Some("500"));
129 assert_eq!(netconf.bulk_limit.as_deref(), Some("20"));
130 assert_eq!(netconf.connection_retry_interval.as_deref(), Some("30"));
131 assert_eq!(netconf.port.as_deref(), Some("830"));
132 assert_eq!(netconf.reconfigure_bulk_limit.as_deref(), Some("10"));
133 assert_eq!(netconf.response_timeout.as_deref(), Some("15"));
134 assert_eq!(netconf.response_timeout_count.as_deref(), Some("3"));
135 assert_eq!(netconf.user_name.as_deref(), Some("provisioner"));
136 assert_eq!(
137 netconf.password.as_ref().map(crate::Secret::expose),
138 crate::secret::expected("$9$secret")
139 );
140 assert!(devices[1].netconf.is_none());
141 }
142
143 #[test]
144 fn no_remote_device_management_is_empty() {
145 let services = system(&juniper::parse_braces(
146 "system {\n services {\n telnet;\n }\n}\n",
147 ))
148 .unwrap()
149 .services
150 .unwrap();
151 assert!(services.remote_device_management.is_empty());
152 }
153}