222 lines
4.2 KiB
TypeScript

/**
* Virtual Machines API
*
* @author Pierre HUBERT
*/
import { APIClient } from "./ApiClient";
export type VMState =
| "NoState"
| "Running"
| "Blocked"
| "Paused"
| "Shutdown"
| "Shutoff"
| "Crashed"
| "PowerManagementSuspended"
| "Other";
interface VMInfoInterface {
name: string;
uuid?: string;
genid?: string;
title?: string;
description?: string;
boot_type: "UEFI" | "UEFISecureBoot";
architecture: "i686" | "x86_64";
memory: number;
vnc_access: boolean;
}
export class VMInfo implements VMInfoInterface {
name: string;
uuid?: string | undefined;
genid?: string | undefined;
title?: string | undefined;
description?: string | undefined;
boot_type: "UEFI" | "UEFISecureBoot";
architecture: "i686" | "x86_64";
memory: number;
vnc_access: boolean;
constructor(int: VMInfoInterface) {
this.name = int.name;
this.uuid = int.uuid;
this.genid = int.genid;
this.title = int.title;
this.description = int.description;
this.boot_type = int.boot_type;
this.architecture = int.architecture;
this.memory = int.memory;
this.vnc_access = int.vnc_access;
}
static NewEmpty(): VMInfo {
return new VMInfo({
name: "",
boot_type: "UEFI",
architecture: "x86_64",
memory: 1024,
vnc_access: true,
});
}
get ViewURL(): string {
return `/vm/${this.uuid}`;
}
get EditURL(): string {
return `/vm/${this.uuid}/edit`;
}
}
export class VMApi {
/**
* Create a new virtual machine
*/
static async Create(v: VMInfo): Promise<{ uuid: string }> {
return (
await APIClient.exec({
uri: `/vm/create`,
method: "POST",
jsonData: v,
})
).data;
}
/**
* Get the list of defined virtual machines
*/
static async GetList(): Promise<VMInfo[]> {
return (
await APIClient.exec({
uri: "/vm/list",
method: "GET",
})
).data.map((i: VMInfoInterface) => new VMInfo(i));
}
/**
* Get the information about a single VM
*/
static async GetSingle(uuid: string): Promise<VMInfo> {
const data = (
await APIClient.exec({
uri: `/vm/${uuid}`,
method: "GET",
})
).data;
return new VMInfo(data);
}
/**
* Update the information about a single VM
*/
static async UpdateSingle(vm: VMInfo): Promise<VMInfo> {
const data = (
await APIClient.exec({
uri: `/vm/${vm.uuid!}`,
method: "PUT",
jsonData: vm,
})
).data;
return new VMInfo(data);
}
/**
* Get the state of a VM
*/
static async GetState(vm: VMInfo): Promise<VMState> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/state`,
method: "GET",
})
).data.state;
}
/**
* Start the VM
*/
static async StartVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/start`,
method: "GET",
})
).data.state;
}
/**
* Shutdown the VM
*/
static async ShutdownVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/shutdown`,
method: "GET",
})
).data.state;
}
/**
* Restt the VM
*/
static async ResetVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/reset`,
method: "GET",
})
).data.state;
}
/**
* Kill the VM
*/
static async KillVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/kill`,
method: "GET",
})
).data.state;
}
/**
* Suspend the VM
*/
static async SuspendVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/suspend`,
method: "GET",
})
).data.state;
}
/**
* Resume the VM
*/
static async ResumeVM(vm: VMInfo): Promise<void> {
return (
await APIClient.exec({
uri: `/vm/${vm.uuid}/resume`,
method: "GET",
})
).data.state;
}
/**
* Delete a virtual machine
*/
static async Delete(vm: VMInfo, keep_files: boolean): Promise<void> {
await APIClient.exec({
uri: `/vm/${vm.uuid}`,
method: "DELETE",
jsonData: { keep_files },
});
}
}