mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(tags): second audit: unchanged saves, report, other lists, hosts list
- Saving a VM's tags unchanged writes no audit row. - The VM report has a Tags column. - A host's VM list and a user's VMs (account page) show the VMs' tags. - The hosts list says "No host matches the filters." with Clear the filters, as the VM list does; the tag list keeps to a phone's width with a long tag. - A removed VM's delete page says its owners and tags no longer come back; the VM history rows say "owners and tags kept/restored". - The API's ?tag= is trimmed; Tom Select's "Add tag" line shows the name in lower case as it is made; Bulk Migrate's tag list goes back to its title after a choice, so the same tag can be chosen again.
This commit is contained in:
parent
45225df1a4
commit
b74e798e70
23 changed files with 376 additions and 324 deletions
|
|
@ -40,6 +40,11 @@ class VmTagsTests(TestCase):
|
|||
self.assertEqual(list(Logs.objects.filter(message__startswith="VM").order_by("id").values_list("message", flat=True)),
|
||||
["VM vt-vm: tags new-one, prod", "VM vt-vm: tags none"])
|
||||
|
||||
def test_the_same_tags_again_are_no_change(self):
|
||||
self.post(self.admin, ["prod"])
|
||||
self.post(self.admin, ["PROD"])
|
||||
self.assertEqual(Logs.objects.filter(message__startswith="VM vt-vm: tags").count(), 1)
|
||||
|
||||
def test_a_bad_name_changes_nothing(self):
|
||||
self.vm.tags.add(Tag.objects.create(name="prod"))
|
||||
self.post(self.admin, ["two words"])
|
||||
|
|
@ -64,6 +69,14 @@ class VmTagsTests(TestCase):
|
|||
self.assertEqual(InstanceTombstone.objects.get().tags, ["prod"])
|
||||
self.assertFalse(Logs.objects.filter(message__startswith="VM vt-vm: tags").exists())
|
||||
|
||||
def test_the_host_s_vm_list_and_the_user_s_vms_show_them(self):
|
||||
self.vm.tags.add(Tag.objects.create(name="prod"))
|
||||
self.client.force_login(self.admin)
|
||||
with patch("computes.views.utils.refresh_instance_database"), patch("computes.views.vm_states", return_value={}), \
|
||||
patch("computes.views.read_list_rows"):
|
||||
self.assertContains(self.client.get(reverse("instances", args=[self.vm.compute_id])), "prod")
|
||||
self.assertContains(self.client.get(reverse("accounts:account", args=[self.owner.id])), "prod")
|
||||
|
||||
def test_the_vm_list_shows_them(self):
|
||||
self.vm.tags.add(Tag.objects.create(name="prod"))
|
||||
html = render_to_string("instance_name_cell.html", {"instance": self.vm})
|
||||
|
|
@ -138,11 +151,16 @@ class TagsApiTests(TestCase):
|
|||
self.assertEqual([(c["name"], c["tags"]) for c in self.client.get("/api/v1/computes/").json()],
|
||||
[("ta", ["prod"]), ("ta2", [])])
|
||||
self.assertEqual([c["name"] for c in self.client.get("/api/v1/computes/?tag=prod").json()], ["ta"])
|
||||
self.assertEqual([c["name"] for c in self.client.get("/api/v1/computes/?tag=PROD").json()], ["ta"])
|
||||
self.assertEqual([c["name"] for c in self.client.get("/api/v1/computes/?tag=%20PROD%20").json()], ["ta"])
|
||||
|
||||
def test_a_tag_asked_of_one_host_is_no_filter(self):
|
||||
self.assertEqual(self.client.get(f"/api/v1/computes/{self.compute.id}/?tag=other").status_code, 200)
|
||||
|
||||
def test_the_tags_are_not_set_through_the_api(self):
|
||||
self.client.patch(f"/api/v1/computes/{self.compute.id}/", {"tags": []}, content_type="application/json")
|
||||
self.assertTrue(self.compute.tags.exists())
|
||||
with patch("computes.models.Compute.clean"): # the host is not reached
|
||||
response = self.client.patch(f"/api/v1/computes/{self.compute.id}/", {"tags": [], "details": "rack 2"},
|
||||
content_type="application/json")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.compute.refresh_from_db()
|
||||
self.assertEqual((self.compute.details, list(self.compute.tags.values_list("name", flat=True))),
|
||||
("rack 2", ["prod"]))
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from io import BytesIO
|
|||
from unittest.mock import patch
|
||||
|
||||
from accounts.models import UserInstance
|
||||
from computes.models import Compute
|
||||
from computes.models import Compute, Tag
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.messages import get_messages
|
||||
|
|
@ -35,6 +35,7 @@ class VmReportTests(TestCase):
|
|||
self.c1 = Compute.objects.create(name="host-one", hostname="192.0.2.1", login="u", password="p", type=1)
|
||||
self.c2 = Compute.objects.create(name="host-two", hostname="192.0.2.2", login="u", password="p", type=1)
|
||||
self.a = Instance.objects.create(compute=self.c1, name="vm-a", uuid=UUID_A)
|
||||
self.a.tags.add(Tag.objects.create(name="web"), Tag.objects.create(name="prod"))
|
||||
Instance.objects.create(compute=self.c2, name="vm-b", uuid=UUID_B)
|
||||
UserInstance.objects.create(user=self.owner, instance=self.a)
|
||||
|
||||
|
|
@ -71,9 +72,9 @@ class VmReportTests(TestCase):
|
|||
self.assertRegex(response["Content-Disposition"], r'attachment; filename="vms-\d{8}-\d{4}\.xlsx"')
|
||||
rows, coverage = self.sheets(response)
|
||||
self.assertEqual(rows[0], ["VM", "State", "Host", "OS", "OS family", "vCPUs", "Memory (MiB)", "IP addresses",
|
||||
"Address sources", "MAC addresses", "Owners", "In the panel"])
|
||||
"Address sources", "MAC addresses", "Owners", "Tags", "In the panel"])
|
||||
self.assertEqual(rows[1], ["vm-a", "Active", "host-one", "AlmaLinux 10.0", "Linux", 2, 2048,
|
||||
"192.0.2.10, 2001:db8::10", "ARP, agent", "52:54:00:00:00:01", "vr-owner", "yes"])
|
||||
"192.0.2.10, 2001:db8::10", "ARP, agent", "52:54:00:00:00:01", "vr-owner", "prod, web", "yes"])
|
||||
self.assertEqual(rows[2][:3] + rows[2][-1:], ["made-with-virsh", "Off", "host-two", "no"])
|
||||
self.assertIn("Hosts not reached: host-three", coverage)
|
||||
|
||||
|
|
|
|||
|
|
@ -238,9 +238,10 @@ def vm_report(request):
|
|||
owners = {}
|
||||
for owner in UserInstance.objects.select_related("user", "instance"):
|
||||
owners.setdefault((owner.instance.compute_id, owner.instance.uuid), []).append(owner.user.username)
|
||||
tags = {(vm.compute_id, vm.uuid): [tag.name for tag in vm.tags.all()] for vm in Instance.objects.prefetch_related("tags")}
|
||||
# in the reader's language
|
||||
header = [_("VM"), _("State"), _("Host"), _("OS"), _("OS family"), _("vCPUs"), _("Memory (MiB)"), _("IP addresses"),
|
||||
_("Address sources"), _("MAC addresses"), _("Owners"), _("In the panel")]
|
||||
_("Address sources"), _("MAC addresses"), _("Owners"), _("Tags"), _("In the panel")]
|
||||
rows = []
|
||||
for vm in result["vms"]:
|
||||
key = (vm["compute_id"], vm["uuid"])
|
||||
|
|
@ -251,7 +252,8 @@ def vm_report(request):
|
|||
rows.append([vm["name"], str(state_label(vm["state"])[0]), hosts[vm["compute_id"]], vm["os"], vm["os_family"],
|
||||
vm["vcpus"], vm["memory"], ", ".join(address for address, _sources in reported),
|
||||
", ".join(s for s in addresses.SOURCES.values() if s in found), ", ".join(vm["macs"]),
|
||||
", ".join(sorted(owners.get(key, []))), _("yes") if key in known else _("no")])
|
||||
", ".join(sorted(owners.get(key, []))), ", ".join(tags.get(key, [])),
|
||||
_("yes") if key in known else _("no")])
|
||||
|
||||
notes = [_("Created: %(time)s") % {"time": f"{timezone.localtime():%Y-%m-%d %H:%M}"},
|
||||
_("VMs listed: %(n)s") % {"n": len(rows)},
|
||||
|
|
@ -2407,8 +2409,9 @@ def set_tags(request, pk):
|
|||
names = form.cleaned_data["tags"]
|
||||
with audit_vm(request, instance, f"tags {', '.join(sorted(names)) or 'none'}") as entry, transaction.atomic():
|
||||
lock_ownership() # not while the VM's tags are kept or given back (computes.utils.forget)
|
||||
entry.skip = moved = _moved_meanwhile(pk, instance) # its host's list may have dropped it meanwhile
|
||||
if not moved:
|
||||
moved = _moved_meanwhile(pk, instance) # its host's list may have dropped it meanwhile
|
||||
entry.skip = moved or set(names) == set(instance.tags.values_list("name", flat=True))
|
||||
if not entry.skip:
|
||||
instance.tags.set(Tag.named(names))
|
||||
return _moved(request, pk) if moved else _back(request, pk, "options")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue