diff --git a/remote_backend/src/controllers/group_controller.rs b/remote_backend/src/controllers/group_controller.rs new file mode 100644 index 0000000..7142c05 --- /dev/null +++ b/remote_backend/src/controllers/group_controller.rs @@ -0,0 +1,30 @@ +use crate::controllers::HttpResult; +use crate::virtweb_client; +use crate::virtweb_client::{GroupID, VMUuid}; +use actix_web::{web, HttpResponse}; + +#[derive(serde::Deserialize)] +pub struct GroupIDInPath { + gid: GroupID, +} + +#[derive(serde::Deserialize)] +pub struct VMIDInQuery { + vm_id: Option, +} + +/// Get the state of one or all VM +pub async fn vm_state( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_state(&path.gid, query.vm_id).await?)) +} + +/// Start one or all VM +pub async fn vm_start( + path: web::Path, + query: web::Query, +) -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::group_vm_start(&path.gid, query.vm_id).await?)) +} diff --git a/remote_backend/src/controllers/mod.rs b/remote_backend/src/controllers/mod.rs index c0d541f..02ad6b9 100644 --- a/remote_backend/src/controllers/mod.rs +++ b/remote_backend/src/controllers/mod.rs @@ -6,6 +6,7 @@ use std::fmt::{Display, Formatter}; use std::io::ErrorKind; pub mod auth_controller; +pub mod group_controller; pub mod server_controller; pub mod static_controller; pub mod sys_info_controller; diff --git a/remote_backend/src/main.rs b/remote_backend/src/main.rs index 90c0a4c..533c9c3 100644 --- a/remote_backend/src/main.rs +++ b/remote_backend/src/main.rs @@ -12,7 +12,8 @@ use light_openid::basic_state_manager::BasicStateManager; use remote_backend::app_config::AppConfig; use remote_backend::constants; use remote_backend::controllers::{ - auth_controller, server_controller, static_controller, sys_info_controller, vm_controller, + auth_controller, group_controller, server_controller, static_controller, sys_info_controller, + vm_controller, }; use remote_backend::middlewares::auth_middleware::AuthChecker; use std::time::Duration; @@ -86,6 +87,30 @@ async fn main() -> std::io::Result<()> { "/api/server/rights", web::get().to(server_controller::rights), ) + // Groups routes + .route( + "/api/group/{gid}/vm/state", + web::get().to(group_controller::vm_state), + ) + .route( + "/api/group/{gid}/vm/start", + web::get().to(group_controller::vm_start), + ) + /*.route( + "/api/group/{gid}/vm/shutdown", + web::get().to(group_controller::vm_shutdown), + ) + .route("/api/group/{gid}/vm/kill", web::get().to(group_controller::vm_kill)) + .route("/api/group/{gid}/vm/reset", web::get().to(group_controller::vm_reset)) + .route( + "/api/group/{gid}/vm/suspend", + web::get().to(group_controller::vm_suspend), + ) + .route("/api/group/{gid}/vm/resume", web::get().to(group_controller::vm_resume)) + .route( + "/api/group/{gid}/vm/screenshot", + web::get().to(group_controller::vm_screenshot), + )*/ // VM routes .route("/api/vm/{uid}/state", web::get().to(vm_controller::state)) .route("/api/vm/{uid}/start", web::get().to(vm_controller::start)) diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index 9344bfb..7e75cad 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -1,6 +1,7 @@ use crate::app_config::AppConfig; use crate::utils::time; use lazy_regex::regex; +use std::collections::HashMap; use std::fmt::Display; use std::str::FromStr; use thiserror::Error; @@ -102,9 +103,15 @@ impl GroupID { } } -#[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Eq, PartialEq, Debug, Copy, Clone, serde::Serialize, serde::Deserialize, Hash)] pub struct VMUuid(Uuid); +#[derive(Default, serde::Deserialize, serde::Serialize)] +pub struct TreatmentResult { + ok: usize, + failed: usize, +} + impl VMUuid { pub fn route_info(&self) -> String { format!("/api/vm/{}", self.0) @@ -268,12 +275,13 @@ async fn request(uri: D) -> anyhow::Result { let url = format!("{}{}", AppConfig::get().virtweb_base_url, uri); log::debug!("Will query {uri}..."); + let uri = uri.to_string(); let jwt = TokenClaims { sub: AppConfig::get().virtweb_token_id.to_string(), iat: time() - 60 * 2, exp: time() + 60 * 3, verb: "GET".to_string(), - path: uri.to_string(), + path: uri.split_once('?').map(|s| s.0).unwrap_or(&uri).to_string(), nonce: Uuid::new_v4().to_string(), }; let jwt = AppConfig::get().token_private_key().sign_jwt(&jwt)?; @@ -365,6 +373,22 @@ pub async fn group_vm_info(id: &GroupID) -> anyhow::Result> { json_request(id.route_vm_info()).await } +/// Get the state of one or all VMs of a group +pub async fn group_vm_state( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result> { + json_request(id.route_vm_state(vm_id)).await +} + +/// Start one or all VMs of a group +pub async fn group_vm_start( + id: &GroupID, + vm_id: Option, +) -> anyhow::Result { + json_request(id.route_vm_start(vm_id)).await +} + /// Get current server information pub async fn get_server_info() -> anyhow::Result { json_request("/api/server/info").await