2020-10-15 14:18:45 +00:00
|
|
|
from admin.decorators import superuser_only
|
|
|
|
from computes.models import Compute
|
|
|
|
from django.contrib import messages
|
2018-09-21 13:50:44 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-09-21 13:50:44 +00:00
|
|
|
from libvirt import libvirtError
|
2018-09-21 15:46:33 +00:00
|
|
|
from logs.views import addlogmsg
|
2020-05-27 12:24:06 +00:00
|
|
|
from vrtManager import util
|
|
|
|
from vrtManager.instance import wvmInstance, wvmInstances
|
|
|
|
from vrtManager.nwfilters import wvmNWFilter, wvmNWFilters
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2018-09-21 13:50:44 +00:00
|
|
|
def nwfilters(request, compute_id):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-10-30 08:05:50 +00:00
|
|
|
:param compute_id:
|
2018-09-21 13:50:44 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
nwfilters_all = []
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
|
|
|
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmNWFilters(compute.hostname, compute.login, compute.password, compute.type)
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'create_nwfilter' in request.POST:
|
|
|
|
xml = request.POST.get('nwfilter_xml', '')
|
|
|
|
if xml:
|
|
|
|
try:
|
|
|
|
util.etree.fromstring(xml)
|
|
|
|
name = util.get_xml_path(xml, '/filter/@name')
|
|
|
|
uuid = util.get_xml_path(xml, '/filter/uuid')
|
|
|
|
except util.etree.ParseError:
|
|
|
|
name = None
|
|
|
|
|
|
|
|
for nwf in nwfilters:
|
|
|
|
if name == nwf.name():
|
|
|
|
error_msg = _("A network filter with this name already exists")
|
|
|
|
raise Exception(error_msg)
|
|
|
|
if uuid == nwf.UUIDString():
|
2020-06-12 11:46:17 +00:00
|
|
|
error_msg = _("A network filter with this UUID already exists")
|
2018-09-21 13:50:44 +00:00
|
|
|
raise Exception(error_msg)
|
|
|
|
else:
|
|
|
|
try:
|
2018-09-21 15:46:33 +00:00
|
|
|
msg = _("Creating NWFilter: %s" % name)
|
2018-09-21 13:50:44 +00:00
|
|
|
conn.create_nwfilter(xml)
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, msg)
|
2018-09-21 13:50:44 +00:00
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2020-03-16 13:59:45 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, lib_err)
|
2018-09-21 15:46:33 +00:00
|
|
|
|
2018-09-21 13:50:44 +00:00
|
|
|
if 'del_nwfilter' in request.POST:
|
2019-10-30 08:05:50 +00:00
|
|
|
name = request.POST.get('nwfiltername', '')
|
2020-05-21 22:13:39 +00:00
|
|
|
msg = _(f"Deleting NWFilter: {name}")
|
2018-09-21 15:46:33 +00:00
|
|
|
in_use = False
|
2018-09-21 13:50:44 +00:00
|
|
|
nwfilter = conn.get_nwfilter(name)
|
2018-09-21 15:46:33 +00:00
|
|
|
|
|
|
|
is_conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type)
|
|
|
|
instances = is_conn.get_instances()
|
|
|
|
for inst in instances:
|
|
|
|
i_conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, inst)
|
|
|
|
dom_filterrefs = i_conn.get_filterrefs()
|
|
|
|
|
|
|
|
if name in dom_filterrefs:
|
|
|
|
in_use = True
|
2020-05-21 22:13:39 +00:00
|
|
|
msg = _(f"NWFilter is in use by {inst}. Cannot be deleted.")
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, msg)
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, msg)
|
|
|
|
i_conn.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
is_conn.close()
|
|
|
|
if nwfilter and not in_use:
|
2018-09-21 13:50:44 +00:00
|
|
|
nwfilter.undefine()
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, msg)
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
if 'cln_nwfilter' in request.POST:
|
2018-09-21 15:46:33 +00:00
|
|
|
|
2019-10-30 08:05:50 +00:00
|
|
|
name = request.POST.get('nwfiltername', '')
|
2018-09-21 13:50:44 +00:00
|
|
|
cln_name = request.POST.get('cln_name', name + '-clone')
|
|
|
|
|
2019-10-30 08:05:50 +00:00
|
|
|
conn.clone_nwfilter(name, cln_name)
|
2020-05-19 16:53:54 +00:00
|
|
|
msg = _(f"Cloning NWFilter {name} as {cln_name}")
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, msg)
|
|
|
|
|
|
|
|
for nwf in conn.get_nwfilters():
|
2018-09-24 11:41:13 +00:00
|
|
|
nwfilters_all.append(conn.get_nwfilter_info(nwf))
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
conn.close()
|
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, lib_err)
|
2018-09-21 13:50:44 +00:00
|
|
|
except Exception as err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, err)
|
2018-09-21 15:46:33 +00:00
|
|
|
addlogmsg(request.user.username, compute.hostname, err)
|
2018-09-21 13:50:44 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
return render(request, 'nwfilters.html', {
|
|
|
|
'nwfilters': nwfilters_all,
|
2020-10-15 14:18:45 +00:00
|
|
|
'compute': compute,
|
2020-05-27 12:24:06 +00:00
|
|
|
})
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def nwfilter(request, compute_id, nwfltr):
|
2020-04-24 16:34:29 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:param compute_id:
|
|
|
|
:param nwfltr:
|
|
|
|
:return:
|
|
|
|
"""
|
2018-09-21 13:50:44 +00:00
|
|
|
nwfilters_all = []
|
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
|
|
|
|
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
nwfilter = wvmNWFilter(compute.hostname, compute.login, compute.password, compute.type, nwfltr)
|
|
|
|
conn = wvmNWFilters(compute.hostname, compute.login, compute.password, compute.type)
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
for nwf in conn.get_nwfilters():
|
2018-09-24 11:41:13 +00:00
|
|
|
nwfilters_all.append(conn.get_nwfilter_info(nwf))
|
2018-09-21 13:50:44 +00:00
|
|
|
|
|
|
|
uuid = nwfilter.get_uuid()
|
|
|
|
name = nwfilter.get_name()
|
|
|
|
xml = nwfilter.get_xml()
|
|
|
|
rules = nwfilter.get_rules()
|
|
|
|
refs = nwfilter.get_filter_refs()
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
|
|
|
if 'edit_nwfilter' in request.POST:
|
|
|
|
new_xml = request.POST.get('edit_xml', '')
|
|
|
|
|
|
|
|
if new_xml:
|
|
|
|
nwfilter.delete()
|
|
|
|
try:
|
|
|
|
conn.create_nwfilter(new_xml)
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
conn.create_nwfilter(xml)
|
|
|
|
raise libvirtError(lib_err)
|
|
|
|
|
|
|
|
if 'del_nwfilter_rule' in request.POST:
|
|
|
|
action = request.POST.get('action', '')
|
|
|
|
direction = request.POST.get('direction', '')
|
|
|
|
priority = request.POST.get('priority', '')
|
|
|
|
|
|
|
|
new_xml = nwfilter.delete_rule(action, direction, priority)
|
|
|
|
nwfilter.delete()
|
|
|
|
try:
|
|
|
|
conn.create_nwfilter(new_xml)
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
conn.create_nwfilter(xml)
|
|
|
|
raise libvirtError(lib_err)
|
|
|
|
|
|
|
|
if 'del_nwfilter_ref' in request.POST:
|
|
|
|
ref_name = request.POST.get('ref')
|
|
|
|
new_xml = nwfilter.delete_ref(ref_name)
|
|
|
|
nwfilter.delete()
|
|
|
|
try:
|
|
|
|
conn.create_nwfilter(new_xml)
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
conn.create_nwfilter(xml)
|
|
|
|
raise libvirtError(lib_err)
|
|
|
|
|
|
|
|
if 'add_nwfilter_rule' in request.POST:
|
|
|
|
rule_xml = request.POST.get('nwfilterrule_xml', '')
|
|
|
|
if not rule_xml:
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
new_xml = nwfilter.add_rule(rule_xml)
|
|
|
|
nwfilter.delete()
|
|
|
|
try:
|
|
|
|
conn.create_nwfilter(new_xml)
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
conn.create_nwfilter(xml)
|
|
|
|
raise libvirtError(lib_err)
|
|
|
|
|
|
|
|
if 'add_nwfilter_ref' in request.POST:
|
|
|
|
ref_name = request.POST.get('nwfilters_select', '')
|
|
|
|
if not ref_name:
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
new_xml = nwfilter.add_ref(ref_name)
|
|
|
|
nwfilter.delete()
|
|
|
|
try:
|
|
|
|
conn.create_nwfilter(new_xml)
|
|
|
|
except libvirtError as lib_err:
|
|
|
|
conn.create_nwfilter(xml)
|
|
|
|
raise libvirtError(lib_err)
|
|
|
|
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
conn.close()
|
|
|
|
nwfilter.close()
|
|
|
|
except libvirtError as lib_err:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, lib_err)
|
2018-09-21 13:50:44 +00:00
|
|
|
except Exception as error_msg:
|
2020-10-15 14:18:45 +00:00
|
|
|
messages.error(request, error_msg)
|
2018-09-21 13:50:44 +00:00
|
|
|
|
2019-10-30 08:05:50 +00:00
|
|
|
return render(request, 'nwfilter.html', locals())
|