Skip to main content

asterust_junos/system/
commit.rs

1//! `commit`
2//! `system { commit { ... } }`.
3//!
4//! # See also
5//!
6//! - [commit (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/commit-edit-system-config.html)
7
8use crate::has_leaf;
9use asterust::config::ConfigLine;
10
11/// The `system commit` options. `peers`, which carries user credentials, is not extracted.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Commit {
15    pub synchronize: bool,
16    pub fast_synchronize: bool,
17    pub peers_synchronize: bool,
18    pub commit_synchronize_server: bool,
19    pub config_sync_with_scp: bool,
20    pub delta_export: bool,
21    pub force_commit_log: bool,
22    pub notification: bool,
23    pub server: bool,
24    /// `Some(true)` for `persist-groups-inheritance`, `Some(false)` for
25    /// `no-persist-groups-inheritance`.
26    pub persist_groups_inheritance: Option<bool>,
27}
28
29pub(super) fn parse_commit(commit: &ConfigLine) -> Commit {
30    let persist_groups_inheritance = if has_leaf(commit, "persist-groups-inheritance") {
31        Some(true)
32    } else if has_leaf(commit, "no-persist-groups-inheritance") {
33        Some(false)
34    } else {
35        None
36    };
37    Commit {
38        synchronize: has_leaf(commit, "synchronize"),
39        fast_synchronize: has_leaf(commit, "fast-synchronize"),
40        peers_synchronize: has_leaf(commit, "peers-synchronize"),
41        commit_synchronize_server: has_leaf(commit, "commit-synchronize-server"),
42        config_sync_with_scp: has_leaf(commit, "config-sync-with-scp"),
43        delta_export: has_leaf(commit, "delta-export"),
44        force_commit_log: has_leaf(commit, "force-commit-log"),
45        notification: has_leaf(commit, "notification"),
46        server: has_leaf(commit, "server"),
47        persist_groups_inheritance,
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use crate::system::system;
54    use asterust::config::juniper;
55
56    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
57    #[test]
58    fn extracts_commit_options() {
59        let commit = system(&juniper::parse_braces(
60            r#"
61system {
62    commit {
63        synchronize;
64        fast-synchronize;
65        peers-synchronize;
66        commit-synchronize-server;
67        config-sync-with-scp;
68        delta-export;
69        force-commit-log;
70        notification;
71        server;
72        persist-groups-inheritance;
73    }
74}
75"#,
76        ))
77        .unwrap()
78        .commit
79        .unwrap();
80        assert!(commit.synchronize);
81        assert!(commit.fast_synchronize);
82        assert!(commit.peers_synchronize);
83        assert!(commit.commit_synchronize_server);
84        assert!(commit.config_sync_with_scp);
85        assert!(commit.delta_export);
86        assert!(commit.force_commit_log);
87        assert!(commit.notification);
88        assert!(commit.server);
89        assert_eq!(commit.persist_groups_inheritance, Some(true));
90    }
91
92    #[test]
93    fn commit_options_default_to_off() {
94        let commit = system(&juniper::parse_braces(
95            "system {\n    commit {\n        no-persist-groups-inheritance;\n    }\n}\n",
96        ))
97        .unwrap()
98        .commit
99        .unwrap();
100        assert!(!commit.synchronize);
101        assert!(!commit.delta_export);
102        assert_eq!(commit.persist_groups_inheritance, Some(false));
103    }
104
105    #[test]
106    fn unset_persist_groups_inheritance_is_none() {
107        let commit = system(&juniper::parse_braces(
108            "system {\n    commit {\n        synchronize;\n    }\n}\n",
109        ))
110        .unwrap()
111        .commit
112        .unwrap();
113        assert_eq!(commit.persist_groups_inheritance, None);
114    }
115
116    #[test]
117    fn no_commit_is_none() {
118        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
119        assert!(system.commit.is_none());
120    }
121}