correction

This commit is contained in:
Mickael BOURNEUF 2024-10-08 20:56:56 +02:00
parent fcd33f4372
commit 038ab7de5c
3 changed files with 89 additions and 95 deletions

151
api/vm.py
View File

@ -5,13 +5,15 @@ from pydantic_xml import element, attr, wrapped
from pydantic import UUID4, Field, field_serializer, ConfigDict, ValidationInfo, field_validator from pydantic import UUID4, Field, field_serializer, ConfigDict, ValidationInfo, field_validator
from pydantic import BaseModel as _BaseModel from pydantic import BaseModel as _BaseModel
from pydantic_core import core_schema as cs from pydantic_core import core_schema as cs
import libvirt
from pydantic_core import core_schema from pydantic_core import core_schema
from pydantic.json_schema import GetJsonSchemaHandler, JsonSchemaValue from pydantic.json_schema import GetJsonSchemaHandler, JsonSchemaValue
from deevirt_common.lib.virt import vm as domain #from deevirt_common.lib.virt import vm as domain
from deevirt_common.schemas import datacenter_schema, storage_schema, network_schema, libvirt #from deevirt_common.schemas import datacenter_schema, storage_schema, network_schema, configDom
from deevirt_schema.libvirt.dom import config as configDom
class BaseModel(_BaseModel): class BaseModel(_BaseModel):
class Config: class Config:
@ -25,28 +27,28 @@ class BaseModel(_BaseModel):
""" Common """ """ Common """
class VMResource(BaseModel): class Resource(BaseModel):
"""Experimental""" """Experimental"""
name:Optional[str] = Field(default=None, description="Nom du pool") name:Optional[str] = Field(default=None, description="Nom du pool")
class VMVCPU(BaseModel): class VCPU(BaseModel):
value:int = Field(description="Nombre de VCPU") value:int = Field(description="Nombre de VCPU")
dedicated:Optional[bool] = Field(default=False, description="VCPU dédié") dedicated:Optional[bool] = Field(default=False, description="VCPU dédié")
hotplug:Optional[bool] = Field(default=False, description="Ajout de VCPU à chaud.") hotplug:Optional[bool] = Field(default=False, description="Ajout de VCPU à chaud.")
class VMMemory(BaseModel): class Memory(BaseModel):
unit:Optional[libvirt.UnitEnum] = Field(default=libvirt.UnitEnum.GiB, description="Unité de mesure.") unit:Optional[configDom.UnitEnum] = Field(default=configDom.UnitEnum.GiB, description="Unité de mesure.")
value:int = Field(description="Taille de la mémoire.") value:int = Field(description="Taille de la mémoire.")
hotplug:Optional[bool] = Field(default=False,description="Ajout à chaud de la mémoire.") hotplug:Optional[bool] = Field(default=False,description="Ajout à chaud de la mémoire.")
class VMOSBootMenu(BaseModel): class OSBootMenu(BaseModel):
enable:Optional[bool] = Field(default=False,description="Activation du menu de démarrage.") 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") delay:Optional[int] = Field(default=0, description="Delai maximal pour entrer dans le menu démarrage en millisecondes")
class VMOSBootOrder(BaseModel): class OSBootOrder(BaseModel):
dev:Optional[libvirt.OSBoot.bootEnum] = Field(default=None, description="Périphérique.") dev:Optional[configDom.OSBoot.bootEnum] = Field(default=None, description="Périphérique.")
class VMOSInfo(BaseModel): class OSInfo(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
name:str = Field(alias="os.name", serialization_alias="name") name:str = Field(alias="os.name", serialization_alias="name")
@ -54,24 +56,17 @@ class VMOSInfo(BaseModel):
machine:str = Field(alias="os.machine", serialization_alias="architecture") machine:str = Field(alias="os.machine", serialization_alias="architecture")
kernel:str = Field(alias="os.kernel-release", serialization_alias="kernel") kernel:str = Field(alias="os.kernel-release", serialization_alias="kernel")
class VMOS(BaseModel): class OS(BaseModel):
firmware:libvirt.OS.firmwareEnum = Field(default=libvirt.OS.firmwareEnum.bios, description="Microprogramme") firmware:configDom.OS.firmwareEnum = Field(default=configDom.OS.firmwareEnum.bios, description="Microprogramme")
enrolled_keys:Optional[bool] = Field(default=None, description="Vérification de la signature du chargeur de démarrage") 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é") secure_boot:Optional[bool] = Field(default=None, description="Démarrage sécurisé")
boot_order:Optional[List[libvirt.OSBoot.bootEnum]] = Field(default=None, description="Ordre de démarrage des périphériques") boot_order:Optional[List[configDom.OSBoot.bootEnum]] = Field(default=None, description="Ordre de démarrage des périphériques")
boot_menu:VMOSBootMenu = Field(default=VMOSBootMenu(), description="Menu de démarrage") boot_menu:OSBootMenu = Field(default=OSBootMenu(), description="Menu de démarrage")
class VMClock(BaseModel): class Clock(BaseModel):
class offsetEnum(str, Enum): offset:configDom.Clock.offsetEnum = Field(default=configDom.Clock.offsetEnum.utc, description="Sélection de l'offset ?")
utc = "utc"
localtime = "localtime"
timezone = "timezone"
variable = "variable"
absolute = "absolute"
offset:offsetEnum = Field(default=offsetEnum.utc, description="Sélection de l'offset ?")
class VMEvent(BaseModel): class Event(BaseModel):
class on_poweroffEnum(str, Enum): class on_poweroffEnum(str, Enum):
destroy = "destroy" destroy = "destroy"
restart = "restart" restart = "restart"
@ -92,21 +87,21 @@ class VMEvent(BaseModel):
pause = "pause" pause = "pause"
ignore = "ignore" ignore = "ignore"
on_poweroff:on_poweroffEnum = Field(default=on_poweroffEnum.destroy, description="Action lors d'un poweroff") on_poweroff:configDom.Dom.on_poweroffEnum = Field(default=configDom.Dom.on_poweroffEnum.destroy, description="Action lors d'un poweroff")
on_reboot:on_rebootEnum = Field(default=on_rebootEnum.restart, description="Action lors d'un redémarrage") on_reboot:configDom.Dom.on_rebootEnum = Field(default=configDom.Dom.on_rebootEnum.restart, description="Action lors d'un redémarrage")
on_crash:on_crashEnum = Field(default=on_crashEnum.restart, description="Action lors d'un crash de la VM") on_crash:configDom.Dom.on_crashEnum = Field(default=configDom.Dom.on_crashEnum.restart, description="Action lors d'un crash de la VM")
on_failurelock:on_failurelockEnum = element(default=on_failurelockEnum.ignore, description="A tester") on_failurelock:configDom.Dom.on_failurelockEnum = element(default=configDom.Dom.on_failurelockEnum.ignore, description="A tester")
class VMDevicesDisk(BaseModel): class DevicesDisk(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
pool_id:UUID4 = Field() pool_id:UUID4 = Field()
id:UUID4 = Field() id:UUID4 = Field()
size:int = Field() size:int = Field()
unit:libvirt.UnitEnum = Field(default=libvirt.UnitEnum.GiB) unit:configDom.UnitEnum = Field(default=configDom.UnitEnum.GiB)
bus:libvirt.DevicesDiskTarget.busEnum = Field(default=libvirt.DevicesDiskTarget.busEnum.scsi) bus:configDom.DevicesDiskTarget.busEnum = Field(default=configDom.DevicesDiskTarget.busEnum.scsi)
class VMDevicesCDROMSource(BaseModel): class DevicesCDROMSource(BaseModel):
class protocolEnum(str, Enum): class protocolEnum(str, Enum):
http = "http" http = "http"
https = "https" https = "https"
@ -116,60 +111,60 @@ class VMDevicesCDROMSource(BaseModel):
protocol:protocolEnum = attr(default=protocolEnum.https) protocol:protocolEnum = attr(default=protocolEnum.https)
name:str = attr() name:str = attr()
class VMDevicesCDROM(BaseModel): class DevicesCDROM(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
device:libvirt.DevicesDisk.deviceEnum = Field(default=libvirt.DevicesDisk.deviceEnum.cdrom, json_schema_extra={"hidden": True}) device:configDom.DevicesDisk.deviceEnum = Field(default=configDom.DevicesDisk.deviceEnum.cdrom, json_schema_extra={"hidden": True})
source:VMDevicesCDROMSource = element(default=None) source:DevicesCDROMSource = element(default=None)
class VMDevicesInterfaceGuest(BaseModel): class DevicesInterfaceGuest(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
addr:str = Field() addr:str = Field()
class VMDevicesInterface(BaseModel): class DevicesInterface(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
switch_id:UUID4 switch_id:UUID4
id:UUID4 id:UUID4
mac_addr:str = Field() mac_addr:str = Field()
class VMGraphics(BaseModel): class Graphics(BaseModel):
port:int = Field(default=-1) port:int = Field(default=-1)
autoport:bool = Field(default=False) autoport:bool = Field(default=False)
websocket:int = Field(default=-1) websocket:int = Field(default=-1)
password:Optional[str] = Field(default=None) password:Optional[str] = Field(default=None)
keymap:libvirt.Graphics.keymapEnum = Field(default=libvirt.Graphics.keymapEnum.fr) keymap:configDom.Graphics.keymapEnum = Field(default=configDom.Graphics.keymapEnum.fr)
power_control:bool = Field(default=False) power_control:bool = Field(default=False)
class VMAudio(BaseModel): class Audio(BaseModel):
pass pass
class VMVideoModel(BaseModel): class VideoModel(BaseModel):
type:libvirt.VideoModel.typeEnum = Field(default=libvirt.VideoModel.typeEnum.cirrus) type:configDom.VideoModel.typeEnum = Field(default=configDom.VideoModel.typeEnum.cirrus)
vram:int = Field(default=16384) vram:int = Field(default=16384)
primary:libvirt.YesNoEnum = Field(default=libvirt.YesNoEnum.YES) primary:configDom.YesNoEnum = Field(default=configDom.YesNoEnum.YES)
class VMVideo(BaseModel): class Video(BaseModel):
model:VMVideoModel = element() model:VideoModel = element()
class VMWatchdog(BaseModel): class Watchdog(BaseModel):
model:libvirt.Watchdog.modelEnum = Field(default=libvirt.Watchdog.modelEnum.itco) model:configDom.Watchdog.modelEnum = Field(default=configDom.Watchdog.modelEnum.itco)
action:libvirt.Watchdog.actionEnum = Field(default=libvirt.Watchdog.actionEnum.reset) action:configDom.Watchdog.actionEnum = Field(default=configDom.Watchdog.actionEnum.reset)
class VMTPM(BaseModel): class TPM(BaseModel):
model:libvirt.TPM.modelEnum = attr(default=libvirt.TPM.modelEnum.tpm_tis) model:configDom.TPM.modelEnum = attr(default=configDom.TPM.modelEnum.tpm_tis)
version:libvirt.TPMBackend.versionEnum = Field(default=libvirt.TPMBackend.versionEnum.v20) version:configDom.TPMBackend.versionEnum = Field(default=configDom.TPMBackend.versionEnum.v20)
class VMDevices(BaseModel): class Devices(BaseModel):
disks:Optional[List[VMDevicesDisk]] = Field(default=None, description="Disques virtuel") disks:Optional[List[DevicesDisk]] = Field(default=None, description="Disques virtuel")
cdrom:VMDevicesCDROM = Field(default=None, description="CDROM virtuel") cdrom:DevicesCDROM = Field(default=None, description="CDROM virtuel")
interfaces:Optional[List[VMDevicesInterface]] = Field(default=None, description="Interfaces réseau") interfaces:Optional[List[DevicesInterface]] = Field(default=None, description="Interfaces réseau")
graphics:Optional[VMGraphics] = Field(default=None, description="Console VNC") graphics:Optional[Graphics] = Field(default=None, description="Console VNC")
audio:VMAudio = Field(default=None, description="Carte Audio") audio:Audio = Field(default=None, description="Carte Audio")
video:VMVideo = Field(default=None, description="Carte Vidéo") video:Video = Field(default=None, description="Carte Vidéo")
watchdog:VMWatchdog = Field(default=None, description="Chien de garde") watchdog:Watchdog = Field(default=None, description="Chien de garde")
tpm:VMTPM = Field(default=None, description="Puce de sécurité") tpm:TPM = Field(default=None, description="Puce de sécurité")
class VM(BaseModel): class VM(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)
@ -181,25 +176,25 @@ class VM(BaseModel):
description:Optional[str] = Field(default=None) description:Optional[str] = Field(default=None)
# CPU Allocation # CPU Allocation
vcpu:VMVCPU = Field() vcpu:VCPU = Field()
# Memory Allocation # Memory Allocation
memory:VMMemory = Field() memory:Memory = Field()
# Pool of resource (Experimental) # Pool of resource (Experimental)
resource:VMResource = Field(default=VMResource(), description="Pool de resources") resource:Resource = Field(default=Resource(), description="Pool de resources")
# BIOS BootLoader # BIOS BootLoader
os:VMOS = Field(default=VMOS(), description="Options VM: Options de démarrage.") os:OS = Field(default=OS(), description="Options VM: Options de démarrage.")
# Clock configuration # Clock configuration
clock:VMClock = Field(default=VMClock(), description="Options VM: Gestion de l'horloge") clock:Clock = Field(default=Clock(), description="Options VM: Gestion de l'horloge")
# Event configuration # Event configuration
event:VMEvent = Field(default=VMEvent(), description="Options VM: Gestion des évènements") event:Event = Field(default=Event(), description="Options VM: Gestion des évènements")
# Devices # Devices
devices:VMDevices = Field(default=VMDevices(), description="Matériel virtuel: Périphériques") devices:Devices = Field(default=Devices(), description="Matériel virtuel: Périphériques")
"""VM LIST GET""" """VM LIST GET"""
class VMs(BaseModel): class VMs(BaseModel):
@ -217,19 +212,19 @@ class VMs(BaseModel):
datacenter:Optional[Datacenter] = Field(default=[]) datacenter:Optional[Datacenter] = Field(default=[])
"""VM GET""" """VM GET"""
class VMGetVCPU(VMVCPU): class VMGetVCPU(VCPU):
max:Optional[int] = Field(default=None, description="Maximum de VCPUs") max:Optional[int] = Field(default=None, description="Maximum de VCPUs")
class VMGetOS(VMOS): class VMGetOS(OS):
guest_info:Optional[VMOSInfo] = Field(default=None, description="Information de l'OS client") guest_info:Optional[OSInfo] = Field(default=None, description="Information de l'OS client")
class VMGetDevicesDisk(VMDevicesDisk): class VMGetDevicesDisk(DevicesDisk):
allocated:float = Field(default=None) allocated:float = Field(default=None)
class VMGetDevicesInterface(VMDevicesInterface): class VMGetDevicesInterface(DevicesInterface):
guest_info:Optional[List[VMDevicesInterfaceGuest]] = Field(default=None, description="Information de configuration de la carte réseau.") guest_info:Optional[List[DevicesInterfaceGuest]] = Field(default=None, description="Information de configuration de la carte réseau.")
class VMGetDevices(VMDevices): class VMGetDevices(Devices):
disks:Optional[List[VMGetDevicesDisk]] = Field(default=None, description="Disques virtuel") disks:Optional[List[VMGetDevicesDisk]] = Field(default=None, description="Disques virtuel")
interfaces:Optional[List[VMGetDevicesInterface]] = Field(default=None, description="Interfaces réseau") interfaces:Optional[List[VMGetDevicesInterface]] = Field(default=None, description="Interfaces réseau")
@ -240,7 +235,7 @@ class VMGet(VM):
devices:VMGetDevices = Field(default=VMGetDevices(), description="Matériel virtuel: Périphériques") devices:VMGetDevices = Field(default=VMGetDevices(), description="Matériel virtuel: Périphériques")
"""VM POST""" """VM POST"""
class VMCreate(VM): class VMPost(VM):
id:UUID4 = Field(default_factory=uuid4) id:UUID4 = Field(default_factory=uuid4)
datacenter_id:UUID4 = Field() datacenter_id:UUID4 = Field()
@ -248,8 +243,8 @@ class VMCreate(VM):
class VMDelete(BaseModel): class VMDelete(BaseModel):
id:UUID4 = Field() id:UUID4 = Field()
class VMHardDisk(BaseModel): class HardDisk(BaseModel):
pass pass
class VMNetInterfaceCreate(BaseModel): class NetInterfaceCreate(BaseModel):
pass pass

View File

@ -4,7 +4,21 @@ from uuid import uuid4
from pydantic import UUID4 from pydantic import UUID4
from pydantic_xml import BaseXmlModel, attr, element, wrapped from pydantic_xml import BaseXmlModel, attr, element, wrapped
from deevirt_schema.libvirt.enum import YesNoEnum, OnOffEnum, UnitEnum from enum import Enum
class YesNoEnum(str, Enum):
YES = "yes"
NO = "no"
class OnOffEnum(str, Enum):
ON = "on"
OFF = "off"
class UnitEnum(str, Enum):
KiB = "KiB"
MiB = "MiB"
GiB = "GiB"
TiB = "TiB"
class Metadata(BaseXmlModel, search_mode='ordered'): class Metadata(BaseXmlModel, search_mode='ordered'):
# wrapped("metadata", element(default=None, ns="deevirt", nsmap={'deevirt': 'https://deevirt.com/'})) # wrapped("metadata", element(default=None, ns="deevirt", nsmap={'deevirt': 'https://deevirt.com/'}))
@ -613,7 +627,7 @@ class Devices(BaseXmlModel, search_mode='ordered'):
watchdog:Optional[Watchdog] = element(default=None) watchdog:Optional[Watchdog] = element(default=None)
tpm:Optional[TPM] = element(default=None) tpm:Optional[TPM] = element(default=None)
class VM(BaseXmlModel, tag='domain', search_mode='ordered'): class Dom(BaseXmlModel, tag='domain', search_mode='ordered'):
class on_poweroffEnum(str, Enum): class on_poweroffEnum(str, Enum):
destroy = "destroy" destroy = "destroy"
restart = "restart" restart = "restart"

View File

@ -1,15 +0,0 @@
from enum import Enum
class YesNoEnum(str, Enum):
YES = "yes"
NO = "no"
class OnOffEnum(str, Enum):
ON = "on"
OFF = "off"
class UnitEnum(str, Enum):
KiB = "KiB"
MiB = "MiB"
GiB = "GiB"
TiB = "TiB"