use crate::{
models::{BackupInfo, BackupSchedule, CreateBackup},
CpanelClient, CpanelError,
};
impl CpanelClient {
pub async fn get_backup_info(&self, user: &str) -> Result<BackupInfo, CpanelError> {
let result: BackupInfo = self.uapi("Backup", "get_config", &[("user", user)]).await?;
Ok(result)
}
pub async fn create_backup(
&self,
user: &str,
create: &CreateBackup,
) -> Result<(), CpanelError> {
let user_s = user.to_string();
let full_s = create.full.map(|v| v.to_string());
let location_s = create.location.clone();
let email_s = create.email.clone();
let mut params: Vec<(&str, &str)> = vec![("user", &user_s)];
if let Some(ref s) = full_s {
params.push(("full", s));
}
if let Some(ref s) = location_s {
params.push(("location", s));
}
if let Some(ref s) = email_s {
params.push(("email", s));
}
let _: () = self.uapi("Backup", "create_backup", ¶ms).await?;
Ok(())
}
pub async fn delete_backup(&self, user: &str, backup_id: &str) -> Result<(), CpanelError> {
let _: () = self
.uapi(
"Backup",
"delete_backup",
&[("user", user), ("backupid", backup_id)],
)
.await?;
Ok(())
}
pub async fn list_backups(&self, user: &str) -> Result<Vec<BackupInfo>, CpanelError> {
let result: Vec<BackupInfo> = self
.uapi("Backup", "list_backups", &[("user", user)])
.await?;
Ok(result)
}
pub async fn restore_backup(&self, user: &str, backup_id: &str) -> Result<(), CpanelError> {
let _: () = self
.uapi(
"Backup",
"restore_backup",
&[("user", user), ("backupid", backup_id)],
)
.await?;
Ok(())
}
pub async fn get_backup_schedule(&self, user: &str) -> Result<BackupSchedule, CpanelError> {
let result: BackupSchedule = self.uapi("Backup", "get_config", &[("user", user)]).await?;
Ok(result)
}
pub async fn set_backup_schedule(
&self,
user: &str,
schedule: &BackupSchedule,
) -> Result<(), CpanelError> {
let frequency_s = schedule.frequency.clone();
let day_week_s = schedule.day_of_week.map(|v| v.to_string());
let day_month_s = schedule.day_of_month.map(|v| v.to_string());
let hour_s = schedule.hour.map(|v| v.to_string());
let email_s = schedule.email.clone();
let mut params: Vec<(&str, &str)> = vec![("user", user)];
if let Some(ref s) = frequency_s {
params.push(("frequency", s));
}
if let Some(ref s) = day_week_s {
params.push(("dayofweek", s));
}
if let Some(ref s) = day_month_s {
params.push(("dayofmonth", s));
}
if let Some(ref s) = hour_s {
params.push(("hour", s));
}
if let Some(ref s) = email_s {
params.push(("email", s));
}
let _: () = self.uapi("Backup", "configure_backups", ¶ms).await?;
Ok(())
}
}