use crate::job::CaptureFormat; use serde::{Deserialize, Serialize}; use uuid::Uuid; #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] pub enum Engine { Postgres, Mysql, } impl CaptureFormat { pub fn engine(self) -> Option { match self { Self::PostgresCustom | Self::PostgresSql => Some(Engine::Postgres), Self::MysqlSql => Some(Engine::Mysql), Self::Filesystem => None, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(deny_unknown_fields)] pub struct DatabaseIdentity { pub instance: String, pub database: String, } impl DatabaseIdentity { pub fn valid(&self, engine: Engine) -> bool { let instance_valid = match engine { Engine::Postgres => self .instance .parse::() .is_ok_and(|id| id.to_string() == self.instance), Engine::Mysql => { Uuid::parse_str(&self.instance).is_ok_and(|id| id.to_string() == self.instance) } }; instance_valid && (1..=255).contains(&self.database.len()) && !self.database.chars().any(char::is_control) } } #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct RestoreTarget { pub id: Uuid, pub application_id: Uuid, pub outpost_id: Uuid, pub name: String, pub engine: Engine, pub identity: DatabaseIdentity, } #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct RestoreRequest { pub snapshot_id: Uuid, pub target_id: Uuid, pub confirmation: String, } impl RestoreRequest { pub fn confirmation(snapshot: Uuid, target: Uuid) -> String { format!("RESTORE {snapshot} TO {target}") } } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct RestoreJob { pub id: Uuid, pub snapshot_id: Uuid, pub target: RestoreTarget, pub actor: String, pub status: String, pub phase: String, pub safety_snapshot_id: Option, pub created_at: i64, pub updated_at: i64, /// Why the job failed, as the agent reported it (cleaned). Only administrators ever see it. #[serde(default, skip_serializing_if = "Option::is_none")] pub failure_reason: Option, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct RestoreArtifact { pub format: CaptureFormat, pub identity: DatabaseIdentity, pub sha256: String, /// The artifact is the safety copy a restore took of the very target it is now restored onto (an undo). /// Its source identity is then the target's own, so the "target is the source" refusal does not apply. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub rollback: bool, } #[derive(Deserialize, Serialize)] pub struct RestoreLease { pub job: RestoreJob, pub token: Uuid, pub artifact: RestoreArtifact, } /// Header on a restore report that carries the reason for a failure. pub const FAILURE_REASON_HEADER: &str = "x-tessera-failure-reason"; const FAILURE_REASON_MAX: usize = 300; /// Make a failure reason safe to store, log and send in a header: printable ASCII on one line, at most 300 /// bytes. Anything else becomes a space (control characters) or `?` (non-ASCII). Empty input gives `None`. pub fn clean_failure_reason(text: &str) -> Option { let mapped: String = text .chars() .map(|c| match c { c if c.is_ascii_graphic() => c, c if c.is_ascii() => ' ', _ => '?', }) .collect(); let line = mapped.split_whitespace().collect::>().join(" "); let line: String = line.chars().take(FAILURE_REASON_MAX).collect(); let line = line.trim_end().to_string(); (!line.is_empty()).then_some(line) } #[derive(Clone, Copy, Debug, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] pub enum RestoreProgress { Heartbeat, Checking, Safety, InvalidPayload, WrongTarget, SafetyFailed, Applied, ApplyFailed, }