feat: notifications
This commit is contained in:
5
lnvps_db/migrations/20241204142919_delete-vm.sql
Normal file
5
lnvps_db/migrations/20241204142919_delete-vm.sql
Normal file
@ -0,0 +1,5 @@
|
||||
-- Add migration script here
|
||||
alter table vm
|
||||
add column deleted bit(1) not null default 0;
|
||||
alter table vm_ip_assignment
|
||||
add column deleted bit(1) not null default 0;
|
@ -102,6 +102,9 @@ pub trait LNVpsDb: Sync + Send {
|
||||
/// List VM ip assignments by IP range
|
||||
async fn list_vm_ip_assignments_in_range(&self, range_id: u64) -> Result<Vec<VmIpAssignment>>;
|
||||
|
||||
/// Delete assigned VM ips
|
||||
async fn delete_vm_ip_assignment(&self, vm_id: u64) -> Result<()>;
|
||||
|
||||
/// List payments by VM id
|
||||
async fn list_vm_payment(&self, vm_id: u64) -> Result<Vec<VmPayment>>;
|
||||
|
||||
|
@ -3,6 +3,7 @@ use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::serde_as;
|
||||
use sqlx::FromRow;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::path::PathBuf;
|
||||
use url::Url;
|
||||
|
||||
@ -151,6 +152,12 @@ impl VmOsImage {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for VmOsImage {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?} {}", self.distribution, self.version)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, FromRow, Clone, Debug)]
|
||||
pub struct IpRange {
|
||||
pub id: u64,
|
||||
@ -234,6 +241,8 @@ pub struct Vm {
|
||||
pub disk_id: u64,
|
||||
/// Network MAC address
|
||||
pub mac_address: String,
|
||||
/// Is the VM deleted
|
||||
pub deleted: bool,
|
||||
|
||||
#[sqlx(skip)]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
@ -265,6 +274,12 @@ pub struct VmIpAssignment {
|
||||
pub ip_range: Option<IpRange>,
|
||||
}
|
||||
|
||||
impl Display for VmIpAssignment {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.ip)
|
||||
}
|
||||
}
|
||||
|
||||
#[serde_as]
|
||||
#[derive(Serialize, Deserialize, FromRow, Clone, Debug, Default)]
|
||||
pub struct VmPayment {
|
||||
|
@ -191,21 +191,21 @@ impl LNVpsDb for LNVpsDbMysql {
|
||||
}
|
||||
|
||||
async fn list_vms(&self) -> Result<Vec<Vm>> {
|
||||
sqlx::query_as("select * from vm")
|
||||
sqlx::query_as("select * from vm ")
|
||||
.fetch_all(&self.db)
|
||||
.await
|
||||
.map_err(Error::new)
|
||||
}
|
||||
|
||||
async fn list_expired_vms(&self) -> Result<Vec<Vm>> {
|
||||
sqlx::query_as("select * from vm where expires > current_timestamp()")
|
||||
sqlx::query_as("select * from vm where expires > current_timestamp() and deleted = 0")
|
||||
.fetch_all(&self.db)
|
||||
.await
|
||||
.map_err(Error::new)
|
||||
}
|
||||
|
||||
async fn list_user_vms(&self, id: u64) -> Result<Vec<Vm>> {
|
||||
sqlx::query_as("select * from vm where user_id = ?")
|
||||
sqlx::query_as("select * from vm where user_id = ? and deleted = 0")
|
||||
.bind(id)
|
||||
.fetch_all(&self.db)
|
||||
.await
|
||||
@ -241,7 +241,7 @@ impl LNVpsDb for LNVpsDbMysql {
|
||||
}
|
||||
|
||||
async fn delete_vm(&self, vm_id: u64) -> Result<()> {
|
||||
sqlx::query("delete from vm where id = ?")
|
||||
sqlx::query("update vm set deleted = 1 where id = ?")
|
||||
.bind(vm_id)
|
||||
.execute(&self.db)
|
||||
.await
|
||||
@ -263,7 +263,7 @@ impl LNVpsDb for LNVpsDbMysql {
|
||||
}
|
||||
|
||||
async fn list_vm_ip_assignments(&self, vm_id: u64) -> Result<Vec<VmIpAssignment>> {
|
||||
sqlx::query_as("select * from vm_ip_assignment where vm_id = ?")
|
||||
sqlx::query_as("select * from vm_ip_assignment where vm_id = ? and deleted = 0")
|
||||
.bind(vm_id)
|
||||
.fetch_all(&self.db)
|
||||
.await
|
||||
@ -271,13 +271,21 @@ impl LNVpsDb for LNVpsDbMysql {
|
||||
}
|
||||
|
||||
async fn list_vm_ip_assignments_in_range(&self, range_id: u64) -> Result<Vec<VmIpAssignment>> {
|
||||
sqlx::query_as("select * from vm_ip_assignment where ip_range_id = ?")
|
||||
sqlx::query_as("select * from vm_ip_assignment where ip_range_id = ? and deleted = 0")
|
||||
.bind(range_id)
|
||||
.fetch_all(&self.db)
|
||||
.await
|
||||
.map_err(Error::new)
|
||||
}
|
||||
|
||||
async fn delete_vm_ip_assignment(&self, vm_id: u64) -> Result<()> {
|
||||
sqlx::query("update vm_ip_assignment set deleted = 1 where vm_id = ?")
|
||||
.bind(&vm_id)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_vm_payment(&self, vm_id: u64) -> Result<Vec<VmPayment>> {
|
||||
sqlx::query_as("select * from vm_payment where vm_id = ?")
|
||||
.bind(vm_id)
|
||||
|
Reference in New Issue
Block a user