Skip to main content

asterust_junos/system/
snapshot.rs

1//! `snapshot`
2//! `system { snapshot { ... } }`.
3//!
4//! # See also
5//!
6//! - [snapshot](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/snapshot-edit-system.html)
7
8use crate::{child_block, has_leaf, toggle};
9use asterust::config::ConfigLine;
10
11/// The `recovery` block of `system snapshot`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct SnapshotRecovery {
15    /// `Some(true)` for `automatic`, `Some(false)` for `no-automatic`.
16    pub automatic: Option<bool>,
17    pub configuration_only: bool,
18    pub configuration_preference: bool,
19    pub on_upgrade: bool,
20}
21
22/// The `system snapshot` options.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct Snapshot {
26    /// `Some(true)` for `configuration`, `Some(false)` for `no-configuration`: whether non-recovery
27    /// snapshots store the configuration.
28    pub configuration: Option<bool>,
29    pub recovery: Option<SnapshotRecovery>,
30}
31
32pub(super) fn parse_snapshot(snapshot: &ConfigLine) -> Snapshot {
33    Snapshot {
34        configuration: toggle(snapshot, "configuration"),
35        recovery: child_block(snapshot, "recovery").map(|recovery| SnapshotRecovery {
36            automatic: toggle(recovery, "automatic"),
37            configuration_only: has_leaf(recovery, "configuration-only"),
38            configuration_preference: has_leaf(recovery, "configuration-preference"),
39            on_upgrade: has_leaf(recovery, "on-upgrade"),
40        }),
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::system::system;
47    use asterust::config::juniper;
48
49    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
50    #[test]
51    fn extracts_the_snapshot_options() {
52        let snapshot = system(&juniper::parse_braces(
53            "system {\n    snapshot {\n        configuration;\n        recovery {\n            automatic;\n            configuration-only;\n            on-upgrade;\n        }\n    }\n}\n",
54        ))
55        .unwrap()
56        .snapshot
57        .unwrap();
58        assert_eq!(snapshot.configuration, Some(true));
59        let recovery = snapshot.recovery.unwrap();
60        assert_eq!(recovery.automatic, Some(true));
61        assert!(recovery.configuration_only);
62        assert!(!recovery.configuration_preference);
63        assert!(recovery.on_upgrade);
64    }
65
66    #[test]
67    fn reads_the_negated_snapshot_options() {
68        let snapshot = system(&juniper::parse_braces(
69            "system {\n    snapshot {\n        no-configuration;\n        recovery {\n            no-automatic;\n        }\n    }\n}\n",
70        ))
71        .unwrap()
72        .snapshot
73        .unwrap();
74        assert_eq!(snapshot.configuration, Some(false));
75        assert_eq!(snapshot.recovery.unwrap().automatic, Some(false));
76    }
77
78    #[test]
79    fn no_snapshot_is_none() {
80        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
81        assert!(system.snapshot.is_none());
82    }
83}