__init__
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""Pydantic request and response models for the UVM HTTP API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ipaddress import IPv4Address
|
||||
|
||||
from pydantic import BaseModel, Field, SecretStr
|
||||
|
||||
from .domain import VmRecord
|
||||
|
||||
|
||||
class HealthResponse(BaseModel):
|
||||
status: str = "ok"
|
||||
|
||||
|
||||
class InstallRequest(BaseModel):
|
||||
force: bool = False
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
class InstallResponse(BaseModel):
|
||||
firecracker: str
|
||||
kernel: str
|
||||
rootfs: str
|
||||
|
||||
|
||||
class VmCreateRequest(BaseModel):
|
||||
cpu: float = Field(default=1, gt=0)
|
||||
ram: str | int | float = "512"
|
||||
guest_ip: IPv4Address | None = None
|
||||
username: str = "root"
|
||||
password: SecretStr = SecretStr("root")
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
|
||||
class VmResponse(BaseModel):
|
||||
id: str
|
||||
cpu: float
|
||||
ram_mib: int
|
||||
guest_ip: str
|
||||
gateway: str
|
||||
mac: str
|
||||
username: str
|
||||
status: str
|
||||
observed_status: str
|
||||
pid: int | None
|
||||
created_at: int
|
||||
updated_at: int
|
||||
last_error: str | None
|
||||
|
||||
|
||||
class DestroyResponse(BaseModel):
|
||||
id: str
|
||||
status: str = "terminated"
|
||||
|
||||
|
||||
def vm_response(vm: VmRecord, *, observed_status: str | None = None) -> VmResponse:
|
||||
return VmResponse(
|
||||
id=vm.id,
|
||||
cpu=vm.cpu,
|
||||
ram_mib=vm.ram_mib,
|
||||
guest_ip=vm.guest_ip,
|
||||
gateway=vm.gateway,
|
||||
mac=vm.mac,
|
||||
username=vm.username,
|
||||
status=vm.status,
|
||||
observed_status=observed_status or vm.status,
|
||||
pid=vm.pid,
|
||||
created_at=vm.created_at,
|
||||
updated_at=vm.updated_at,
|
||||
last_error=vm.last_error,
|
||||
)
|
||||
Reference in New Issue
Block a user