Skip to main content

asterust_junos/system/
mod.rs

1//! `system { ... }`.
2
3mod addressing;
4mod auth;
5mod device;
6mod internet;
7mod login;
8mod ntp;
9mod services;
10mod syslog;
11
12pub use addressing::{BackupRouter, NameServer, StaticHostMapping};
13pub use auth::{RadiusServer, RootAuthentication, TacplusServer};
14pub use device::{Location, Ports, SerialPort};
15pub use internet::{IcmpRateLimit, InternetOptions};
16pub use login::{Login, LoginClass, LoginUser};
17pub use ntp::{Ntp, NtpAuthenticationKey, NtpHost};
18pub use services::{OutboundSshClient, OutboundSshServer, Services, SshService};
19pub use syslog::{Syslog, SyslogArchive, SyslogHost, SyslogRule, SyslogTarget};
20
21use crate::{arg_of, args_of, child_block, has_leaf, list_values, top_level};
22use addressing::{name_servers, parse_backup_router, static_host_mappings};
23use asterust::config::Config;
24use auth::{radius_servers, root_authentication, tacplus_servers};
25use device::{parse_location, ports};
26use internet::parse_internet_options;
27use login::parse_login;
28use ntp::parse_ntp;
29use services::parse_services;
30use syslog::parse_syslog;
31
32/// The top-level `system` configuration.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct System {
36    pub host_name: Option<String>,
37    pub domain_name: Option<String>,
38    /// e.g. `"UTC"` or `"America/New_York"`.
39    pub time_zone: Option<String>,
40    pub domain_search: Vec<String>,
41    pub static_host_mappings: Vec<StaticHostMapping>,
42    pub backup_router: Option<BackupRouter>,
43    pub inet6_backup_router: Option<BackupRouter>,
44    /// Methods in the order tried: `password`, `radius`, `tacplus`.
45    pub authentication_order: Vec<String>,
46    pub radius_servers: Vec<RadiusServer>,
47    pub tacplus_servers: Vec<TacplusServer>,
48    pub location: Option<Location>,
49    pub ports: Option<Ports>,
50    pub max_configurations_on_flash: Option<String>,
51    pub max_configuration_rollbacks: Option<String>,
52    pub no_redirects: bool,
53    /// `default-address-selection`: use the loopback as the source of locally generated packets.
54    pub default_address_selection: bool,
55    pub internet_options: Option<InternetOptions>,
56    pub root_authentication: Option<RootAuthentication>,
57    pub name_servers: Vec<NameServer>,
58    pub ntp: Option<Ntp>,
59    pub syslog: Option<Syslog>,
60    pub login: Option<Login>,
61    pub services: Option<Services>,
62}
63
64/// The top-level `system` block, or `None` if there is none.
65pub fn system(config: &Config) -> Option<System> {
66    let system = top_level(config, "system")?;
67    Some(System {
68        host_name: arg_of(system, "host-name").map(str::to_string),
69        domain_name: arg_of(system, "domain-name").map(str::to_string),
70        time_zone: arg_of(system, "time-zone").map(str::to_string),
71        domain_search: args_of(system, "domain-search")
72            .into_iter()
73            .flat_map(list_values)
74            .collect(),
75        static_host_mappings: static_host_mappings(system),
76        backup_router: arg_of(system, "backup-router").map(parse_backup_router),
77        inet6_backup_router: arg_of(system, "inet6-backup-router").map(parse_backup_router),
78        authentication_order: args_of(system, "authentication-order")
79            .into_iter()
80            .flat_map(list_values)
81            .collect(),
82        radius_servers: radius_servers(system),
83        tacplus_servers: tacplus_servers(system),
84        location: child_block(system, "location").map(parse_location),
85        ports: ports(system),
86        max_configurations_on_flash: arg_of(system, "max-configurations-on-flash").map(str::to_string),
87        max_configuration_rollbacks: arg_of(system, "max-configuration-rollbacks").map(str::to_string),
88        no_redirects: has_leaf(system, "no-redirects"),
89        default_address_selection: has_leaf(system, "default-address-selection"),
90        internet_options: child_block(system, "internet-options").map(parse_internet_options),
91        root_authentication: root_authentication(system),
92        name_servers: name_servers(system),
93        ntp: child_block(system, "ntp").map(parse_ntp),
94        syslog: child_block(system, "syslog").map(parse_syslog),
95        login: child_block(system, "login").map(parse_login),
96        services: child_block(system, "services").map(parse_services),
97    })
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use asterust::config::juniper;
104
105    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
106    #[test]
107    fn extracts_host_name() {
108        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
109        assert_eq!(system.host_name.as_deref(), Some("pe1"));
110    }
111
112    #[test]
113    fn host_name_is_optional() {
114        let system = system(&juniper::parse_braces("system {\n    domain-name example.net;\n}\n")).unwrap();
115        assert_eq!(system.host_name, None);
116    }
117
118    #[test]
119    fn skips_inactive_host_name() {
120        let system = system(&juniper::parse_braces("system {\n    inactive: host-name pe1;\n}\n")).unwrap();
121        assert_eq!(system.host_name, None);
122    }
123
124    #[test]
125    fn extracts_domain_name() {
126        let system = system(&juniper::parse_braces(
127            "system {\n    host-name pe1;\n    domain-name example.net;\n}\n",
128        ))
129        .unwrap();
130        assert_eq!(system.domain_name.as_deref(), Some("example.net"));
131    }
132
133    #[test]
134    fn domain_name_is_optional() {
135        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
136        assert_eq!(system.domain_name, None);
137    }
138
139    #[test]
140    fn extracts_time_zone() {
141        let system = system(&juniper::parse_braces("system {\n    time-zone America/New_York;\n}\n")).unwrap();
142        assert_eq!(system.time_zone.as_deref(), Some("America/New_York"));
143    }
144
145    #[test]
146    fn time_zone_is_optional() {
147        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
148        assert_eq!(system.time_zone, None);
149    }
150
151    #[test]
152    fn no_system_is_none() {
153        assert!(system(&juniper::parse_braces("interfaces {\n    lo0;\n}\n")).is_none());
154    }
155}