add more tables

This commit is contained in:
2024-11-10 19:24:51 +00:00
parent f9623edd38
commit 1dd72fd011
5 changed files with 141 additions and 47 deletions

View File

@ -3,8 +3,9 @@ use crate::nip98::Nip98Auth;
use crate::provisioner::Provisioner;
use anyhow::Error;
use rocket::serde::json::Json;
use rocket::{get, routes, Responder, Route, State};
use serde::Serialize;
use rocket::{get, post, routes, Data, Responder, Route, State};
use serde::{Deserialize, Serialize};
use crate::vm::VMSpec;
pub fn routes() -> Vec<Route> {
routes![v1_list_vms]
@ -37,6 +38,15 @@ impl From<Error> for ApiError {
}
}
#[derive(Debug, Serialize, Deserialize)]
struct CreateVmRequest {}
impl From<CreateVmRequest> for VMSpec {
fn from(value: CreateVmRequest) -> Self {
todo!()
}
}
#[get("/api/v1/vms")]
async fn v1_list_vms(auth: Nip98Auth, provisioner: &State<Provisioner>) -> ApiResult<Vec<db::Vm>> {
let pubkey = auth.event.pubkey.to_bytes();
@ -44,3 +54,19 @@ async fn v1_list_vms(auth: Nip98Auth, provisioner: &State<Provisioner>) -> ApiRe
let vms = provisioner.list_vms(uid).await?;
ApiData::ok(vms)
}
#[get("/api/v1/vm/templates")]
async fn v1_list_vm_templates(provisioner: &State<Provisioner>) -> ApiResult<Vec<db::VmTemplate>> {
let vms = provisioner.list_vm_templates().await?;
ApiData::ok(vms)
}
#[post("/api/v1/vm", data = "<req>", format = "json")]
async fn v1_provision_vm(auth: Nip98Auth, provisioner: &State<Provisioner>, req: Json<CreateVmRequest>) -> ApiResult<db::Vm> {
let pubkey = auth.event.pubkey.to_bytes();
let uid = provisioner.upsert_user(&pubkey).await?;
let req = req.0;
let rsp = provisioner.provision(req.into()).await?;
ApiData::ok(rsp)
}

View File

@ -76,6 +76,19 @@ pub struct IpRange {
pub enabled: bool,
}
#[derive(Serialize, FromRow, Clone, Debug)]
pub struct VmTemplate {
pub id: u64,
pub name: String,
pub enabled: bool,
pub created: DateTime<Utc>,
pub expires: Option<DateTime<Utc>>,
pub cpu: u16,
pub memory: u64,
pub disk_size: u64,
pub disk_id: u64,
}
#[derive(Serialize, FromRow, Clone, Debug)]
pub struct Vm {
/// Unique VM ID (Same in proxmox)

View File

@ -2,7 +2,7 @@ use crate::db;
use crate::db::{VmHost, VmHostDisk};
use crate::host::proxmox::ProxmoxClient;
use crate::vm::VMSpec;
use anyhow::Error;
use anyhow::{Error, Result};
use log::{info, warn};
use sqlx::{MySqlPool, Row};
@ -17,7 +17,7 @@ impl Provisioner {
}
/// Auto-discover resources
pub async fn auto_discover(&self) -> Result<(), Error> {
pub async fn auto_discover(&self) -> Result<()> {
let hosts = self.list_hosts().await?;
for host in hosts {
let api = ProxmoxClient::new(host.ip.parse()?).with_api_token(&host.api_token);
@ -58,12 +58,12 @@ impl Provisioner {
}
/// Provision a new VM
pub async fn provision(&self, spec: VMSpec) -> Result<db::Vm, Error> {
pub async fn provision(&self, spec: VMSpec) -> Result<db::Vm> {
todo!()
}
/// Insert/Fetch user id
pub async fn upsert_user(&self, pubkey: &[u8; 32]) -> Result<u64, Error> {
pub async fn upsert_user(&self, pubkey: &[u8; 32]) -> Result<u64> {
let res = sqlx::query("insert ignore into users(pubkey) values(?) returning id")
.bind(pubkey.as_slice())
.fetch_optional(&self.db)
@ -79,8 +79,16 @@ impl Provisioner {
}
}
/// List VM templates
pub async fn list_vm_templates(&self) -> Result<Vec<db::VmTemplate>> {
sqlx::query_as("select * from vm_template where enabled = 1 and (expires is null or expires < now())")
.fetch_all(&self.db)
.await
.map_err(Error::new)
}
/// List VM's owned by a specific user
pub async fn list_vms(&self, id: u64) -> Result<Vec<db::Vm>, Error> {
pub async fn list_vms(&self, id: u64) -> Result<Vec<db::Vm>> {
sqlx::query_as("select * from vm where user_id = ?")
.bind(&id)
.fetch_all(&self.db)
@ -89,7 +97,7 @@ impl Provisioner {
}
/// List VM's owned by a specific user
pub async fn list_hosts(&self) -> Result<Vec<VmHost>, Error> {
pub async fn list_hosts(&self) -> Result<Vec<VmHost>> {
sqlx::query_as("select * from vm_host")
.fetch_all(&self.db)
.await
@ -97,7 +105,7 @@ impl Provisioner {
}
/// List VM's owned by a specific user
pub async fn list_host_disks(&self, host_id: u64) -> Result<Vec<VmHostDisk>, Error> {
pub async fn list_host_disks(&self, host_id: u64) -> Result<Vec<VmHostDisk>> {
sqlx::query_as("select * from vm_host_disk where host_id = ?")
.bind(&host_id)
.fetch_all(&self.db)
@ -106,7 +114,7 @@ impl Provisioner {
}
/// Update host resources (usually from [auto_discover])
pub async fn update_host(&self, host: VmHost) -> Result<(), Error> {
pub async fn update_host(&self, host: VmHost) -> Result<()> {
sqlx::query("update vm_host set name = ?, cpu = ?, memory = ? where id = ?")
.bind(&host.name)
.bind(&host.cpu)