1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00
webvirtcloud/computes/api/viewsets.py
catborise 12e33f7964 fix(computes): a compute is not edited while a job on it is admitted
A job resolves its compute's connection when it runs. Changing the
compute's hostname meanwhile sent the job to another host, where a VM
with the same UUID (a copy) could be deleted or changed instead. An edit
now holds the compute as a delete does, and is refused while a job or VM
operation on it is admitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
2026-11-10 13:19:39 +03:00

74 lines
2.7 KiB
Python

from admin.permissions import IsSuperUser
from computes.models import Compute
from computes.utils import libvirt_compute_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]
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):
compute = serializer.instance
subject = f"Compute {compute.name} ({compute.hostname})"
before = snapshot(compute)
with audit(self.request, compute, subject, "change") as entry, libvirt_compute_lock(compute):
serializer.save()
fields = changed_fields(before, snapshot(compute))
entry.action = "change " + ", ".join(fields)
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))