asterust-junos 0.5.11

Typed Juniper JunOS config views (interfaces, protocols, routing instances and options, policy options, firewall filters, class of service, system, services) extracted from an asterust config tree.
Documentation
//! `commit`
//! `system { commit { ... } }`.
//!
//! # See also
//!
//! - [commit (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/commit-edit-system-config.html)
//! - [peers (Commit)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peers-edit-system-config.html)
//! - [server (Batch Commits)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-edit-system-commit.html)
//! - [traceoptions (Batch Commits)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-commit-server.html)

use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
use crate::{active_children, arg_of, child_block, has_leaf, secret_value, unquote, Secret};
use asterust::config::ConfigLine;

/// A peer under `system commit peers`. The `authentication` password is read only with the `secrets` feature.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CommitPeer {
    /// Hostname or IP address.
    pub name: String,
    pub user: Option<String>,
    /// `None` unless the `secrets` feature is enabled.
    pub authentication: Option<Secret>,
    pub routing_instance: Option<String>,
}

/// The `server` block of `system commit` (batch commits).
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct CommitServer {
    /// Seconds between commits.
    pub commit_interval: Option<String>,
    pub commit_schedule_profile: Option<String>,
    pub days_to_keep_error_logs: Option<String>,
    pub maximum_aggregate_pool: Option<String>,
    pub maximum_entries: Option<String>,
    pub redirect_completion_status: bool,
    pub retry_attempts: Option<String>,
    pub retry_interval: Option<String>,
    pub traceoptions: Option<Traceoptions>,
}

/// The `system commit` options. The `server` flag also covers a `server { ... }` block, read into
/// `server_options`.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Commit {
    pub synchronize: bool,
    pub fast_synchronize: bool,
    pub peers_synchronize: bool,
    pub commit_synchronize_server: bool,
    pub config_sync_with_scp: bool,
    pub delta_export: bool,
    pub force_commit_log: bool,
    pub notification: bool,
    pub server: bool,
    /// `Some(true)` for `persist-groups-inheritance`, `Some(false)` for
    /// `no-persist-groups-inheritance`.
    pub persist_groups_inheritance: Option<bool>,
    pub peers: Vec<CommitPeer>,
    /// The batch commit `server` statement; every option is unset for a bare `server;`.
    pub server_options: Option<CommitServer>,
}

fn parse_server(server: &ConfigLine) -> CommitServer {
    CommitServer {
        commit_interval: arg_of(server, "commit-interval").map(str::to_string),
        commit_schedule_profile: arg_of(server, "commit-schedule-profile").map(str::to_string),
        days_to_keep_error_logs: arg_of(server, "days-to-keep-error-logs").map(str::to_string),
        maximum_aggregate_pool: arg_of(server, "maximum-aggregate-pool").map(str::to_string),
        maximum_entries: arg_of(server, "maximum-entries").map(str::to_string),
        redirect_completion_status: has_leaf(server, "redirect-completion-status"),
        retry_attempts: arg_of(server, "retry-attempts").map(str::to_string),
        retry_interval: arg_of(server, "retry-interval").map(str::to_string),
        traceoptions: parse_traceoptions(server),
    }
}

fn parse_peers(commit: &ConfigLine) -> Vec<CommitPeer> {
    child_block(commit, "peers")
        .map(|peers| {
            active_children(peers)
                .map(|peer| CommitPeer {
                    name: unquote(&peer.text).to_string(),
                    user: arg_of(peer, "user").map(str::to_string),
                    authentication: secret_value(arg_of(peer, "authentication")),
                    routing_instance: arg_of(peer, "routing-instance").map(str::to_string),
                })
                .collect()
        })
        .unwrap_or_default()
}

pub(super) fn parse_commit(commit: &ConfigLine) -> Commit {
    let persist_groups_inheritance = if has_leaf(commit, "persist-groups-inheritance") {
        Some(true)
    } else if has_leaf(commit, "no-persist-groups-inheritance") {
        Some(false)
    } else {
        None
    };
    Commit {
        synchronize: has_leaf(commit, "synchronize"),
        fast_synchronize: has_leaf(commit, "fast-synchronize"),
        peers_synchronize: has_leaf(commit, "peers-synchronize"),
        commit_synchronize_server: has_leaf(commit, "commit-synchronize-server"),
        config_sync_with_scp: has_leaf(commit, "config-sync-with-scp"),
        delta_export: has_leaf(commit, "delta-export"),
        force_commit_log: has_leaf(commit, "force-commit-log"),
        notification: has_leaf(commit, "notification"),
        server: has_leaf(commit, "server"),
        persist_groups_inheritance,
        peers: parse_peers(commit),
        server_options: child_block(commit, "server").map(parse_server),
    }
}

