use crate::{arg_of, has_leaf, toggle};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TacplusOptions {
pub exclude_cmd_attribute: bool,
pub no_cmd_attribute_value: bool,
pub authorization_time_interval: Option<String>,
pub enhanced_accounting: bool,
pub strict_authorization: Option<bool>,
pub service_name: Option<String>,
pub timestamp_and_timezone: bool,
}
pub(super) fn parse_tacplus_options(options: &ConfigLine) -> TacplusOptions {
TacplusOptions {
exclude_cmd_attribute: has_leaf(options, "exclude-cmd-attribute"),
no_cmd_attribute_value: has_leaf(options, "no-cmd-attribute-value"),
authorization_time_interval: arg_of(options, "authorization-time-interval").map(str::to_string),
enhanced_accounting: has_leaf(options, "enhanced-accounting"),
strict_authorization: toggle(options, "strict-authorization"),
service_name: arg_of(options, "service-name").map(str::to_string),
timestamp_and_timezone: has_leaf(options, "timestamp-and-timezone"),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_tacplus_options() {
let options = system(&juniper::parse_braces(
r#"
system {
tacplus-options {
exclude-cmd-attribute;
authorization-time-interval 30;
enhanced-accounting;
strict-authorization;
service-name junos-exec;
timestamp-and-timezone;
}
}
"#,
))
.unwrap()
.tacplus_options
.unwrap();
assert!(options.exclude_cmd_attribute);
assert!(!options.no_cmd_attribute_value);
assert_eq!(options.authorization_time_interval.as_deref(), Some("30"));
assert!(options.enhanced_accounting);
assert_eq!(options.strict_authorization, Some(true));
assert_eq!(options.service_name.as_deref(), Some("junos-exec"));
assert!(options.timestamp_and_timezone);
}
#[test]
fn reads_no_strict_authorization() {
let options = system(&juniper::parse_braces(
"system {\n tacplus-options {\n no-cmd-attribute-value;\n no-strict-authorization;\n }\n}\n",
))
.unwrap()
.tacplus_options
.unwrap();
assert!(options.no_cmd_attribute_value);
assert_eq!(options.strict_authorization, Some(false));
assert_eq!(options.service_name, None);
}
#[test]
fn no_tacplus_options_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.tacplus_options.is_none());
}
}