Skip to main content

asterust_junos/interfaces/auto_configure/
mod.rs

1//! `auto-configure`
2//! `interfaces { <name> { auto-configure { ... } } }`.
3//!
4//! # See also
5//!
6//! - [auto-configure](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/auto-configure-edit-interfaces.html)
7
8mod ranges;
9
10pub use ranges::{AutoConfigureProfile, AutoConfigureRanges};
11
12use crate::{child_block, has_leaf};
13use asterust::config::ConfigLine;
14use ranges::parse_ranges;
15
16/// The `auto-configure` settings that create VLAN interfaces dynamically.
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[non_exhaustive]
19pub struct AutoConfigure {
20    pub vlan_ranges: Option<AutoConfigureRanges>,
21    pub stacked_vlan_ranges: Option<AutoConfigureRanges>,
22    pub remove_when_no_subscribers: bool,
23}
24
25pub(super) fn parse_auto_configure(auto_configure: &ConfigLine) -> AutoConfigure {
26    AutoConfigure {
27        vlan_ranges: child_block(auto_configure, "vlan-ranges").map(parse_ranges),
28        stacked_vlan_ranges: child_block(auto_configure, "stacked-vlan-ranges").map(parse_ranges),
29        remove_when_no_subscribers: has_leaf(auto_configure, "remove-when-no-subscribers"),
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::interfaces::interfaces;
36    use asterust::config::juniper;
37
38    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
39    const CONFIG: &str = r#"
40interfaces {
41    ge-1/0/0 {
42        auto-configure {
43            remove-when-no-subscribers;
44            vlan-ranges {
45                access-profile AP;
46                authentication {
47                    password "$9$secret"; ## SECRET-DATA
48                    username-include {
49                        mac-address;
50                    }
51                }
52                dynamic-profile DP1 {
53                    accept dhcp-v4;
54                    accept pppoe;
55                    access-profile DAP;
56                    ranges 100-200;
57                }
58                dynamic-profile DP2 {
59                    ranges any-any;
60                }
61            }
62            stacked-vlan-ranges {
63                dynamic-profile SDP {
64                    accept any;
65                    ranges 100-200,any;
66                }
67            }
68        }
69    }
70}
71"#;
72
73    #[test]
74    fn extracts_vlan_ranges_without_the_password() {
75        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
76        let auto = ifaces[0].auto_configure.as_ref().unwrap();
77        assert!(auto.remove_when_no_subscribers);
78
79        let ranges = auto.vlan_ranges.as_ref().unwrap();
80        assert_eq!(ranges.access_profile.as_deref(), Some("AP"));
81        assert!(ranges.has_authentication);
82        assert_eq!(ranges.dynamic_profiles.len(), 2);
83
84        let profile = &ranges.dynamic_profiles[0];
85        assert_eq!(profile.name, "DP1");
86        assert_eq!(profile.accept, ["dhcp-v4", "pppoe"]);
87        assert_eq!(profile.access_profile.as_deref(), Some("DAP"));
88        assert_eq!(profile.ranges.as_deref(), Some("100-200"));
89
90        let bare = &ranges.dynamic_profiles[1];
91        assert!(bare.accept.is_empty());
92        assert_eq!(bare.ranges.as_deref(), Some("any-any"));
93    }
94
95    #[test]
96    fn extracts_stacked_vlan_ranges() {
97        let ifaces = interfaces(&juniper::parse_braces(CONFIG));
98        let stacked = ifaces[0]
99            .auto_configure
100            .as_ref()
101            .unwrap()
102            .stacked_vlan_ranges
103            .as_ref()
104            .unwrap();
105        assert_eq!(stacked.access_profile, None);
106        assert!(!stacked.has_authentication);
107        assert_eq!(stacked.dynamic_profiles[0].name, "SDP");
108        assert_eq!(stacked.dynamic_profiles[0].accept, ["any"]);
109        assert_eq!(stacked.dynamic_profiles[0].ranges.as_deref(), Some("100-200,any"));
110    }
111
112    #[test]
113    fn auto_configure_is_optional() {
114        let ifaces = interfaces(&juniper::parse_braces(
115            "interfaces {\n    ge-0/0/0 {\n        disable;\n    }\n}\n",
116        ));
117        assert!(ifaces[0].auto_configure.is_none());
118    }
119}