diff --git a/instances/api/viewsets.py b/instances/api/viewsets.py index 328a456e..ac46a1d9 100644 --- a/instances/api/viewsets.py +++ b/instances/api/viewsets.py @@ -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" diff --git a/instances/tests/test_api_list_refresh.py b/instances/tests/test_api_list_refresh.py new file mode 100644 index 00000000..fd165276 --- /dev/null +++ b/instances/tests/test_api_list_refresh.py @@ -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]