Skip to main content

asterust_junos/system/services/
telnet.rs

1//! `telnet`
2//! `system { services { telnet { ... } } }`.
3//!
4//! # See also
5//!
6//! - [telnet (Services)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/telnet-edit-system.html)
7
8use crate::{arg_of, args_of, list_values};
9use asterust::config::ConfigLine;
10
11/// The options of `system services telnet`; every field is empty when only `telnet;` is written.
12#[derive(Clone, Debug, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct TelnetService {
15    /// Methods tried in order: `password`, `radius`, `tacplus`.
16    pub authentication_order: Vec<String>,
17    pub connection_limit: Option<String>,
18    pub rate_limit: Option<String>,
19}
20
21pub(super) fn parse_telnet(telnet: &ConfigLine) -> TelnetService {
22    TelnetService {
23        authentication_order: args_of(telnet, "authentication-order")
24            .into_iter()
25            .flat_map(list_values)
26            .collect(),
27        connection_limit: arg_of(telnet, "connection-limit").map(str::to_string),
28        rate_limit: arg_of(telnet, "rate-limit").map(str::to_string),
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::system::system;
35    use asterust::config::juniper;
36
37    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
38    #[test]
39    fn extracts_telnet_options() {
40        let telnet = system(&juniper::parse_braces(
41            "system {\n    services {\n        telnet {\n            authentication-order [ tacplus password ];\n            connection-limit 10;\n            rate-limit 5;\n        }\n    }\n}\n",
42        ))
43        .unwrap()
44        .services
45        .unwrap()
46        .telnet_service
47        .unwrap();
48        assert_eq!(telnet.authentication_order, ["tacplus", "password"]);
49        assert_eq!(telnet.connection_limit.as_deref(), Some("10"));
50        assert_eq!(telnet.rate_limit.as_deref(), Some("5"));
51    }
52
53    #[test]
54    fn bare_telnet_has_no_options() {
55        let services = system(&juniper::parse_braces(
56            "system {\n    services {\n        telnet;\n    }\n}\n",
57        ))
58        .unwrap()
59        .services
60        .unwrap();
61        assert!(services.telnet);
62        assert_eq!(services.telnet_service, Some(Default::default()));
63    }
64}