From 766ec1780ab3186045752343a4dd32b94be254e3 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 6 May 2024 18:20:55 +0200 Subject: [PATCH] Get VM state --- remote_frontend/src/api/VMApi.ts | 20 +++++++++++++ .../src/widgets/VirtualMachinesWidget.tsx | 29 +++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/remote_frontend/src/api/VMApi.ts b/remote_frontend/src/api/VMApi.ts index ce73b6b..912922f 100644 --- a/remote_frontend/src/api/VMApi.ts +++ b/remote_frontend/src/api/VMApi.ts @@ -17,6 +17,17 @@ export interface VMInfo { can_screenshot: boolean; } +export type VMState = + | "NoState" + | "Running" + | "Blocked" + | "Paused" + | "Shutdown" + | "Shutoff" + | "Crashed" + | "PowerManagementSuspended" + | "Other"; + export class VMApi { /** * Get the list of VM that can be managed by this console @@ -24,4 +35,13 @@ export class VMApi { static async GetList(): Promise { return (await APIClient.exec({ method: "GET", uri: "/vm/list" })).data; } + + /** + * Get the state of a VM + */ + static async State(vm: VMInfo): Promise { + return ( + await APIClient.exec({ method: "GET", uri: `/vm/${vm.uiid}/state` }) + ).data.state; + } } diff --git a/remote_frontend/src/widgets/VirtualMachinesWidget.tsx b/remote_frontend/src/widgets/VirtualMachinesWidget.tsx index dd7a173..d0a8aec 100644 --- a/remote_frontend/src/widgets/VirtualMachinesWidget.tsx +++ b/remote_frontend/src/widgets/VirtualMachinesWidget.tsx @@ -13,9 +13,10 @@ import { PowerRegular, } from "@fluentui/react-icons"; import React from "react"; -import { VMApi, VMInfo } from "../api/VMApi"; +import { VMApi, VMInfo, VMState } from "../api/VMApi"; import { AsyncWidget } from "./AsyncWidget"; import { SectionContainer } from "./SectionContainer"; +import { useToast } from "../hooks/providers/ToastProvider"; export function VirtualMachinesWidget(): React.ReactElement { const [list, setList] = React.useState(); @@ -54,6 +55,26 @@ function VirtualMachinesWidgetInner(p: { list: VMInfo[] }): React.ReactElement { } function VMWidget(p: { vm: VMInfo }): React.ReactElement { + const toast = useToast(); + + const [state, setState] = React.useState(); + + const load = async () => { + setState(await VMApi.State(p.vm)); + }; + + React.useEffect(() => { + const interval = setInterval(async () => { + try { + if (p.vm.can_get_state) await load(); + } catch (e) { + console.error(e); + toast("Error", `Failed to refresh ${p.vm.name} status!`, "error"); + } + }, 1000); + return () => clearInterval(interval); + }); + return ( {p.vm.name} } - description={STATE TODO} + description={ + + {p.vm.can_get_state ? state ?? "..." : "Unavailable"} + + } />

{p.vm.description}