Implements VM groups API #206

Merged
pierre merged 7 commits from groups_api into master 2024-11-28 18:06:20 +00:00
3 changed files with 60 additions and 6 deletions
Showing only changes of commit 4b97fc498f - Show all commits

View File

@ -17,7 +17,7 @@ pub async fn list(client: LibVirtReq) -> HttpResult {
Ok(HttpResponse::Ok().json(groups))
}
/// Get information about a VM
/// Get information about the VMs of a group
pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult {
let mut vms = Vec::new();
for vm in vms_ids.0 {
@ -25,3 +25,43 @@ pub async fn vm_info(vms_ids: GroupVmIdExtractor) -> HttpResult {
}
Ok(HttpResponse::Ok().json(vms))
}
/// Start the VMs of a group
pub async fn vm_start(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult {
for vm in vms_ids.0 {
if let Some(uuid) = vm.uuid {
client.start_domain(uuid).await?;
}
}
Ok(HttpResponse::Ok().finish())
}
/// Shutdown the VMs of a group
pub async fn vm_shutdown(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult {
for vm in vms_ids.0 {
if let Some(uuid) = vm.uuid {
client.shutdown_domain(uuid).await?;
}
}
Ok(HttpResponse::Ok().finish())
}
/// Kill the VMs of a group
pub async fn vm_kill(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult {
for vm in vms_ids.0 {
if let Some(uuid) = vm.uuid {
client.kill_domain(uuid).await?;
}
}
Ok(HttpResponse::Ok().finish())
}
/// Reset the VMs of a group
pub async fn vm_reset(client: LibVirtReq, vms_ids: GroupVmIdExtractor) -> HttpResult {
for vm in vms_ids.0 {
if let Some(uuid) = vm.uuid {
client.reset_domain(uuid).await?;
}
}
Ok(HttpResponse::Ok().finish())
}

View File

@ -51,8 +51,8 @@ impl FromRequest for GroupVmIdExtractor {
let vms = match client.get_full_group_vm_list(&group_id).await {
Ok(vms) => vms,
Err(e) => {
log::error!("Failed to get the VM of the group {group_id:?}: {e}");
return Err(ErrorBadRequest("Failed to get the VM of the group!"));
log::error!("Failed to get the VMs of the group {group_id:?}: {e}");
return Err(ErrorBadRequest("Failed to get the VMs of the group!"));
}
};

View File

@ -216,13 +216,27 @@ async fn main() -> std::io::Result<()> {
"/api/group/{gid}/vm/info",
web::get().to(groups_controller::vm_info),
)
// TODO start VM
// TODO stop VM
.route(
"/api/group/{gid}/vm/start",
web::get().to(groups_controller::vm_start),
)
.route(
"/api/group/{gid}/vm/shutdown",
web::get().to(groups_controller::vm_shutdown),
)
// TODO suspend VM
// TODO resume VM
// TODO kill VM
.route(
"/api/group/{gid}/vm/kill",
web::get().to(groups_controller::vm_kill),
)
.route(
"/api/group/{gid}/vm/reset",
web::get().to(groups_controller::vm_reset),
)
// TODO reset VM
// TODO screenshot VM
// TODO state of VM
// Network controller
.route(
"/api/network/create",