use crate::domain::Recurrence;
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, PartialEq)]
pub struct BackupDestination {
pub endpoint: String,
pub region: String,
pub bucket: String,
#[serde(default)]
pub prefix: String,
#[serde(default = "path_style_default")]
pub path_style: bool,
pub access_key: String,
#[serde(default)]
pub secret_key: Option<String>,
}
fn path_style_default() -> bool {
true
}
impl std::fmt::Debug for BackupDestination {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BackupDestination")
.field("endpoint", &self.endpoint)
.field("bucket", &self.bucket)
.field("access_key", &self.access_key)
.field("secret_key", &self.secret_key.as_ref().map(|_| "***"))
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct BackupDestinationView {
pub configured: bool,
#[serde(default)]
pub endpoint: String,
#[serde(default)]
pub region: String,
#[serde(default)]
pub bucket: String,
#[serde(default)]
pub prefix: String,
#[serde(default)]
pub path_style: bool,
#[serde(default)]
pub access_key: String,
#[serde(default)]
pub secret_set: bool,
#[serde(default)]
pub recovery_configured: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BackupEntry {
pub key: String,
pub last_modified: String,
pub size_bytes: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BackupRun {
pub op: String,
pub state: String,
pub started_at: String,
#[serde(default)]
pub finished_at: Option<String>,
#[serde(default)]
pub size_bytes: Option<u64>,
#[serde(default)]
pub key: Option<String>,
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub checks: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BackupPolicy {
pub enabled: bool,
pub recurrence: Recurrence,
#[serde(default = "backup_keep_default")]
pub keep: u32,
#[serde(default = "backup_stop_default")]
pub stop: bool,
}
fn backup_keep_default() -> u32 {
7
}
fn backup_stop_default() -> bool {
true
}
impl BackupPolicy {
pub fn validate(&self) -> Result<(), String> {
match self.recurrence {
Recurrence::IntervalHours(h) if !(1..=168).contains(&h) => {
return Err("a frequência por horas vai de 1 a 168 (uma semana)".into());
}
Recurrence::Daily { hour, minute } | Recurrence::Weekly { hour, minute, .. }
if hour > 23 || minute > 59 =>
{
return Err("horário inválido (hora 0–23, minuto 0–59)".into());
}
Recurrence::Weekly { weekday, .. } if weekday > 6 => {
return Err("dia da semana inválido (0 = segunda … 6 = domingo)".into());
}
_ => {}
}
if !(1..=365).contains(&self.keep) {
return Err("quantos manter: de 1 a 365".into());
}
Ok(())
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ServiceBackups {
pub entries: Vec<BackupEntry>,
#[serde(default)]
pub last_run: Option<BackupRun>,
#[serde(default)]
pub blocked: Option<String>,
#[serde(default)]
pub list_error: Option<String>,
#[serde(default)]
pub policy: Option<BackupPolicy>,
#[serde(default)]
pub next_run_at: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RecoverService {
pub service_id: String,
pub name: String,
pub project: String,
pub kind: String,
#[serde(default)]
pub latest_backup: Option<String>,
pub step: String,
#[serde(default)]
pub error: Option<String>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ServerRecoverInfo {
pub phase: String,
#[serde(default)]
pub projects: u32,
#[serde(default)]
pub services: Vec<RecoverService>,
#[serde(default)]
pub destination_configured: bool,
#[serde(default)]
pub destination_error: Option<String>,
#[serde(default)]
pub message: Option<String>,
#[serde(default)]
pub recovery_public_key: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn politica_valida_os_limites() {
let ok = BackupPolicy {
enabled: true,
recurrence: Recurrence::Daily { hour: 3, minute: 0 },
keep: 7,
stop: true,
};
assert!(ok.validate().is_ok());
let mut p = ok.clone();
p.keep = 0;
assert!(p.validate().is_err());
p.keep = 366;
assert!(p.validate().is_err());
p.keep = 7;
p.recurrence = Recurrence::IntervalHours(0);
assert!(p.validate().is_err());
p.recurrence = Recurrence::IntervalHours(169);
assert!(p.validate().is_err());
p.recurrence = Recurrence::Daily {
hour: 24,
minute: 0,
};
assert!(p.validate().is_err());
p.recurrence = Recurrence::Weekly {
weekday: 7,
hour: 1,
minute: 0,
};
assert!(p.validate().is_err());
}
#[test]
fn politica_sem_keep_e_stop_usa_os_padroes() {
let p: BackupPolicy =
serde_json::from_str(r#"{"enabled":true,"recurrence":{"IntervalHours":6}}"#).unwrap();
assert_eq!((p.keep, p.stop), (7, true));
}
#[test]
fn rascunho_sem_chave_e_sem_path_style_usa_os_padroes() {
let d: BackupDestination = serde_json::from_str(
r#"{"endpoint":"https://s3.x","region":"us-east-1","bucket":"b","access_key":"AK"}"#,
)
.unwrap();
assert_eq!(d.secret_key, None);
assert!(d.path_style);
assert_eq!(d.prefix, "");
}
#[test]
fn debug_do_destino_nao_mostra_a_chave() {
let d = BackupDestination {
endpoint: "https://s3.x".into(),
region: "r".into(),
bucket: "b".into(),
prefix: String::new(),
path_style: true,
access_key: "AK".into(),
secret_key: Some("muito-secreta".into()),
};
assert!(!format!("{d:?}").contains("muito-secreta"));
}
#[test]
fn a_visao_nao_tem_campo_para_a_chave() {
let v = BackupDestinationView {
configured: true,
secret_set: true,
..Default::default()
};
assert!(!serde_json::to_string(&v).unwrap().contains("secret_key"));
}
}