1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/computes/api/serializers.py
catborise eb3bc69754 fix(computes): the compute forms save their own kind, a password as typed, and say when the host is not reached
The kind of a compute came from a hidden form field: posted as another
number it was saved, and the edit page of that compute failed. Each form
now saves its own kind. Passwords lost their leading and trailing spaces
on the forms and the API, so a SASL password with one never worked. A
host name whose last label is all digits, such as an IPv4 address with a
trailing dot, was taken and libvirt could not resolve it. A host saved with
a wrong password or not answering was saved silently; the page now says
why the panel cannot connect to it.
2026-11-12 09:19:15 +03:00

51 lines
1.7 KiB
Python

from copy import copy
from computes.models import Compute
from computes.validators import validate_gstfsd_key
from rest_framework import serializers
from vrtManager.connection import CONN_SOCKET, CONN_SSH, CONN_TCP, CONN_TLS
class ComputeSerializer(serializers.ModelSerializer):
password = serializers.CharField(
style={"input_type": "password"},
write_only=True,
trim_whitespace=False,
required=False,
allow_blank=True,
allow_null=True,
)
gstfsd_key = serializers.CharField(
style={"input_type": "password"},
write_only=True,
required=False,
allow_blank=True,
validators=[validate_gstfsd_key],
)
# Use a radio input instead of a select input.
conn_types = (
(CONN_SSH, "SSH"),
(CONN_TCP, "TCP"),
(CONN_TLS, "TLS"),
(CONN_SOCKET, "SOCK"),
)
type = serializers.ChoiceField(choices=conn_types)
def validate_password(self, value):
# as on the web form, an empty password on an edit keeps the stored one
return value or (self.instance.password if self.instance else value)
def validate_gstfsd_key(self, value):
return value or (self.instance.gstfsd_key if self.instance else value)
def validate(self, attrs):
# as the web form: the connection is tried and its host checked (Compute.clean)
compute = copy(self.instance) if self.instance else Compute()
for field, value in attrs.items():
setattr(compute, field, value)
compute.clean()
return {**attrs, "host_uuid": compute.host_uuid}
class Meta:
model = Compute
fields = ["id", "name", "hostname", "login", "password", "type", "details", "migration_address", "gstfsd_key"]