mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 17:06:09 +00:00
fix(instances): the REST API's list of all VMs reads the hosts first
/api/v1/instances/ listed the panel's VM records without asking the hosts: a VM undefined or renamed with virsh stayed there as it was, and one defined outside never appeared, until someone opened a web VM list (the per-host API list already read its host). It now reads the hosts it lists first, as the VM list does: all at once, one that does not answer left as last read. Seen live: a VM defined with virsh appears, and is gone once undefined.
This commit is contained in:
parent
cdc7468af3
commit
3dfdd529b2
2 changed files with 47 additions and 0 deletions
|
|
@ -8,7 +8,9 @@ from django.urls import reverse
|
|||
from django.utils.translation import gettext as _
|
||||
from instances.jobs import MIGRATE_OPTIONS
|
||||
from instances.models import Flavor, Instance
|
||||
from instances.utils import host_domain_states
|
||||
from instances.views import (
|
||||
_listed_computes,
|
||||
force_off,
|
||||
get_instance,
|
||||
powercycle,
|
||||
|
|
@ -56,6 +58,12 @@ class InstancesViewSet(viewsets.ViewSet):
|
|||
lookup_value_converter = "int"
|
||||
|
||||
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
|
||||
computes = _listed_computes(request.user, list(Compute.objects.all()))
|
||||
host_domain_states(computes)
|
||||
for compute in computes:
|
||||
utils.refresh_instance_database(compute)
|
||||
|
||||
if request.user.is_superuser or request.user.has_perm(
|
||||
"instances.view_instances"
|
||||
|
|
|
|||
39
instances/tests/test_api_list_refresh.py
Normal file
39
instances/tests/test_api_list_refresh.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
"""The REST API's list of all VMs reads the hosts first, as the VM list
|
||||
does: a VM removed, renamed or defined outside the panel is seen there too,
|
||||
not only after someone opens a web list."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from accounts.models import UserInstance
|
||||
from computes.models import Compute
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from instances.models import Instance
|
||||
|
||||
|
||||
class ApiListRefreshTests(TestCase):
|
||||
def setUp(self):
|
||||
self.mine, self.other = (Compute.objects.create(name=name, hostname=f"192.0.2.{n}", login="", password="", type=1)
|
||||
for n, name in ((1, "mine"), (2, "other")))
|
||||
self.owner = get_user_model().objects.create_user("api-owner")
|
||||
vm = Instance.objects.create(compute=self.mine, name="vm", uuid="11111111-1111-1111-1111-111111111111")
|
||||
UserInstance.objects.create(user=self.owner, instance=vm)
|
||||
|
||||
def listed_hosts(self, user):
|
||||
self.client.force_login(user)
|
||||
with patch("instances.api.viewsets.host_domain_states") as states, \
|
||||
patch("instances.api.viewsets.utils.refresh_instance_database") as refresh:
|
||||
self.assertEqual(self.client.get("/api/v1/instances/").status_code, 200)
|
||||
self.assertEqual([c.name for c in states.call_args.args[0]], [c.name for c in refresh_args(refresh)])
|
||||
return sorted(c.name for c in refresh_args(refresh))
|
||||
|
||||
def test_the_hosts_are_read_first(self):
|
||||
admin = get_user_model().objects.create_superuser("api-admin", "a@example.com", "pw")
|
||||
self.assertEqual(self.listed_hosts(admin), ["mine", "other"])
|
||||
|
||||
def test_an_owner_reads_only_their_vms_hosts(self):
|
||||
self.assertEqual(self.listed_hosts(self.owner), ["mine"])
|
||||
|
||||
|
||||
def refresh_args(refresh):
|
||||
return [call.args[0] for call in refresh.call_args_list]
|
||||
Loading…
Add table
Add a link
Reference in a new issue