1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +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,28 +1,35 @@
from rest_framework import serializers
from storages.models import Storages, Storage, Volume
class StoragesSerializer(serializers.ModelSerializer):
class StoragesSerializer(serializers.Serializer):
"""A storage pool as the API lists it."""
class Meta:
model = Storages
fields = ['name', 'status', 'type', 'size', 'volumes']
name = serializers.CharField(max_length=20)
status = serializers.IntegerField()
type = serializers.CharField(max_length=100)
size = serializers.IntegerField()
volumes = serializers.IntegerField()
class StorageSerializer(serializers.ModelSerializer):
volumes = serializers.ReadOnlyField()
class Meta:
model = Storage
fields = ['state', 'size', 'free', 'status', 'path', 'type', 'autostart', 'volumes']
class StorageSerializer(serializers.Serializer):
"""A storage pool's details."""
state = serializers.IntegerField()
size = serializers.IntegerField()
free = serializers.IntegerField()
status = serializers.CharField(max_length=128)
path = serializers.CharField(max_length=128)
type = serializers.CharField(max_length=128)
autostart = serializers.BooleanField()
# the pool's volumes (wvmStorage.update_volumes); none while it is stopped
volumes = serializers.ListField(child=serializers.DictField(), read_only=True, allow_null=True)
class VolumeSerializer(serializers.ModelSerializer):
class VolumeSerializer(serializers.Serializer):
# what the web form (storages.forms.CreateVolumeForm) takes
name = serializers.RegexField(r"^[a-zA-Z0-9._-]+$", max_length=120)
type = serializers.ChoiceField(choices=[("qcow2", "qcow2 (recommended)"), ("qcow", "qcow"), ("raw", "raw")],
label="Format")
allocation = serializers.IntegerField(read_only=True)
size = serializers.IntegerField(min_value=1) # GiB
allocation = serializers.ReadOnlyField()
meta_prealloc = serializers.BooleanField(write_only=True, default=False)
class Meta:
model = Volume
fields = ['name', 'type', 'allocation', 'size', 'meta_prealloc']