feat: vm actions start/stop/delete

This commit is contained in:
2024-11-29 16:43:14 +00:00
parent 632a5aaa87
commit 2370204546
11 changed files with 304 additions and 45 deletions

View File

@ -78,6 +78,9 @@ pub trait LNVpsDb: Sync + Send {
/// List all VM's
async fn list_vms(&self) -> Result<Vec<Vm>>;
/// List expired VM's
async fn list_expired_vms(&self) -> Result<Vec<Vm>>;
/// List VM's owned by a specific user
async fn list_user_vms(&self, id: u64) -> Result<Vec<Vm>>;
@ -87,6 +90,9 @@ pub trait LNVpsDb: Sync + Send {
/// Insert a new VM record
async fn insert_vm(&self, vm: &Vm) -> Result<u64>;
/// Delete a VM by id
async fn delete_vm(&self, vm_id: u64) -> Result<()>;
/// List VM ip assignments
async fn insert_vm_ip_assignment(&self, ip_assignment: &VmIpAssignment) -> Result<u64>;

View File

@ -197,6 +197,13 @@ impl LNVpsDb for LNVpsDbMysql {
.map_err(Error::new)
}
async fn list_expired_vms(&self) -> Result<Vec<Vm>> {
sqlx::query_as("select * from vm where expires > current_timestamp()")
.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 = ?")
.bind(id)
@ -233,6 +240,15 @@ impl LNVpsDb for LNVpsDbMysql {
.try_get(0)?)
}
async fn delete_vm(&self, vm_id: u64) -> Result<()> {
sqlx::query("delete from vm where id = ?")
.bind(vm_id)
.execute(&self.db)
.await
.map_err(Error::new)?;
Ok(())
}
async fn insert_vm_ip_assignment(&self, ip_assignment: &VmIpAssignment) -> Result<u64> {
Ok(sqlx::query(
"insert into vm_ip_assignment(vm_id,ip_range_id,ip) values(?, ?, ?) returning id",