1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

refactor: the API's serializers and the new VM form no longer need models

Networks, Interfaces, Storages, Storage, Volume, MigrateInstance and
CreateInstance were unmanaged models with no table, there only so that
ModelSerializers and the new VM ModelForm could take their fields. They
made content types and migrations, and their DO_NOTHING foreign keys
added reverse accessors that meant nothing (Compute.host,
Compute.target_host, Instance.source_host).

The serializers are plain Serializers and NewVMForm a Form, field for
field as the models gave them (types, lengths, required, defaults, the
minimum of 1 for vCPUs, memory and disk size; the form's labels are
still derived from the field names). Compared field by field before and
after, the API schema changes only where the models were wrong:
- no int64 bounds on the listing fields;
- the interface fields lose the titles that named the wrong column
  (type was "Status", state "Device", mac "Forward");
- a pool's volumes is a list of objects (it was described as an
  integer) and a volume's allocation an integer (it was untyped).
One migration per app deletes the models; none touches a table.
This commit is contained in:
catborise 2026-11-06 11:06:40 +03:00
parent f3bdeddb0b
commit f1f52aac42
17 changed files with 434 additions and 736 deletions

View file

@ -1,6 +1,4 @@
from computes.models import Compute
from django.conf import settings
from django.core.validators import MinValueValidator
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
@ -403,76 +401,6 @@ class Instance(models.Model):
return self.proxy.get_image_formats()
class MigrateInstance(models.Model):
instance = models.ForeignKey(
Instance, related_name="source_host", on_delete=models.DO_NOTHING
)
target_compute = models.ForeignKey(
Compute, related_name="target_host", on_delete=models.DO_NOTHING
)
live = models.BooleanField(_("Live"))
offline = models.BooleanField(_("Offline"))
autoconverge = models.BooleanField(_("Auto Converge"), default=True)
compress = models.BooleanField(_("Compress"), default=False)
unsafe = models.BooleanField(_("Unsafe"), default=False)
class Meta:
managed = False
class CreateInstance(models.Model):
compute = models.ForeignKey(
Compute, related_name="host", on_delete=models.DO_NOTHING
)
name = models.CharField(
max_length=64,
error_messages={"required": _("No Virtual Machine name has been entered")},
)
firmware = models.CharField(max_length=64)
# an emulated TPM 2.0, as Windows 11 needs
tpm = models.BooleanField(default=False)
vcpu = models.IntegerField(
error_messages={"required": _("No VCPU has been entered")}, validators=[MinValueValidator(1)]
)
# a mode of libvirt's (host-model, host-passthrough, maximum) or a CPU model's name
vcpu_mode = models.CharField(max_length=64)
disk = models.IntegerField(blank=True)
memory = models.IntegerField(
error_messages={"required": _("No RAM size has been entered")}, validators=[MinValueValidator(1)]
)
networks = models.CharField(
max_length=256,
error_messages={"required": _("No Network pool has been choosen")},
)
nwfilter = models.CharField(max_length=256, blank=True)
net_model = models.CharField(max_length=256, blank=True)
storage = models.CharField(max_length=256, blank=True)
template = models.CharField(max_length=256, blank=True)
images = models.CharField(max_length=256, blank=True)
cache_mode = models.CharField(
max_length=16, error_messages={"required": _("Please select HDD cache mode")}
)
hdd_size = models.IntegerField(blank=True, validators=[MinValueValidator(1)])
meta_prealloc = models.BooleanField(default=False, blank=True)
virtio = models.BooleanField(default=True)
qemu_ga = models.BooleanField(default=False)
mac = models.CharField(max_length=255, blank=True) # one per NIC, comma separated
add_cdrom = models.CharField(max_length=16)
# a NoCloud seed on a CD-ROM: the hostname and the creating user's SSH keys
cloud_init = models.BooleanField(default=False)
add_input = models.CharField(max_length=16)
video = models.CharField(
max_length=16, error_messages={"required": _("Please select a video driver")}
)
listener_addr = models.CharField(
max_length=20, choices=settings.QEMU_CONSOLE_LISTENER_ADDRESSES
)
class Meta:
managed = False
class InstanceTombstone(models.Model):
"""Ownership of a VM that disappeared from its compute.