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