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//! - [peers (Commit)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/peers-edit-system-config.html)
8//! - [server (Batch Commits)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-edit-system-commit.html)
9//! - [traceoptions (Batch Commits)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-commit-server.html)
10
11use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
12use crate::{active_children, arg_of, child_block, has_leaf, secret_value, unquote, Secret};
13use asterust::config::ConfigLine;
14
15/// A peer under `system commit peers`. The `authentication` password is read only with the `secrets` feature.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct CommitPeer {
19    /// Hostname or IP address.
20    pub name: String,
21    pub user: Option<String>,
22    /// `None` unless the `secrets` feature is enabled.
23    pub authentication: Option<Secret>,
24    pub routing_instance: Option<String>,
25}
26
27/// The `server` block of `system commit` (batch commits).
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct CommitServer {
31    /// Seconds between commits.
32    pub commit_interval: Option<String>,
33    pub commit_schedule_profile: Option<String>,
34    pub days_to_keep_error_logs: Option<String>,
35    pub maximum_aggregate_pool: Option<String>,
36    pub maximum_entries: Option<String>,
37    pub redirect_completion_status: bool,
38    pub retry_attempts: Option<String>,
39    pub retry_interval: Option<String>,
40    pub traceoptions: Option<Traceoptions>,
41}
42
43/// The `system commit` options. The `server` flag also covers a `server { ... }` block, read into
44/// `server_options`.
45#[derive(Clone, Debug, PartialEq, Eq)]
46#[non_exhaustive]
47pub struct Commit {
48    pub synchronize: bool,
49    pub fast_synchronize: bool,
50    pub peers_synchronize: bool,
51    pub commit_synchronize_server: bool,
52    pub config_sync_with_scp: bool,
53    pub delta_export: bool,
54    pub force_commit_log: bool,
55    pub notification: bool,
56    pub server: bool,
57    /// `Some(true)` for `persist-groups-inheritance`, `Some(false)` for
58    /// `no-persist-groups-inheritance`.
59    pub persist_groups_inheritance: Option<bool>,
60    pub peers: Vec<CommitPeer>,
61    /// The batch commit `server` statement; every option is unset for a bare `server;`.
62    pub server_options: Option<CommitServer>,
63}
64
65fn parse_server(server: &ConfigLine) -> CommitServer {
66    CommitServer {
67        commit_interval: arg_of(server, "commit-interval").map(str::to_string),
68        commit_schedule_profile: arg_of(server, "commit-schedule-profile").map(str::to_string),
69        days_to_keep_error_logs: arg_of(server, "days-to-keep-error-logs").map(str::to_string),
70        maximum_aggregate_pool: arg_of(server, "maximum-aggregate-pool").map(str::to_string),
71        maximum_entries: arg_of(server, "maximum-entries").map(str::to_string),
72        redirect_completion_status: has_leaf(server, "redirect-completion-status"),
73        retry_attempts: arg_of(server, "retry-attempts").map(str::to_string),
74        retry_interval: arg_of(server, "retry-interval").map(str::to_string),
75        traceoptions: parse_traceoptions(server),
76    }
77}
78
79fn parse_peers(commit: &ConfigLine) -> Vec<CommitPeer> {
80    child_block(commit, "peers")
81        .map(|peers| {
82            active_children(peers)
83                .map(|peer| CommitPeer {
84                    name: unquote(&peer.text).to_string(),
85                    user: arg_of(peer, "user").map(str::to_string),
86                    authentication: secret_value(arg_of(peer, "authentication")),
87                    routing_instance: arg_of(peer, "routing-instance").map(str::to_string),
88                })
89                .collect()
90        })
91        .unwrap_or_default()
92}
93
94pub(super) fn parse_commit(commit: &ConfigLine) -> Commit {
95    let persist_groups_inheritance = if has_leaf(commit, "persist-groups-inheritance") {
96        Some(true)
97    } else if has_leaf(commit, "no-persist-groups-inheritance") {
98        Some(false)
99    } else {
100        None
101    };
102    Commit {
103        synchronize: has_leaf(commit, "synchronize"),
104        fast_synchronize: has_leaf(commit, "fast-synchronize"),
105        peers_synchronize: has_leaf(commit, "peers-synchronize"),
106        commit_synchronize_server: has_leaf(commit, "commit-synchronize-server"),
107        config_sync_with_scp: has_leaf(commit, "config-sync-with-scp"),
108        delta_export: has_leaf(commit, "delta-export"),
109        force_commit_log: has_leaf(commit, "force-commit-log"),
110        notification: has_leaf(commit, "notification"),
111        server: has_leaf(commit, "server"),
112        persist_groups_inheritance,
113        peers: parse_peers(commit),
114        server_options: child_block(commit, "server").map(parse_server),
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use crate::system::system;
121    use asterust::config::juniper;
122
123    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
124    #[test]
125    fn extracts_commit_options() {
126        let commit = system(&juniper::parse_braces(
127            r#"
128system {
129    commit {
130        synchronize;
131        fast-synchronize;
132        peers-synchronize;
133        commit-synchronize-server;
134        config-sync-with-scp;
135        delta-export;
136        force-commit-log;
137        notification;
138        server;
139        persist-groups-inheritance;
140    }
141}
142"#,
143        ))
144        .unwrap()
145        .commit
146        .unwrap();
147        assert!(commit.synchronize);
148        assert!(commit.fast_synchronize);
149        assert!(commit.peers_synchronize);
150        assert!(commit.commit_synchronize_server);
151        assert!(commit.config_sync_with_scp);
152        assert!(commit.delta_export);
153        assert!(commit.force_commit_log);
154        assert!(commit.notification);
155        assert!(commit.server);
156        assert_eq!(commit.persist_groups_inheritance, Some(true));
157    }
158
159    #[test]
160    fn commit_options_default_to_off() {
161        let commit = system(&juniper::parse_braces(
162            "system {\n    commit {\n        no-persist-groups-inheritance;\n    }\n}\n",
163        ))
164        .unwrap()
165        .commit
166        .unwrap();
167        assert!(!commit.synchronize);
168        assert!(!commit.delta_export);
169        assert_eq!(commit.persist_groups_inheritance, Some(false));
170    }
171
172    #[test]
173    fn unset_persist_groups_inheritance_is_none() {
174        let commit = system(&juniper::parse_braces(
175            "system {\n    commit {\n        synchronize;\n    }\n}\n",
176        ))
177        .unwrap()
178        .commit
179        .unwrap();
180        assert_eq!(commit.persist_groups_inheritance, None);
181    }
182
183    #[test]
184    fn extracts_commit_peers_without_the_password() {
185        let commit = system(&juniper::parse_braces(
186            r#"
187system {
188    commit {
189        peers {
190            192.0.2.2 {
191                user admin;
192                authentication "$9$secret"; ## SECRET-DATA
193                routing-instance mgmt_junos;
194            }
195            node1;
196        }
197    }
198}
199"#,
200        ))
201        .unwrap()
202        .commit
203        .unwrap();
204        assert_eq!(commit.peers.len(), 2);
205        assert_eq!(commit.peers[0].name, "192.0.2.2");
206        assert_eq!(commit.peers[0].user.as_deref(), Some("admin"));
207        assert_eq!(commit.peers[0].routing_instance.as_deref(), Some("mgmt_junos"));
208        assert_eq!(
209            commit.peers[0].authentication.as_ref().map(crate::Secret::expose),
210            crate::secret::expected("$9$secret")
211        );
212        assert_eq!(commit.peers[1].name, "node1");
213        assert_eq!(commit.peers[1].user, None);
214    }
215
216    #[test]
217    fn extracts_the_batch_commit_server() {
218        let commit = system(&juniper::parse_braces(
219            r#"
220system {
221    commit {
222        server {
223            commit-interval 30;
224            days-to-keep-error-logs 7;
225            maximum-aggregate-pool 50;
226            maximum-entries 500;
227            redirect-completion-status;
228            traceoptions {
229                file commit-server.log;
230                flag batch;
231            }
232        }
233    }
234}
235"#,
236        ))
237        .unwrap()
238        .commit
239        .unwrap();
240        assert!(commit.server);
241        let server = commit.server_options.unwrap();
242        assert_eq!(server.commit_interval.as_deref(), Some("30"));
243        assert_eq!(server.days_to_keep_error_logs.as_deref(), Some("7"));
244        assert_eq!(server.maximum_aggregate_pool.as_deref(), Some("50"));
245        assert_eq!(server.maximum_entries.as_deref(), Some("500"));
246        assert!(server.redirect_completion_status);
247        assert_eq!(server.traceoptions.unwrap().flags[0].name, "batch");
248    }
249
250    #[test]
251    fn no_commit_is_none() {
252        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
253        assert!(system.commit.is_none());
254    }
255}