Skip to main content

asterust_junos/system/
radius_options.rs

1//! `radius-options`
2//! `system { radius-options { ... } }`.
3//!
4//! # See also
5//!
6//! - [radius-options (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/radius-options-edit-system.html)
7
8use crate::{arg_of, child_block, has_leaf};
9use asterust::config::ConfigLine;
10
11/// The `system radius-options` settings.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct RadiusOptions {
15    /// From `attributes nas-id`.
16    pub nas_id: Option<String>,
17    /// From `attributes nas-ip-address`.
18    pub nas_ip_address: Option<String>,
19    pub enhanced_accounting: bool,
20    /// e.g. `mschap-v2`.
21    pub password_protocol: Option<String>,
22}
23
24pub(super) fn parse_radius_options(options: &ConfigLine) -> RadiusOptions {
25    let attributes = child_block(options, "attributes");
26    RadiusOptions {
27        nas_id: attributes.and_then(|a| arg_of(a, "nas-id")).map(str::to_string),
28        nas_ip_address: attributes.and_then(|a| arg_of(a, "nas-ip-address")).map(str::to_string),
29        enhanced_accounting: has_leaf(options, "enhanced-accounting"),
30        password_protocol: arg_of(options, "password-protocol").map(str::to_string),
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use crate::system::system;
37    use asterust::config::juniper;
38
39    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
40    #[test]
41    fn extracts_radius_options() {
42        let options = system(&juniper::parse_braces(
43            r#"
44system {
45    radius-options {
46        attributes {
47            nas-id pe1;
48            nas-ip-address 192.0.2.1;
49        }
50        enhanced-accounting;
51        password-protocol mschap-v2;
52    }
53}
54"#,
55        ))
56        .unwrap()
57        .radius_options
58        .unwrap();
59        assert_eq!(options.nas_id.as_deref(), Some("pe1"));
60        assert_eq!(options.nas_ip_address.as_deref(), Some("192.0.2.1"));
61        assert!(options.enhanced_accounting);
62        assert_eq!(options.password_protocol.as_deref(), Some("mschap-v2"));
63    }
64
65    #[test]
66    fn radius_options_default_to_unset() {
67        let options = system(&juniper::parse_braces(
68            "system {\n    radius-options {\n        enhanced-accounting;\n    }\n}\n",
69        ))
70        .unwrap()
71        .radius_options
72        .unwrap();
73        assert_eq!(options.nas_id, None);
74        assert_eq!(options.nas_ip_address, None);
75        assert_eq!(options.password_protocol, None);
76    }
77
78    #[test]
79    fn no_radius_options_is_none() {
80        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
81        assert!(system.radius_options.is_none());
82    }
83}