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:
parent
11b447f681
commit
f4051213fb
15 changed files with 508 additions and 163 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from computes.api.serializers import TAGS
|
||||
from computes.models import Compute
|
||||
from django.conf import settings
|
||||
from instances.models import Flavor, Instance
|
||||
|
|
@ -7,9 +8,11 @@ from rest_framework import serializers
|
|||
|
||||
|
||||
class InstanceSerializer(serializers.ModelSerializer):
|
||||
tags = serializers.SlugRelatedField(**TAGS)
|
||||
|
||||
class Meta:
|
||||
model = Instance
|
||||
fields = ["id", "compute", "name", "uuid", "is_template", "created", "drbd"]
|
||||
fields = ["id", "compute", "name", "uuid", "is_template", "created", "drbd", "tags"]
|
||||
|
||||
|
||||
# what a VM's device dicts tell of the host: its files and pools
|
||||
|
|
@ -18,6 +21,7 @@ HOST_KEYS = {"image", "storage", "path", "backing_file", "loader"}
|
|||
|
||||
class InstanceDetailsSerializer(serializers.ModelSerializer):
|
||||
networks = serializers.SerializerMethodField()
|
||||
tags = serializers.SlugRelatedField(**TAGS)
|
||||
|
||||
def get_networks(self, instance) -> list[dict]:
|
||||
"""Each NIC with its addresses (None: none known), which the VM page
|
||||
|
|
@ -58,6 +62,7 @@ class InstanceDetailsSerializer(serializers.ModelSerializer):
|
|||
"status",
|
||||
"uuid",
|
||||
"name",
|
||||
"tags",
|
||||
"title",
|
||||
"description",
|
||||
"is_template",
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from admin.permissions import IsSuperUser
|
||||
from computes import utils
|
||||
from computes.api.serializers import TAG_PARAMETER, with_tag
|
||||
from computes.models import Compute
|
||||
from django.contrib.messages import get_messages
|
||||
from django.http import Http404
|
||||
|
|
@ -57,6 +58,7 @@ class InstancesViewSet(viewsets.ViewSet):
|
|||
serializer_class = InstanceSerializer
|
||||
lookup_value_converter = "int"
|
||||
|
||||
@extend_schema(parameters=[TAG_PARAMETER])
|
||||
def list(self, request):
|
||||
# as the VM list: the hosts read first (all at once, one that does not answer
|
||||
# left as last read), so that VMs added, renamed or removed outside are seen
|
||||
|
|
@ -74,7 +76,7 @@ class InstancesViewSet(viewsets.ViewSet):
|
|||
userinstance__user=request.user
|
||||
).prefetch_related("userinstance_set")
|
||||
serializer = InstanceSerializer(
|
||||
queryset, many=True, context={"request": request}
|
||||
with_tag(queryset, request), many=True, context={"request": request}
|
||||
)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
|
@ -95,6 +97,7 @@ class InstanceViewSet(viewsets.ViewSet):
|
|||
serializer_class = InstanceSerializer
|
||||
lookup_value_converter = "int"
|
||||
|
||||
@extend_schema(parameters=[TAG_PARAMETER])
|
||||
def list(self, request, compute_pk=None):
|
||||
compute = get_object_or_404(Compute, pk=compute_pk)
|
||||
|
||||
|
|
@ -119,7 +122,7 @@ class InstanceViewSet(viewsets.ViewSet):
|
|||
compute=compute, userinstance__user=request.user
|
||||
).prefetch_related("userinstance_set")
|
||||
serializer = InstanceSerializer(
|
||||
queryset, many=True, context={"request": request}
|
||||
with_tag(queryset, request), many=True, context={"request": request}
|
||||
)
|
||||
|
||||
return Response(serializer.data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue