asterust_junos/system/
autoinstallation.rs1use super::archival::redact_password;
9use crate::{active_children, child_block, has_leaf, unquote};
10use asterust::config::ConfigLine;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub struct AutoinstallationInterface {
16 pub name: String,
17 pub bootp: bool,
18 pub rarp: bool,
19}
20
21#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct Autoinstallation {
26 pub configuration_servers: Vec<String>,
28 pub interfaces: Vec<AutoinstallationInterface>,
29 pub usb_disabled: bool,
31}
32
33pub(super) fn parse_autoinstallation(autoinstallation: &ConfigLine) -> Autoinstallation {
34 Autoinstallation {
35 configuration_servers: child_block(autoinstallation, "configuration-servers")
36 .map(|servers| {
37 active_children(servers)
38 .filter_map(|server| server.text.split_whitespace().next())
39 .map(|url| redact_password(unquote(url)))
40 .collect()
41 })
42 .unwrap_or_default(),
43 interfaces: child_block(autoinstallation, "interfaces")
44 .map(|interfaces| {
45 active_children(interfaces)
46 .map(|interface| AutoinstallationInterface {
47 name: interface.text.clone(),
48 bootp: has_leaf(interface, "bootp"),
49 rarp: has_leaf(interface, "rarp"),
50 })
51 .collect()
52 })
53 .unwrap_or_default(),
54 usb_disabled: child_block(autoinstallation, "usb").is_some_and(|usb| has_leaf(usb, "disable")),
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use crate::system::system;
61 use asterust::config::juniper;
62
63 #[test]
65 fn extracts_autoinstallation_settings() {
66 let autoinstallation = system(&juniper::parse_braces(
67 r#"
68system {
69 autoinstallation {
70 configuration-servers {
71 tftp://192.0.2.10/config;
72 "ftp://svc:hunter2@192.0.2.11/config" {
73 password "$9$secret"; ## SECRET-DATA
74 }
75 }
76 interfaces {
77 ge-0/0/0 {
78 bootp;
79 }
80 ge-0/0/1 {
81 bootp;
82 rarp;
83 }
84 }
85 usb {
86 disable;
87 }
88 }
89}
90"#,
91 ))
92 .unwrap()
93 .autoinstallation
94 .unwrap();
95 assert_eq!(
96 autoinstallation.configuration_servers,
97 ["tftp://192.0.2.10/config", "ftp://svc@192.0.2.11/config"]
98 );
99 assert_eq!(autoinstallation.interfaces.len(), 2);
100 assert_eq!(autoinstallation.interfaces[0].name, "ge-0/0/0");
101 assert!(autoinstallation.interfaces[0].bootp);
102 assert!(!autoinstallation.interfaces[0].rarp);
103 assert!(autoinstallation.interfaces[1].rarp);
104 assert!(autoinstallation.usb_disabled);
105 }
106
107 #[test]
108 fn bare_autoinstallation_is_enabled_with_no_settings() {
109 let autoinstallation = system(&juniper::parse_braces("system {\n autoinstallation;\n}\n"))
110 .unwrap()
111 .autoinstallation
112 .unwrap();
113 assert!(autoinstallation.configuration_servers.is_empty());
114 assert!(autoinstallation.interfaces.is_empty());
115 assert!(!autoinstallation.usb_disabled);
116 }
117
118 #[test]
119 fn no_autoinstallation_is_none() {
120 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
121 assert!(system.autoinstallation.is_none());
122 }
123}