deevirt_schema/api/vm.py

213 lines
8.3 KiB
Python
Raw Normal View History

2024-10-09 12:35:38 +02:00
from typing import List, Optional
from uuid import uuid4
from pydantic import UUID4, Field, ConfigDict
2024-10-08 19:09:39 +02:00
from pydantic import BaseModel as _BaseModel
2024-10-08 20:56:56 +02:00
2024-10-09 15:55:25 +02:00
from ..libvirt.dom import config as configDom
2024-10-08 19:09:39 +02:00
class BaseModel(_BaseModel):
class Config:
@staticmethod
def json_schema_extra(schema: dict, _):
props = {}
for k, v in schema.get('properties', {}).items():
if not v.get("hidden", False):
props[k] = v
schema["properties"] = props
2024-10-09 12:35:38 +02:00
"""Common"""
2024-10-08 20:56:56 +02:00
class Resource(BaseModel):
2024-10-08 19:09:39 +02:00
"""Experimental"""
name:Optional[str] = Field(default=None, description="Nom du pool")
2024-10-08 20:56:56 +02:00
class VCPU(BaseModel):
2024-10-08 19:09:39 +02:00
value:int = Field(description="Nombre de VCPU")
dedicated:Optional[bool] = Field(default=False, description="VCPU dédié")
hotplug:Optional[bool] = Field(default=False, description="Ajout de VCPU à chaud.")
2024-10-08 20:56:56 +02:00
class Memory(BaseModel):
unit:Optional[configDom.UnitEnum] = Field(default=configDom.UnitEnum.GiB, description="Unité de mesure.")
2024-10-08 19:09:39 +02:00
value:int = Field(description="Taille de la mémoire.")
hotplug:Optional[bool] = Field(default=False,description="Ajout à chaud de la mémoire.")
2024-10-08 20:56:56 +02:00
class OSBootMenu(BaseModel):
2024-10-08 19:09:39 +02:00
enable:Optional[bool] = Field(default=False,description="Activation du menu de démarrage.")
delay:Optional[int] = Field(default=0, description="Delai maximal pour entrer dans le menu démarrage en millisecondes")
2024-10-08 20:56:56 +02:00
class OSBootOrder(BaseModel):
dev:Optional[configDom.OSBoot.bootEnum] = Field(default=None, description="Périphérique.")
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class OSInfo(BaseModel):
2024-10-08 19:09:39 +02:00
model_config = ConfigDict(from_attributes=True)
2024-10-11 10:48:05 +02:00
name:str = Field(alias="os.name")
version:str = Field(alias="os.version-id")
machine:str = Field(alias="os.machine")
kernel:str = Field(alias="os.kernel-release")
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class OS(BaseModel):
firmware:configDom.OS.firmwareEnum = Field(default=configDom.OS.firmwareEnum.bios, description="Microprogramme")
2024-10-08 19:09:39 +02:00
enrolled_keys:Optional[bool] = Field(default=None, description="Vérification de la signature du chargeur de démarrage")
secure_boot:Optional[bool] = Field(default=None, description="Démarrage sécurisé")
2024-10-08 20:56:56 +02:00
boot_order:Optional[List[configDom.OSBoot.bootEnum]] = Field(default=None, description="Ordre de démarrage des périphériques")
boot_menu:OSBootMenu = Field(default=OSBootMenu(), description="Menu de démarrage")
class Clock(BaseModel):
offset:configDom.Clock.offsetEnum = Field(default=configDom.Clock.offsetEnum.utc, description="Sélection de l'offset ?")
class Event(BaseModel):
on_poweroff:configDom.Dom.on_poweroffEnum = Field(default=configDom.Dom.on_poweroffEnum.destroy, description="Action lors d'un poweroff")
on_reboot:configDom.Dom.on_rebootEnum = Field(default=configDom.Dom.on_rebootEnum.restart, description="Action lors d'un redémarrage")
on_crash:configDom.Dom.on_crashEnum = Field(default=configDom.Dom.on_crashEnum.restart, description="Action lors d'un crash de la VM")
2024-10-09 12:35:38 +02:00
on_failurelock:configDom.Dom.on_failurelockEnum = Field(default=configDom.Dom.on_failurelockEnum.ignore, description="A tester")
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class DevicesDisk(BaseModel):
2024-10-08 19:09:39 +02:00
model_config = ConfigDict(from_attributes=True)
2024-10-09 15:55:25 +02:00
pool_id:Optional[UUID4] = Field(default=None)
id:UUID4 = Field(default_factory=uuid4)
2024-10-08 19:09:39 +02:00
size:int = Field()
2024-10-08 20:56:56 +02:00
unit:configDom.UnitEnum = Field(default=configDom.UnitEnum.GiB)
bus:configDom.DevicesDiskTarget.busEnum = Field(default=configDom.DevicesDiskTarget.busEnum.scsi)
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class DevicesCDROMSource(BaseModel):
2024-10-09 12:35:38 +02:00
protocol:configDom.DevicesDiskSource.protocolEnum = Field(default=configDom.DevicesDiskSource.protocolEnum.https)
name:str = Field()
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class DevicesCDROM(BaseModel):
2024-10-08 19:09:39 +02:00
model_config = ConfigDict(from_attributes=True)
2024-10-08 20:56:56 +02:00
device:configDom.DevicesDisk.deviceEnum = Field(default=configDom.DevicesDisk.deviceEnum.cdrom, json_schema_extra={"hidden": True})
2024-10-09 12:35:38 +02:00
source:DevicesCDROMSource = Field(default=None)
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class DevicesInterfaceGuest(BaseModel):
2024-10-08 19:09:39 +02:00
model_config = ConfigDict(from_attributes=True)
addr:str = Field()
2024-10-08 20:56:56 +02:00
class DevicesInterface(BaseModel):
2024-10-08 19:09:39 +02:00
model_config = ConfigDict(from_attributes=True)
switch_id:UUID4
2024-10-09 15:55:25 +02:00
id:UUID4 = Field(default_factory=uuid4)
mac_addr:str = Field(default='')
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class Graphics(BaseModel):
2024-10-08 19:09:39 +02:00
port:int = Field(default=-1)
autoport:bool = Field(default=False)
websocket:int = Field(default=-1)
password:Optional[str] = Field(default=None)
2024-10-08 20:56:56 +02:00
keymap:configDom.Graphics.keymapEnum = Field(default=configDom.Graphics.keymapEnum.fr)
2024-10-08 19:09:39 +02:00
power_control:bool = Field(default=False)
2024-10-08 20:56:56 +02:00
class Audio(BaseModel):
2024-10-08 19:09:39 +02:00
pass
2024-10-08 20:56:56 +02:00
class VideoModel(BaseModel):
type:configDom.VideoModel.typeEnum = Field(default=configDom.VideoModel.typeEnum.cirrus)
2024-10-08 19:09:39 +02:00
vram:int = Field(default=16384)
2024-10-09 15:55:25 +02:00
primary:bool = Field(default=True)
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class Video(BaseModel):
2024-10-09 12:35:38 +02:00
model:VideoModel = Field()
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class Watchdog(BaseModel):
model:configDom.Watchdog.modelEnum = Field(default=configDom.Watchdog.modelEnum.itco)
action:configDom.Watchdog.actionEnum = Field(default=configDom.Watchdog.actionEnum.reset)
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class TPM(BaseModel):
2024-10-09 12:35:38 +02:00
model:configDom.TPM.modelEnum = Field(default=configDom.TPM.modelEnum.tpm_tis)
2024-10-08 20:56:56 +02:00
version:configDom.TPMBackend.versionEnum = Field(default=configDom.TPMBackend.versionEnum.v20)
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class Devices(BaseModel):
disks:Optional[List[DevicesDisk]] = Field(default=None, description="Disques virtuel")
cdrom:Optional[DevicesCDROM] = Field(default=None, description="CDROM virtuel")
2024-10-08 20:56:56 +02:00
interfaces:Optional[List[DevicesInterface]] = Field(default=None, description="Interfaces réseau")
graphics:Optional[Graphics] = Field(default=None, description="Console VNC")
audio:Audio = Field(default=None, description="Carte Audio")
video:Video = Field(default=None, description="Carte Vidéo")
watchdog:Watchdog = Field(default=None, description="Chien de garde")
tpm:TPM = Field(default=None, description="Puce de sécurité")
2024-10-08 19:09:39 +02:00
class VM(BaseModel):
model_config = ConfigDict(from_attributes=True)
# General metadata
2024-10-09 15:55:25 +02:00
id:UUID4 = Field(default_factory=uuid4)
2024-10-08 19:09:39 +02:00
datacenter_id:Optional[UUID4] = Field(default=None)
name:str = Field()
description:Optional[str] = Field(default=None)
# CPU Allocation
2024-10-08 20:56:56 +02:00
vcpu:VCPU = Field()
2024-10-08 19:09:39 +02:00
# Memory Allocation
2024-10-08 20:56:56 +02:00
memory:Memory = Field()
2024-10-08 19:09:39 +02:00
# Pool of resource (Experimental)
2024-10-08 20:56:56 +02:00
resource:Resource = Field(default=Resource(), description="Pool de resources")
2024-10-08 19:09:39 +02:00
# BIOS BootLoader
2024-10-08 20:56:56 +02:00
os:OS = Field(default=OS(), description="Options VM: Options de démarrage.")
2024-10-08 19:09:39 +02:00
# Clock configuration
2024-10-08 20:56:56 +02:00
clock:Clock = Field(default=Clock(), description="Options VM: Gestion de l'horloge")
2024-10-08 19:09:39 +02:00
# Event configuration
2024-10-08 20:56:56 +02:00
event:Event = Field(default=Event(), description="Options VM: Gestion des évènements")
2024-10-08 19:09:39 +02:00
# Devices
2024-10-08 20:56:56 +02:00
devices:Devices = Field(default=Devices(), description="Matériel virtuel: Périphériques")
2024-10-08 19:09:39 +02:00
"""VM LIST GET"""
class VMs(BaseModel):
model_config = ConfigDict(from_attributes=True)
class Datacenter(BaseModel):
model_config = ConfigDict(from_attributes=True)
id:UUID4
name:str
id:UUID4
name:str
state:int
datacenter:Optional[Datacenter] = Field(default=[])
"""VM GET"""
2024-10-08 20:56:56 +02:00
class VMGetVCPU(VCPU):
2024-10-08 19:09:39 +02:00
max:Optional[int] = Field(default=None, description="Maximum de VCPUs")
2024-10-08 20:56:56 +02:00
class VMGetOS(OS):
guest_info:Optional[OSInfo] = Field(default=None, description="Information de l'OS client")
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class VMGetDevicesDisk(DevicesDisk):
2024-10-08 19:09:39 +02:00
allocated:float = Field(default=None)
2024-10-08 20:56:56 +02:00
class VMGetDevicesInterface(DevicesInterface):
guest_info:Optional[List[DevicesInterfaceGuest]] = Field(default=None, description="Information de configuration de la carte réseau.")
2024-10-08 19:09:39 +02:00
2024-10-08 20:56:56 +02:00
class VMGetDevices(Devices):
2024-10-08 19:09:39 +02:00
disks:Optional[List[VMGetDevicesDisk]] = Field(default=None, description="Disques virtuel")
interfaces:Optional[List[VMGetDevicesInterface]] = Field(default=None, description="Interfaces réseau")
class VMGet(VM):
vcpu:VMGetVCPU = Field()
gen_id:Optional[UUID4] = Field(default=None)
os:VMGetOS = Field(default=VMGetOS(), description="Options VM: Options de démarrage.")
devices:VMGetDevices = Field(default=VMGetDevices(), description="Matériel virtuel: Périphériques")
"""VM POST"""
2024-10-08 20:56:56 +02:00
class VMPost(VM):
2024-10-08 19:09:39 +02:00
id:UUID4 = Field(default_factory=uuid4)
datacenter_id:UUID4 = Field()
"""VM DELETE"""
class VMDelete(BaseModel):
id:UUID4 = Field()
2024-10-08 20:56:56 +02:00
class HardDisk(BaseModel):
2024-10-08 19:09:39 +02:00
pass
2024-10-08 20:56:56 +02:00
class NetInterfaceCreate(BaseModel):
2024-10-08 19:09:39 +02:00
pass