use std::collections::BTreeMap;
use std::path::PathBuf;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::CloudErrorDetails;
use crate::snapshot::cloud_manifest::Manifest as SnapshotManifest;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
#[serde(rename_all = "snake_case")]
pub enum CloudSnapshotKind {
Disk,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct CloudSnapshotSpec {
pub sandbox_id: String,
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "ts", ts(type = "string | null"))]
#[cfg_attr(feature = "utoipa", schema(value_type = Option<String>))]
pub dest_dir: Option<PathBuf>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub labels: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub force: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub record_integrity: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CloudCreateSnapshotRequest {
Disk {
#[serde(flatten)]
snapshot: CloudSnapshotSpec,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct CloudSnapshotDetails {
pub name: String,
pub location: CloudSnapshotLocation,
#[serde(default)]
pub sandbox_id: Option<String>,
pub digest: String,
pub size_bytes: u64,
pub manifest: SnapshotManifest,
pub labels: BTreeMap<String, String>,
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CloudSnapshot {
Disk {
#[serde(flatten)]
snapshot: CloudSnapshotDetails,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CloudSnapshotLocation {
Managed {
id: String,
},
HostVolume {
path: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct CloudSnapshotOperation {
pub id: String,
pub kind: CloudSnapshotKind,
pub status: CloudSnapshotOperationStatus,
#[serde(default)]
pub result: Option<CloudSnapshot>,
#[serde(default)]
pub error: Option<CloudErrorDetails>,
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub created_at: DateTime<Utc>,
#[cfg_attr(feature = "ts", ts(type = "string"))]
pub updated_at: DateTime<Utc>,
#[serde(default)]
#[cfg_attr(feature = "ts", ts(type = "string | null"))]
pub completed_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
#[serde(rename_all = "snake_case")]
pub enum CloudSnapshotOperationStatus {
Queued,
InProgress,
Succeeded,
Failed,
}
impl CloudCreateSnapshotRequest {
pub const fn kind(&self) -> CloudSnapshotKind {
match self {
Self::Disk { .. } => CloudSnapshotKind::Disk,
}
}
pub const fn snapshot_spec(&self) -> &CloudSnapshotSpec {
match self {
Self::Disk { snapshot } => snapshot,
}
}
pub const fn snapshot_spec_mut(&mut self) -> &mut CloudSnapshotSpec {
match self {
Self::Disk { snapshot } => snapshot,
}
}
}
impl CloudSnapshot {
pub const fn kind(&self) -> CloudSnapshotKind {
match self {
Self::Disk { .. } => CloudSnapshotKind::Disk,
}
}
pub const fn details(&self) -> &CloudSnapshotDetails {
match self {
Self::Disk { snapshot } => snapshot,
}
}
pub fn into_details(self) -> CloudSnapshotDetails {
match self {
Self::Disk { snapshot } => snapshot,
}
}
}