mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
Backups in the menu now opens a page of every backup, newest first, filtered by VM name, target and state; a removed VM's backups are marked. Each backup, there and on the VM's Backups tab, has a Delete button (also DELETE /api/v1/backups/<uuid>/, 202 with the job). The backup.delete job holds the backup and its target. Through the first of the target's computes that reaches its pool, it marks the backup deleting, removes the manifest first, so the target no longer holds a complete backup, then every other file named from the backup's UUID (also those a failed backup left), then the row. Recovery runs the same steps again. A backup being made is refused: its job is cancelled instead. States and consistency are shown in the reader's language.
44 lines
2.1 KiB
Python
44 lines
2.1 KiB
Python
from django.urls import reverse
|
|
from drf_spectacular.utils import extend_schema
|
|
from rest_framework import mixins, status, viewsets
|
|
from rest_framework.response import Response
|
|
|
|
from admin.permissions import IsSuperUser
|
|
from operations import jobs
|
|
from operations.api.serializers import OperationSerializer
|
|
from webvirtcloud.serializers import ErrorSerializer
|
|
|
|
from .models import Backup
|
|
from .serializers import BackupSerializer, CreateBackupSerializer, SubmissionSerializer
|
|
|
|
|
|
def _accepted(request, op):
|
|
return Response(OperationSerializer(op, context={"request": request}).data, status=status.HTTP_202_ACCEPTED,
|
|
headers={"Location": reverse("operation-detail", args=[op.pk])})
|
|
|
|
|
|
class BackupViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
|
"""VM backups: list, read, back a VM up and delete a backup (jobs,
|
|
answered 202)."""
|
|
|
|
permission_classes = [IsSuperUser]
|
|
serializer_class = BackupSerializer
|
|
queryset = Backup.objects.select_related("target").prefetch_related("files")
|
|
lookup_field = "uuid"
|
|
|
|
@extend_schema(request=CreateBackupSerializer, responses={202: OperationSerializer, 400: dict, 409: ErrorSerializer})
|
|
def create(self, request):
|
|
serializer = CreateBackupSerializer(data=request.data)
|
|
if not serializer.is_valid():
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
data = serializer.validated_data
|
|
raw = {"instance": data["instance"].pk, "target": data["target"].pk, "frozen": data["frozen"]}
|
|
return _accepted(request, jobs.submit("backup.create", request.user, raw, data["submission_key"]))
|
|
|
|
@extend_schema(request=SubmissionSerializer, responses={202: OperationSerializer, 409: ErrorSerializer})
|
|
def destroy(self, request, uuid=None):
|
|
backup = self.get_object()
|
|
serializer = SubmissionSerializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
key = serializer.validated_data["submission_key"]
|
|
return _accepted(request, jobs.submit("backup.delete", request.user, {"backup": str(backup.uuid)}, key))
|