1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00
webvirtcloud/computes/api/viewsets.py
catborise 29ec20682b fix(computes): a libvirt host is one compute, and a compute's VMs stay on their host
One host could be added twice, under another name, as TCP and SSH, or by
another address: its VMs were listed once per record, each with its own
owners, and counted twice in the capacity totals. A compute with VMs could
also be pointed at another host: the owners of a VM then reached the
VM with the same UUID there, a restored or copied one.

A compute now keeps its libvirt host UUID. Adding a compute or changing
its connection, on the web or the API, connects and reads it: a host that
is already a compute is refused, and a compute with VMs moves only to an
address of the same host, which must answer. Computes added before are
read when another one is checked.
2026-11-12 09:19:03 +03:00

84 lines
3.2 KiB
Python

from admin.permissions import IsSuperUser
from computes.models import Compute
from computes.utils import hosts_lock
from django.shortcuts import get_object_or_404
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema
from logs.audit import audit, changed_fields, snapshot
from rest_framework import viewsets
from rest_framework.response import Response
from vrtManager.create import wvmCreate
from .serializers import ComputeSerializer
class ComputeViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows computes to be viewed or edited.
"""
queryset = Compute.objects.all().order_by("name")
lookup_value_converter = "int"
serializer_class = ComputeSerializer
permission_classes = [IsSuperUser]
# checked and saved under hosts_lock, as on the web (Compute.clean)
def create(self, request, *args, **kwargs):
with hosts_lock():
return super().create(request, *args, **kwargs)
def update(self, request, *args, **kwargs):
# the row is written here, as on the web: a busy compute is refused with one too
compute = self.get_object()
with audit(request, compute, f"Compute {compute.name} ({compute.hostname})", "change") as self.entry, \
hosts_lock(compute):
self.entry.skip = True # until a change is saved
return super().update(request, *args, **kwargs)
def perform_create(self, serializer):
data = serializer.validated_data
with audit(self.request, data["name"], f"Compute {data['name']} ({data['hostname']})", "add"):
serializer.save()
def perform_update(self, serializer):
before = snapshot(serializer.instance)
serializer.save()
fields = changed_fields(before, snapshot(serializer.instance))
self.entry.action = "change " + ", ".join(fields)
self.entry.skip = not fields
def perform_destroy(self, instance):
with audit(self.request, instance, f"Compute {instance.name} ({instance.hostname})", "delete"):
instance.delete()
class ComputeArchitecturesView(viewsets.ViewSet):
permission_classes = [IsSuperUser]
# The list is a mapping of architecture -> machine types, not an array,
# so drf-spectacular would name it "retrieve" and clash with retrieve().
@extend_schema(operation_id="api_v1_computes_archs_list", responses=OpenApiTypes.OBJECT)
def list(self, request, compute_pk=None):
"""
Return a list of supported host architectures.
"""
compute = get_object_or_404(Compute, pk=compute_pk)
conn = wvmCreate(
compute.hostname,
compute.login,
compute.password,
compute.type,
)
return Response(conn.get_hypervisors_machines())
@extend_schema(responses={200: {"type": "array", "items": {"type": "string"}}})
def retrieve(self, request, compute_pk=None, pk=None):
compute = get_object_or_404(Compute, pk=compute_pk)
conn = wvmCreate(
compute.hostname,
compute.login,
compute.password,
compute.type,
)
return Response(conn.get_machine_types(pk))