1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 23:55:24 +00:00
webvirtcloud/networks/views.py

176 lines
7.1 KiB
Python
Raw Normal View History

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.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
2015-02-27 09:28:22 +00:00
from computes.models import Compute
from networks.forms import AddNetPool
from vrtManager.network import wvmNetwork, wvmNetworks
from vrtManager.network import network_size
from libvirt import libvirtError
from django.contrib import messages
2015-02-27 08:53:51 +00:00
2015-02-27 09:28:22 +00:00
@login_required
2015-02-27 09:28:22 +00:00
def networks(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:
"""
2015-03-03 14:53:10 +00:00
if not request.user.is_superuser:
2015-03-03 09:34:09 +00:00
return HttpResponseRedirect(reverse('index'))
2015-02-27 09:51:33 +00:00
error_messages = []
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:
conn = wvmNetworks(compute.hostname,
compute.login,
compute.password,
compute.type)
networks = conn.get_networks_info()
if request.method == 'POST':
if 'create' in request.POST:
form = AddNetPool(request.POST)
if form.is_valid():
data = form.cleaned_data
if data['name'] in networks:
msg = _("Pool name already in use")
2015-02-27 09:51:33 +00:00
error_messages.append(msg)
2015-02-27 09:28:22 +00:00
if data['forward'] == 'bridge' and data['bridge_name'] == '':
2015-02-27 09:51:33 +00:00
error_messages.append('Please enter bridge name')
2015-02-27 09:28:22 +00:00
try:
gateway, netmask, dhcp = network_size(data['subnet'], data['dhcp'])
except:
2015-02-27 09:51:33 +00:00
error_msg = _("Input subnet pool error")
error_messages.append(error_msg)
if not error_messages:
2015-02-27 09:28:22 +00:00
conn.create_network(data['name'], data['forward'], gateway, netmask,
dhcp, data['bridge_name'], data['openvswitch'], data['fixed'])
2015-02-27 09:51:33 +00:00
return HttpResponseRedirect(reverse('network', args=[compute_id, data['name']]))
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
error_messages.append(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:
error_messages.append(lib_err)
2015-02-27 09:28:22 +00:00
2015-04-02 13:20:46 +00:00
return render(request, 'networks.html', locals())
2015-02-27 09:28:22 +00:00
@login_required
2015-02-27 09:28:22 +00:00
def network(request, compute_id, pool):
"""
:param request:
2019-09-10 06:48:31 +00:00
:param compute_id:
:param pool:
2015-02-27 09:28:22 +00:00
:return:
"""
2015-03-03 09:34:09 +00:00
if not request.user.is_superuser:
return HttpResponseRedirect(reverse('index'))
2015-02-27 09:51:33 +00:00
error_messages = []
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:
conn = wvmNetwork(compute.hostname,
compute.login,
compute.password,
compute.type,
pool)
networks = conn.get_networks()
state = conn.is_active()
device = conn.get_bridge_device()
autostart = conn.get_autostart()
ipv4_forward = conn.get_ipv4_forward()
ipv4_dhcp_range_start = conn.get_ipv4_dhcp_range_start()
ipv4_dhcp_range_end = conn.get_ipv4_dhcp_range_end()
ipv4_network = conn.get_ipv4_network()
fixed_address = conn.get_mac_ipaddr()
xml = conn._XMLDesc(0)
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err)
return HttpResponseRedirect(reverse('networks', args=compute_id))
2015-02-27 09:28:22 +00:00
if request.method == 'POST':
if 'start' in request.POST:
try:
conn.start()
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'stop' in request.POST:
try:
conn.stop()
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'delete' in request.POST:
try:
conn.delete()
2015-02-27 09:51:33 +00:00
return HttpResponseRedirect(reverse('networks', args=[compute_id]))
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'set_autostart' in request.POST:
try:
conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
if 'unset_autostart' in request.POST:
try:
conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path())
2015-02-27 09:51:33 +00:00
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'modify_fixed_address' in request.POST:
name = request.POST.get('name', '')
address = request.POST.get('address', '')
mac = request.POST.get('mac', '')
try:
ret_val = conn.modify_fixed_address(name, address, mac)
messages.success(request, "Fixed Address Operation Completed.")
return HttpResponseRedirect(request.get_full_path())
except libvirtError as lib_err:
error_messages.append(lib_err.message)
except ValueError as val_err:
error_messages.append(val_err.message)
if 'delete_fixed_address' in request.POST:
mac = request.POST.get('mac', '')
conn.delete_fixed_address(mac)
messages.success(request, "Fixed Address is Deleted.")
return HttpResponseRedirect(request.get_full_path())
if 'modify_dhcp_range' in request.POST:
range_start = request.POST.get('range_start', '')
range_end = request.POST.get('range_end', '')
try:
conn.modify_dhcp_range(range_start, range_end)
messages.success(request, "DHCP Range is Changed.")
return HttpResponseRedirect(request.get_full_path())
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'edit_network' in request.POST:
edit_xml = request.POST.get('edit_xml', '')
if edit_xml:
try:
new_conn = wvmNetworks(compute.hostname,
2019-09-10 06:48:31 +00:00
compute.login,
compute.password,
compute.type)
conn.define_network(edit_xml)
if conn.is_active():
messages.success(request, _("Network XML is changed. Stop and start network to activate new config."))
else:
messages.success(request, _("Network XML is changed."))
return HttpResponseRedirect(request.get_full_path())
except libvirtError as lib_err:
error_messages.append(lib_err.message)
2015-02-27 09:28:22 +00:00
conn.close()
2015-04-02 13:20:46 +00:00
return render(request, 'network.html', locals())