1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat: a Tags page and tags in the API

Administration gets a Tags page: each tag with how many VMs and hosts
have it, renamed (on the VMs, hosts and removed VMs that keep it) or
deleted (taken off all of them). Both are logged.

The API gives a VM's and a host's tags by name, read only, and its VM
and host lists take ?tag= for those with a tag.
This commit is contained in:
catborise 2026-11-18 20:50:59 +03:00
parent 11b447f681
commit f4051213fb
15 changed files with 508 additions and 163 deletions

View file

@ -2,11 +2,24 @@ from copy import copy
from computes.models import Compute
from computes.validators import validate_gstfsd_key
from drf_spectacular.utils import OpenApiParameter
from rest_framework import serializers
from vrtManager.connection import CONN_SOCKET, CONN_SSH, CONN_TCP, CONN_TLS
# a VM's or host's tags, by name: set on the web pages only
TAGS = {"many": True, "read_only": True, "slug_field": "name"}
TAG_PARAMETER = OpenApiParameter("tag", str, description="Only those with this tag")
def with_tag(queryset, request):
"""queryset's VMs or hosts with the ?tag= of request, all without one; their tags read at once."""
tag = request.query_params.get("tag")
return (queryset.filter(tags__name=tag) if tag else queryset).prefetch_related("tags")
class ComputeSerializer(serializers.ModelSerializer):
tags = serializers.SlugRelatedField(**TAGS)
password = serializers.CharField(
style={"input_type": "password"},
write_only=True,
@ -48,4 +61,5 @@ class ComputeSerializer(serializers.ModelSerializer):
class Meta:
model = Compute
fields = ["id", "name", "hostname", "login", "password", "type", "details", "migration_address", "gstfsd_key"]
fields = ["id", "name", "hostname", "login", "password", "type", "details", "migration_address", "gstfsd_key",
"tags"]

View file

@ -9,7 +9,7 @@ from rest_framework import viewsets
from rest_framework.response import Response
from vrtManager.create import wvmCreate
from .serializers import ComputeSerializer
from .serializers import TAG_PARAMETER, ComputeSerializer, with_tag
class ComputeViewSet(viewsets.ModelViewSet):
@ -22,6 +22,14 @@ class ComputeViewSet(viewsets.ModelViewSet):
serializer_class = ComputeSerializer
permission_classes = [IsSuperUser]
def get_queryset(self):
queryset = super().get_queryset()
return with_tag(queryset, self.request) if self.action == "list" else queryset
@extend_schema(parameters=[TAG_PARAMETER])
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
# checked and saved under hosts_lock, as on the web (Compute.clean)
def create(self, request, *args, **kwargs):
with hosts_lock():