From 012eba0c80298b3499b19098b41cfc1beaa6c2d8 Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 17 Nov 2026 23:07:45 +0300 Subject: [PATCH] fix(instances): a host in maintenance offers no new VM from its VM list The host's VM list offered New Instance while the host was in maintenance; the create pages then refused it and sent the user to every VM's list. The button is disabled there, saying why, and a refused create page goes back to the page it was asked from, else the host's VM list. --- computes/templates/computes/instances.html | 7 +++++++ computes/tests/test_maintenance.py | 19 +++++++++++++++++-- instances/views.py | 5 +++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/computes/templates/computes/instances.html b/computes/templates/computes/instances.html index d0819713..f312de65 100644 --- a/computes/templates/computes/instances.html +++ b/computes/templates/computes/instances.html @@ -7,10 +7,17 @@ {% block page_heading %}{{ compute.name }} - {% trans "Instances" %}{% endblock page_heading %} {% block page_heading_extra %} +{% if compute.maintenance %} +{# a disabled button shows no title: its wrapper says why #} + + + +{% else %} {% bs_icon 'plus-circle-fill' %} +{% endif %} {% if instances %} {% trans "Bulk Migrate" %} {% include 'search_block.html' %} diff --git a/computes/tests/test_maintenance.py b/computes/tests/test_maintenance.py index 75efeff9..a04d2ec9 100644 --- a/computes/tests/test_maintenance.py +++ b/computes/tests/test_maintenance.py @@ -55,12 +55,27 @@ class MaintenanceSwitchTests(TestCase): def test_a_vm_cannot_be_created_from_its_pages(self): self.compute.maintenance_since = timezone.now() self.compute.save() + host_vms = reverse("instances", args=[self.compute.pk]) for url in (reverse("instances:create_instance_select_type", args=[self.compute.pk]), reverse("instances:create_instance", args=[self.compute.pk, "x86_64", "q35"])): - response = self.client.get(url, follow=True) - self.assertRedirects(response, reverse("instances:index")) + # back to the page it was asked from (the host's VM list), not every VM's + response = self.client.get(url, HTTP_REFERER=f"http://testserver{host_vms}") + self.assertEqual(response.url, f"http://testserver{host_vms}") + response = self.client.get(url, follow=True) # typed in: the host's VM list too + self.assertRedirects(response, host_vms) self.assertContains(response, f"mt {REFUSED}") + def test_the_host_vm_list_offers_no_new_vm(self): + from lxml import html as lxml_html + + page = lambda: lxml_html.fromstring(self.client.get(reverse("instances", args=[self.compute.pk])).content) + create = reverse("instances:create_instance_select_type", args=[self.compute.pk]) + self.assertTrue(page().xpath("//a[@href=$u]", u=create)) + self.compute.maintenance_since = timezone.now() + self.compute.save() + self.assertFalse(page().xpath("//a[@href=$u]", u=create)) + self.assertTrue(page().xpath("//button[@disabled][contains(@aria-label, 'New Instance')]")) + class MaintenanceGuardTests(TestCase): def setUp(self): diff --git a/instances/views.py b/instances/views.py index bf217942..a600fc60 100755 --- a/instances/views.py +++ b/instances/views.py @@ -2555,12 +2555,13 @@ def getvvfile(request, pk): def in_maintenance(request, compute): - """Back to the VM list, saying why, when the host a VM is made on is in maintenance.""" + """Back to the page it was asked from (else the host's VM list), saying + why, when the host a VM is made on is in maintenance.""" try: compute.refuse_new_vms() except util.OperationError as err: messages.error(request, str(err)) - return redirect("instances:index") + return redirect(same_origin_referer(request, reverse("instances", args=[compute.pk]))) return None