1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00
webvirtcloud/nwfilters/views.py
catborise 3dbd4c0316 refactor(nwfilters): the filter page names what its template gets
The filter page rendered locals(): every local of the view reached the
template, so which values the page uses could not be read from the view,
and a renamed variable left the page blank without an error. The view
now passes a dict of the names the template reads; a filter that could
not be read shows the host only, as before.
2026-11-14 13:36:06 +03:00

130 lines
6.4 KiB
Python

from admin.decorators import superuser_only
from computes.models import Compute
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from instances.models import Instance
from libvirt import libvirtError
from logs.audit import audit
from operations import claims
from operations.models import Claim
from operations.utils import busy_message
from vrtManager import util
from vrtManager.nwfilters import wvmNWFilter, wvmNWFilters
def one_change(compute):
"""The host's filters for one change: read, checked and defined while no
other change of them runs, which it would undo."""
return claims.hold({claims.nwfilters_key(compute.pk): Claim.EXCLUSIVE})
@superuser_only
def nwfilters(request, compute_id):
"""The host's network filters; one created, cloned or deleted, after
which the list is read again."""
compute = get_object_or_404(Compute, pk=compute_id)
nwfilters_all = None # not read: the page shows no list
try:
conn = wvmNWFilters(compute.hostname, compute.login, compute.password, compute.type)
if request.method == "POST":
name = request.POST.get("nwfiltername", "")
with one_change(compute):
if "create_nwfilter" in request.POST:
xml = request.POST.get("nwfilter_xml", "")
try: # the row names the filter where the XML does
new_name = util.parse_xml(xml).get("name")
except util.OperationError:
new_name = None
with audit(request, compute, f"Network filter {new_name or '(no name)'}", "add"):
conn.create_nwfilter(xml)
elif "cln_nwfilter" in request.POST:
clone_name = request.POST.get("cln_name", "")
with audit(request, compute, f"Network filter {name}", f"clone as {clone_name}"):
conn.clone_nwfilter(name, clone_name)
elif "del_nwfilter" in request.POST:
with audit(request, compute, f"Network filter {name}", "delete"):
conn.delete_nwfilter(name)
return HttpResponseRedirect(request.get_full_path())
# the VMs first: a list without them would offer to delete filters in use
users = conn.get_users()
nwfilters_all = conn.get_nwfilters_info()
for nwf in nwfilters_all:
nwf["vms"] = users.get(nwf["name"], [])
except libvirtError as lib_err: # OperationError too: a name in use, a filter in use
messages.error(request, lib_err)
if request.method == "POST": # a host not reached as well: nothing was changed
return HttpResponseRedirect(request.get_full_path())
except claims.Busy as busy: # another change of the host's filters runs
messages.error(request, busy_message(busy, request.user))
return HttpResponseRedirect(request.get_full_path())
return render(request, "nwfilters.html", {"nwfilters": nwfilters_all, "compute": compute})
@superuser_only
def nwfilter(request, compute_id, nwfltr):
"""A network filter of the host: its rules and references changed one at
a time, or its XML as a whole."""
compute = get_object_or_404(Compute, pk=compute_id)
context = {"compute": compute} # a filter not read shows only the host
try:
nwfilter = wvmNWFilter(compute.hostname, compute.login, compute.password, compute.type, nwfltr)
if request.method == "POST":
post = request.POST
subject = f"Network filter {nwfltr}"
try:
with one_change(compute):
if "edit_nwfilter" in post:
with audit(request, compute, subject, "change XML"):
nwfilter.edit(post.get("edit_xml", ""), post.get("seen", ""))
elif "add_nwfilter_rule" in post:
with audit(request, compute, subject, "add rule"):
nwfilter.add_rule(post.get("nwfilterrule_xml", ""))
elif "del_nwfilter_rule" in post:
with audit(request, compute, subject, "delete rule"):
nwfilter.delete_rule(post.get("index", ""), post.get("rule", ""))
elif "add_nwfilter_ref" in post:
ref_name = post.get("nwfilters_select", "")
with audit(request, compute, subject, f"add reference {ref_name}"):
nwfilter.add_ref(ref_name)
elif "del_nwfilter_ref" in post:
with audit(request, compute, subject, "delete reference") as entry:
entry.action = f"delete reference {nwfilter.delete_ref(post.get('index', ''), post.get('ref', ''))}"
except libvirtError as err: # OperationError too: a filter changed meanwhile, XML refused
messages.error(request, err)
except claims.Busy as busy: # another change of the host's filters runs
messages.error(request, busy_message(busy, request.user))
return HttpResponseRedirect(request.get_full_path())
name = nwfilter.get_name()
xml = nwfilter.get_xml()
filters = nwfilter.get_nwfilters_info()
vm_names = nwfilter.get_users().get(name, [])
vm_ids = dict(Instance.objects.filter(compute=compute, name__in=vm_names).values_list("name", "id"))
context.update({
"name": name,
"uuid": nwfilter.get_uuid(),
"xml": xml,
"seen": util.digest(xml),
"chain": nwfilter.tree.get("chain"),
"priority": nwfilter.tree.get("priority"),
"rules": nwfilter.get_rules(),
"refs": nwfilter.get_refs(),
"ref_choices": nwfilter.get_ref_choices(),
"filter_names": [nwf["name"] for nwf in filters],
"referenced_by": [nwf["name"] for nwf in filters if name in nwf["refs"]],
"vms": [{"name": vm_name, "id": vm_ids.get(vm_name)} for vm_name in vm_names],
})
except libvirtError as lib_err:
messages.error(request, lib_err)
if request.method == "POST": # a filter gone meanwhile, a host not reached: nothing was changed
return HttpResponseRedirect(reverse("nwfilters", args=[compute_id]))
return render(request, "nwfilter.html", context)