1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-11-01 12:04:15 +00:00
webvirtcloud/interfaces/views.py

98 lines
3.2 KiB
Python
Raw Normal View History

2015-02-27 08:53:51 +00:00
from django.shortcuts import render
2015-02-27 09:28:22 +00:00
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from computes.models import Compute
from interfaces.forms import AddInterface
from vrtManager.interface import wvmInterface, wvmInterfaces
from libvirt import libvirtError
2015-02-27 08:53:51 +00:00
2015-02-27 09:28:22 +00:00
def interfaces(request, compute_id):
"""
:param request:
:return:
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
ifaces_all = []
2015-02-27 09:51:33 +00:00
error_messages = []
2015-02-27 09:28:22 +00:00
compute = Compute.objects.get(id=compute_id)
try:
conn = wvmInterfaces(compute.hostname,
compute.login,
compute.password,
compute.type)
ifaces = conn.get_ifaces()
try:
netdevs = conn.get_net_device()
except:
netdevs = ['eth0', 'eth1']
for iface in ifaces:
ifaces_all.append(conn.get_iface_info(iface))
if request.method == 'POST':
if 'create' in request.POST:
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'])
return HttpResponseRedirect(request.get_full_path())
conn.close()
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
2015-02-27 09:28:22 +00:00
return render(request, 'interfaces.html', locals())
def interface(request, compute_id, iface):
"""
:param request:
:return:
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
ifaces_all = []
2015-02-27 09:51:33 +00:00
error_messages = []
2015-02-27 09:28:22 +00:00
compute = Compute.objects.get(id=compute_id)
try:
conn = wvmInterface(compute.hostname,
compute.login,
compute.password,
compute.type,
iface)
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()
if request.method == 'POST':
if 'stop' in request.POST:
conn.stop_iface()
return HttpResponseRedirect(request.get_full_path())
if 'start' in request.POST:
conn.start_iface()
return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST:
conn.delete_iface()
2015-02-27 09:51:33 +00:00
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:
error_messages.append(lib_err)
2015-02-27 09:28:22 +00:00
return render(request, 'interface.html', locals())