asterust_junos/system/
accounting.rs1use super::radius_server::radius_servers;
4use super::tacplus_server::tacplus_servers;
5use super::{RadiusServer, TacplusServer};
6use crate::{arg_of, args_of, child_block, list_values};
7use asterust::config::ConfigLine;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
11#[non_exhaustive]
12pub struct AccountingDestination {
13 pub radius: Vec<RadiusServer>,
14 pub tacplus: Vec<TacplusServer>,
15}
16
17#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct Accounting {
21 pub events: Vec<String>,
23 pub enhanced_avs_max: Option<String>,
24 pub destination: Option<AccountingDestination>,
25}
26
27pub(super) fn parse_accounting(accounting: &ConfigLine) -> Accounting {
28 Accounting {
29 events: args_of(accounting, "events")
30 .into_iter()
31 .flat_map(list_values)
32 .collect(),
33 enhanced_avs_max: arg_of(accounting, "enhanced-avs-max").map(str::to_string),
34 destination: child_block(accounting, "destination").map(|destination| AccountingDestination {
35 radius: child_block(destination, "radius")
36 .map(|radius| radius_servers(radius, "server"))
37 .unwrap_or_default(),
38 tacplus: child_block(destination, "tacplus")
39 .map(|tacplus| tacplus_servers(tacplus, "server"))
40 .unwrap_or_default(),
41 }),
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use crate::system::system;
48 use asterust::config::juniper;
49
50 const ACCOUNTING: &str = r#"
52system {
53 accounting {
54 events [ login change-log interactive-commands ];
55 enhanced-avs-max 10;
56 destination {
57 radius {
58 server 192.0.2.10 {
59 secret "$9$secret"; ## SECRET-DATA
60 accounting-port 1813;
61 timeout 5;
62 routing-instance mgmt_junos;
63 }
64 }
65 tacplus {
66 server 192.0.2.20 {
67 secret "$9$other"; ## SECRET-DATA
68 port 49;
69 single-connection;
70 }
71 server 192.0.2.21 {
72 secret "$9$third"; ## SECRET-DATA
73 }
74 }
75 }
76 }
77}
78"#;
79
80 #[test]
81 fn extracts_accounting_events() {
82 let accounting = system(&juniper::parse_braces(ACCOUNTING)).unwrap().accounting.unwrap();
83 assert_eq!(accounting.events, ["login", "change-log", "interactive-commands"]);
84 assert_eq!(accounting.enhanced_avs_max.as_deref(), Some("10"));
85 }
86
87 #[test]
88 fn extracts_accounting_destination_servers() {
89 let accounting = system(&juniper::parse_braces(ACCOUNTING)).unwrap().accounting.unwrap();
90 let destination = accounting.destination.unwrap();
91
92 assert_eq!(destination.radius.len(), 1);
93 assert_eq!(destination.radius[0].address, "192.0.2.10");
94 assert_eq!(destination.radius[0].accounting_port.as_deref(), Some("1813"));
95 assert_eq!(destination.radius[0].timeout.as_deref(), Some("5"));
96 assert_eq!(destination.radius[0].routing_instance.as_deref(), Some("mgmt_junos"));
97
98 let addresses: Vec<_> = destination.tacplus.iter().map(|s| s.address.as_str()).collect();
99 assert_eq!(addresses, ["192.0.2.20", "192.0.2.21"]);
100 assert!(destination.tacplus[0].single_connection);
101 assert_eq!(destination.tacplus[0].port.as_deref(), Some("49"));
102 }
103
104 #[test]
105 fn accounting_without_destination() {
106 let accounting = system(&juniper::parse_braces(
107 "system {\n accounting {\n events login;\n }\n}\n",
108 ))
109 .unwrap()
110 .accounting
111 .unwrap();
112 assert_eq!(accounting.events, ["login"]);
113 assert!(accounting.destination.is_none());
114 }
115
116 #[test]
117 fn no_accounting_is_none() {
118 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
119 assert!(system.accounting.is_none());
120 }
121}