#[cfg(test)]
mod tests {
    use crate::system::system;
    use asterust::config::juniper;

    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
    #[test]
    fn extracts_commit_options() {
        let commit = system(&juniper::parse_braces(
            r#"
system {
    commit {
        synchronize;
        fast-synchronize;
        peers-synchronize;
        commit-synchronize-server;
        config-sync-with-scp;
        delta-export;
        force-commit-log;
        notification;
        server;
        persist-groups-inheritance;
    }
}
"#,
        ))
        .unwrap()
        .commit
        .unwrap();
        assert!(commit.synchronize);
        assert!(commit.fast_synchronize);
        assert!(commit.peers_synchronize);
        assert!(commit.commit_synchronize_server);
        assert!(commit.config_sync_with_scp);
        assert!(commit.delta_export);
        assert!(commit.force_commit_log);
        assert!(commit.notification);
        assert!(commit.server);
        assert_eq!(commit.persist_groups_inheritance, Some(true));
    }

    #[test]
    fn commit_options_default_to_off() {
        let commit = system(&juniper::parse_braces(
            "system {\n    commit {\n        no-persist-groups-inheritance;\n    }\n}\n",
        ))
        .unwrap()
        .commit
        .unwrap();
        assert!(!commit.synchronize);
        assert!(!commit.delta_export);
        assert_eq!(commit.persist_groups_inheritance, Some(false));
    }

    #[test]
    fn unset_persist_groups_inheritance_is_none() {
        let commit = system(&juniper::parse_braces(
            "system {\n    commit {\n        synchronize;\n    }\n}\n",
        ))
        .unwrap()
        .commit
        .unwrap();
        assert_eq!(commit.persist_groups_inheritance, None);
    }

    #[test]
    fn extracts_commit_peers_without_the_password() {
        let commit = system(&juniper::parse_braces(
            r#"
system {
    commit {
        peers {
            192.0.2.2 {
                user admin;
                authentication "$9$secret"; ## SECRET-DATA
                routing-instance mgmt_junos;
            }
            node1;
        }
    }
}
"#,
        ))
        .unwrap()
        .commit
        .unwrap();
        assert_eq!(commit.peers.len(), 2);
        assert_eq!(commit.peers[0].name, "192.0.2.2");
        assert_eq!(commit.peers[0].user.as_deref(), Some("admin"));
        assert_eq!(commit.peers[0].routing_instance.as_deref(), Some("mgmt_junos"));
        assert_eq!(
            commit.peers[0].authentication.as_ref().map(crate::Secret::expose),
            crate::secret::expected("$9$secret")
        );
        assert_eq!(commit.peers[1].name, "node1");
        assert_eq!(commit.peers[1].user, None);
    }

    #[test]
    fn extracts_the_batch_commit_server() {
        let commit = system(&juniper::parse_braces(
            r#"
system {
    commit {
        server {
            commit-interval 30;
            days-to-keep-error-logs 7;
            maximum-aggregate-pool 50;
            maximum-entries 500;
            redirect-completion-status;
            traceoptions {
                file commit-server.log;
                flag batch;
            }
        }
    }
}
"#,
        ))
        .unwrap()
        .commit
        .unwrap();
        assert!(commit.server);
        let server = commit.server_options.unwrap();
        assert_eq!(server.commit_interval.as_deref(), Some("30"));
        assert_eq!(server.days_to_keep_error_logs.as_deref(), Some("7"));
        assert_eq!(server.maximum_aggregate_pool.as_deref(), Some("50"));
        assert_eq!(server.maximum_entries.as_deref(), Some("500"));
        assert!(server.redirect_completion_status);
        assert_eq!(server.traceoptions.unwrap().flags[0].name, "batch");
    }

    #[test]
    fn no_commit_is_none() {
        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
        assert!(system.commit.is_none());
    }
}