1mod accounting;
4mod addressing;
5mod archival;
6mod auth;
7mod internet;
8mod location;
9mod login;
10mod ntp;
11mod ports;
12mod services;
13mod syslog;
14
15pub use accounting::{Accounting, AccountingDestination};
16pub use addressing::{BackupRouter, NameServer, StaticHostMapping};
17pub use archival::{Archival, ArchivalConfiguration};
18pub use auth::{RadiusServer, RootAuthentication, TacplusServer};
19pub use internet::{IcmpRateLimit, InternetOptions};
20pub use location::Location;
21pub use login::{Login, LoginClass, LoginPassword, LoginRetryOptions, LoginUser};
22pub use ntp::{Ntp, NtpAuthenticationKey, NtpHost};
23pub use ports::{Auxiliary, Console, Ports};
24pub use services::{OutboundSshClient, OutboundSshServer, Services, SshService};
25pub use syslog::{Syslog, SyslogArchive, SyslogHost, SyslogRule, SyslogStructuredData, SyslogTarget};
26
27use crate::{arg_of, args_of, child_block, has_leaf, list_values, top_level};
28use accounting::parse_accounting;
29use addressing::{name_servers, parse_backup_router, static_host_mappings};
30use archival::parse_archival;
31use asterust::config::Config;
32use auth::{radius_servers, root_authentication, tacplus_servers};
33use internet::parse_internet_options;
34use location::parse_location;
35use login::parse_login;
36use ntp::parse_ntp;
37use ports::parse_ports;
38use services::parse_services;
39use syslog::parse_syslog;
40
41#[derive(Clone, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub struct System {
45 pub host_name: Option<String>,
46 pub domain_name: Option<String>,
47 pub time_zone: Option<String>,
49 pub domain_search: Vec<String>,
50 pub static_host_mappings: Vec<StaticHostMapping>,
51 pub backup_router: Option<BackupRouter>,
52 pub inet6_backup_router: Option<BackupRouter>,
53 pub authentication_order: Vec<String>,
55 pub radius_servers: Vec<RadiusServer>,
56 pub tacplus_servers: Vec<TacplusServer>,
57 pub location: Option<Location>,
58 pub ports: Option<Ports>,
59 pub max_configurations_on_flash: Option<String>,
60 pub max_configuration_rollbacks: Option<String>,
61 pub no_redirects: bool,
62 pub default_address_selection: bool,
64 pub internet_options: Option<InternetOptions>,
65 pub archival: Option<Archival>,
66 pub accounting: Option<Accounting>,
67 pub root_authentication: Option<RootAuthentication>,
68 pub name_servers: Vec<NameServer>,
69 pub ntp: Option<Ntp>,
70 pub syslog: Option<Syslog>,
71 pub login: Option<Login>,
72 pub services: Option<Services>,
73}
74
75pub fn system(config: &Config) -> Option<System> {
77 let system = top_level(config, "system")?;
78 Some(System {
79 host_name: arg_of(system, "host-name").map(str::to_string),
80 domain_name: arg_of(system, "domain-name").map(str::to_string),
81 time_zone: arg_of(system, "time-zone").map(str::to_string),
82 domain_search: args_of(system, "domain-search")
83 .into_iter()
84 .flat_map(list_values)
85 .collect(),
86 static_host_mappings: static_host_mappings(system),
87 backup_router: arg_of(system, "backup-router").map(parse_backup_router),
88 inet6_backup_router: arg_of(system, "inet6-backup-router").map(parse_backup_router),
89 authentication_order: args_of(system, "authentication-order")
90 .into_iter()
91 .flat_map(list_values)
92 .collect(),
93 radius_servers: radius_servers(system, "radius-server"),
94 tacplus_servers: tacplus_servers(system, "tacplus-server"),
95 location: child_block(system, "location").map(parse_location),
96 ports: child_block(system, "ports").map(parse_ports),
97 max_configurations_on_flash: arg_of(system, "max-configurations-on-flash").map(str::to_string),
98 max_configuration_rollbacks: arg_of(system, "max-configuration-rollbacks").map(str::to_string),
99 no_redirects: has_leaf(system, "no-redirects"),
100 default_address_selection: has_leaf(system, "default-address-selection"),
101 internet_options: child_block(system, "internet-options").map(parse_internet_options),
102 archival: child_block(system, "archival").map(parse_archival),
103 accounting: child_block(system, "accounting").map(parse_accounting),
104 root_authentication: root_authentication(system),
105 name_servers: name_servers(system),
106 ntp: child_block(system, "ntp").map(parse_ntp),
107 syslog: child_block(system, "syslog").map(parse_syslog),
108 login: child_block(system, "login").map(parse_login),
109 services: child_block(system, "services").map(parse_services),
110 })
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116 use asterust::config::juniper;
117
118 #[test]
120 fn extracts_host_name() {
121 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
122 assert_eq!(system.host_name.as_deref(), Some("pe1"));
123 }
124
125 #[test]
126 fn host_name_is_optional() {
127 let system = system(&juniper::parse_braces("system {\n domain-name example.net;\n}\n")).unwrap();
128 assert_eq!(system.host_name, None);
129 }
130
131 #[test]
132 fn skips_inactive_host_name() {
133 let system = system(&juniper::parse_braces("system {\n inactive: host-name pe1;\n}\n")).unwrap();
134 assert_eq!(system.host_name, None);
135 }
136
137 #[test]
138 fn extracts_domain_name() {
139 let system = system(&juniper::parse_braces(
140 "system {\n host-name pe1;\n domain-name example.net;\n}\n",
141 ))
142 .unwrap();
143 assert_eq!(system.domain_name.as_deref(), Some("example.net"));
144 }
145
146 #[test]
147 fn domain_name_is_optional() {
148 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
149 assert_eq!(system.domain_name, None);
150 }
151
152 #[test]
153 fn extracts_time_zone() {
154 let system = system(&juniper::parse_braces("system {\n time-zone America/New_York;\n}\n")).unwrap();
155 assert_eq!(system.time_zone.as_deref(), Some("America/New_York"));
156 }
157
158 #[test]
159 fn time_zone_is_optional() {
160 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
161 assert_eq!(system.time_zone, None);
162 }
163
164 #[test]
165 fn extracts_configuration_limits_and_no_redirects() {
166 let system = system(&juniper::parse_braces(
167 "system {\n max-configurations-on-flash 5;\n max-configuration-rollbacks 20;\n no-redirects;\n}\n",
168 ))
169 .unwrap();
170 assert_eq!(system.max_configurations_on_flash.as_deref(), Some("5"));
171 assert_eq!(system.max_configuration_rollbacks.as_deref(), Some("20"));
172 assert!(system.no_redirects);
173 }
174
175 #[test]
176 fn configuration_limits_are_optional() {
177 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
178 assert_eq!(system.max_configurations_on_flash, None);
179 assert_eq!(system.max_configuration_rollbacks, None);
180 assert!(!system.no_redirects);
181 }
182
183 #[test]
184 fn no_system_is_none() {
185 assert!(system(&juniper::parse_braces("interfaces {\n lo0;\n}\n")).is_none());
186 }
187}