1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-27 08:35:22 +00:00
webvirtcloud/interfaces/views.py

123 lines
3.7 KiB
Python
Raw Normal View History

2022-11-02 13:05:41 +00:00
import contextlib
2020-10-15 14:18:45 +00:00
from django.contrib import messages
2015-04-02 12:39:40 +00:00
from django.shortcuts import render, get_object_or_404
2015-02-27 09:28:22 +00:00
from django.http import HttpResponseRedirect
from django.urls import reverse
from libvirt import libvirtError
2015-02-27 09:28:22 +00:00
from computes.models import Compute
from interfaces.forms import AddInterface
from vrtManager.interface import wvmInterface, wvmInterfaces
from admin.decorators import superuser_only
2015-02-27 08:53:51 +00:00
2015-02-27 09:28:22 +00:00
@superuser_only
2015-02-27 09:28:22 +00:00
def interfaces(request, compute_id):
"""
:param request:
2019-09-10 06:48:31 +00:00
:param compute_id:
2015-02-27 09:28:22 +00:00
:return:
"""
ifaces_all = []
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
2015-02-27 09:28:22 +00:00
try:
2022-11-02 05:54:35 +00:00
conn = wvmInterfaces(
compute.hostname,
compute.login,
compute.password,
compute.type
)
2015-02-27 09:28:22 +00:00
ifaces = conn.get_ifaces()
2022-11-02 13:05:41 +00:00
netdevs = ["eth0", "eth1"]
with contextlib.suppress(Exception):
netdevs = conn.get_net_devices()
2015-02-27 09:28:22 +00:00
for iface in ifaces:
2022-11-02 05:54:35 +00:00
interf = wvmInterface(
compute.hostname,
compute.login,
compute.password,
compute.type,
iface
)
ifaces_all.append(interf.get_details())
2015-02-27 09:28:22 +00:00
if request.method == "POST":
if "create" in request.POST:
2015-02-27 09:28:22 +00:00
form = AddInterface(request.POST)
if form.is_valid():
data = form.cleaned_data
conn.create_iface(
data["name"],
data["itype"],
data["start_mode"],
data["netdev"],
data["ipv4_type"],
data["ipv4_addr"],
data["ipv4_gw"],
data["ipv6_type"],
data["ipv6_addr"],
data["ipv6_gw"],
data["stp"],
data["delay"],
)
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
2020-10-15 14:18:45 +00:00
messages.error(request, msg_err.as_text())
2015-02-27 09:28:22 +00:00
conn.close()
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
2020-10-15 14:18:45 +00:00
messages.error(request, lib_err)
2015-02-27 09:28:22 +00:00
return render(request, "interfaces.html", locals())
2015-02-27 09:28:22 +00:00
@superuser_only
2015-02-27 09:28:22 +00:00
def interface(request, compute_id, iface):
"""
:param request:
:param compute_id:
2019-09-10 06:48:31 +00:00
:param iface:
2015-02-27 09:28:22 +00:00
:return:
"""
ifaces_all = []
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
2015-02-27 09:28:22 +00:00
try:
2022-11-02 05:54:35 +00:00
conn = wvmInterface(
compute.hostname,
compute.login,
compute.password,
compute.type,
iface
)
2015-02-27 09:28:22 +00:00
start_mode = conn.get_start_mode()
state = conn.is_active()
mac = conn.get_mac()
itype = conn.get_type()
ipv4 = conn.get_ipv4()
ipv4_type = conn.get_ipv4_type()
ipv6 = conn.get_ipv6()
ipv6_type = conn.get_ipv6_type()
bridge = conn.get_bridge()
slave_ifaces = conn.get_bridge_slave_ifaces()
2015-02-27 09:28:22 +00:00
if request.method == "POST":
if "stop" in request.POST:
2015-02-27 09:28:22 +00:00
conn.stop_iface()
return HttpResponseRedirect(request.get_full_path())
if "start" in request.POST:
2015-02-27 09:28:22 +00:00
conn.start_iface()
return HttpResponseRedirect(request.get_full_path())
if "delete" in request.POST:
2015-02-27 09:28:22 +00:00
conn.delete_iface()
return HttpResponseRedirect(reverse("interfaces", args=[compute_id]))
2015-02-27 09:28:22 +00:00
conn.close()
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
2020-10-15 14:18:45 +00:00
messages.error(request, lib_err)
2015-02-27 09:28:22 +00:00
return render(request, "interface.html", locals())