oxirs_cluster/backup/
policy.rs1use serde::{Deserialize, Serialize};
8
9use super::{destination::DestinationConfig, gfs::GfsRotation, retention::RetentionTier};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct BackupPolicy {
14 pub name: String,
16 pub schedule: CronSchedule,
18 pub retention: RetentionTier,
20 pub gfs: Option<GfsRotation>,
22 pub encryption: EncryptionConfig,
24 pub destination: DestinationConfig,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct CronSchedule {
31 pub expression: String,
33 pub description: String,
35}
36
37impl CronSchedule {
38 pub fn daily() -> Self {
40 CronSchedule {
41 expression: "0 2 * * *".into(),
42 description: "Every day at 02:00 UTC".into(),
43 }
44 }
45
46 pub fn weekly() -> Self {
48 CronSchedule {
49 expression: "0 2 * * 0".into(),
50 description: "Every Sunday at 02:00 UTC".into(),
51 }
52 }
53
54 pub fn monthly() -> Self {
56 CronSchedule {
57 expression: "0 2 1 * *".into(),
58 description: "First day of month at 02:00 UTC".into(),
59 }
60 }
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct EncryptionConfig {
70 pub enabled: bool,
72 pub key_id: Option<String>,
74}
75
76impl EncryptionConfig {
77 pub fn none() -> Self {
79 EncryptionConfig {
80 enabled: false,
81 key_id: None,
82 }
83 }
84
85 pub fn with_key(key_id: &str) -> Self {
87 EncryptionConfig {
88 enabled: true,
89 key_id: Some(key_id.to_owned()),
90 }
91 }
92}
93
94#[cfg(test)]
99mod tests {
100 use super::*;
101 use crate::backup::{
102 destination::DestinationConfig, gfs::GfsRotation, retention::RetentionTier,
103 };
104 use std::env;
105
106 fn make_policy(name: &str) -> BackupPolicy {
107 BackupPolicy {
108 name: name.into(),
109 schedule: CronSchedule::daily(),
110 retention: RetentionTier::standard(),
111 gfs: Some(GfsRotation::default()),
112 encryption: EncryptionConfig::none(),
113 destination: DestinationConfig::Filesystem {
114 path: env::temp_dir().join("oxirs_policy_test"),
115 },
116 }
117 }
118
119 #[test]
120 fn policy_builds_and_serialises() {
121 let p = make_policy("daily-test");
122 let json = serde_json::to_string(&p).unwrap();
123 let back: BackupPolicy = serde_json::from_str(&json).unwrap();
124 assert_eq!(back.name, p.name);
125 assert_eq!(back.schedule.expression, p.schedule.expression);
126 }
127
128 #[test]
129 fn cron_schedules_have_correct_expressions() {
130 assert_eq!(CronSchedule::daily().expression, "0 2 * * *");
131 assert_eq!(CronSchedule::weekly().expression, "0 2 * * 0");
132 assert_eq!(CronSchedule::monthly().expression, "0 2 1 * *");
133 }
134
135 #[test]
136 fn encryption_config_none() {
137 let cfg = EncryptionConfig::none();
138 assert!(!cfg.enabled);
139 assert!(cfg.key_id.is_none());
140 }
141
142 #[test]
143 fn encryption_config_with_key() {
144 let cfg = EncryptionConfig::with_key("key-abc");
145 assert!(cfg.enabled);
146 assert_eq!(cfg.key_id.as_deref(), Some("key-abc"));
147 }
148
149 #[test]
150 fn encryption_config_serialises() {
151 let cfg = EncryptionConfig::with_key("key-123");
152 let json = serde_json::to_string(&cfg).unwrap();
153 let back: EncryptionConfig = serde_json::from_str(&json).unwrap();
154 assert_eq!(back.key_id.as_deref(), Some("key-123"));
155 assert!(back.enabled);
156 }
157}