1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 09:26:03 +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

69
admin/tests/test_tags.py Normal file
View file

@ -0,0 +1,69 @@
"""Superusers rename and delete the tags of VMs and hosts."""
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from computes.models import Compute, Tag
from instances.models import Instance, InstanceTombstone
from logs.models import Logs
class TagAdminTests(TestCase):
def setUp(self):
self.client.force_login(User.objects.create_superuser("tg-admin", "tg@example.com", "pw"))
self.prod = Tag.objects.create(name="prod")
compute = Compute.objects.create(name="tg", hostname="192.0.2.7", login="u", password="p", type=1)
compute.tags.add(self.prod)
Instance.objects.create(compute=compute, name="vm", uuid="12345678-0000-0000-0000-00000000dd01").tags.add(self.prod)
self.tombstone = InstanceTombstone.objects.create(uuid="12345678-0000-0000-0000-00000000dd02", name="gone",
tags=["prod", "db"])
def messages(self):
return list(Logs.objects.filter(message__startswith="Tag").order_by("id").values_list("message", flat=True))
def test_the_list_counts_each_tag_s_vms_and_hosts(self):
response = self.client.get(reverse("admin:tag_list"))
self.assertEqual([(t.name, t.vm_count, t.host_count) for t in response.context["tags"]], [("prod", 1, 1)])
def test_a_rename_keeps_the_tag_on_its_vms_hosts_and_removed_vms(self):
self.client.post(reverse("admin:tag_update", args=[self.prod.pk]), {"name": "Live"})
self.prod.refresh_from_db()
self.assertEqual(self.prod.name, "live")
self.assertEqual(self.prod.instances.count(), 1)
self.tombstone.refresh_from_db()
self.assertEqual(self.tombstone.tags, ["live", "db"])
self.assertEqual(self.messages(), ["Tag prod: rename to live"])
def test_a_name_taken_or_bad_is_refused(self):
Tag.objects.create(name="db")
for name in ("DB", "two words"):
with self.subTest(name):
response = self.client.post(reverse("admin:tag_update", args=[self.prod.pk]), {"name": name})
self.assertEqual(response.status_code, 200)
self.assertTrue(response.context["form"].errors)
self.prod.refresh_from_db()
self.assertEqual(self.prod.name, "prod")
def test_a_delete_takes_it_off_everything(self):
self.assertContains(self.client.get(reverse("admin:tag_delete", args=[self.prod.pk])), "1 VM and 1 host")
self.client.post(reverse("admin:tag_delete", args=[self.prod.pk]))
self.assertFalse(Tag.objects.exists())
self.assertFalse(Instance.objects.get(name="vm").tags.exists())
self.assertEqual(self.messages(), ["Tag prod: delete"])
def test_a_deleted_name_made_again_is_not_given_to_a_vm_that_returns(self):
self.client.post(reverse("admin:tag_delete", args=[self.prod.pk]))
self.tombstone.refresh_from_db()
self.assertEqual(self.tombstone.tags, ["db"])
def test_the_same_name_is_no_rename(self):
self.client.post(reverse("admin:tag_update", args=[self.prod.pk]), {"name": "PROD"})
self.assertEqual(self.messages(), [])
def test_only_superusers(self):
self.client.force_login(User.objects.create_user("tg-user", password="pw"))
for url in (reverse("admin:tag_list"), reverse("admin:tag_update", args=[self.prod.pk]),
reverse("admin:tag_delete", args=[self.prod.pk])):
self.assertEqual(self.client.post(url, {"name": "x"}).status_code, 403)
self.assertEqual(Tag.objects.get().name, "prod")