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

Refactoring

This commit is contained in:
Retspen 2015-02-27 11:51:33 +02:00
parent dd5f98cbe8
commit fa3df5bff3
6 changed files with 73 additions and 72 deletions

View file

@ -28,8 +28,9 @@ def instances(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
computes = Compute.objects.filter()
error_messages = []
all_host_vms = {}
computes = Compute.objects.filter()
for compute in computes:
if connection_manager.host_is_up(compute.type, compute.hostname):
@ -37,8 +38,8 @@ def instances(request):
conn = wvmHostDetails(compute, compute.login, compute.password, compute.type)
all_host_vms[compute.id, compute.name] = conn.get_host_instances()
conn.close()
except libvirtError:
pass
except libvirtError as lib_err:
error_messages.append(lib_err)
return render(request, 'instances.html', locals())

View file

@ -16,8 +16,8 @@ def interfaces(request, compute_id):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
ifaces_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -45,8 +45,8 @@ def interfaces(request, compute_id):
data['stp'], data['delay'])
return HttpResponseRedirect(request.get_full_path())
conn.close()
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
return render(request, 'interfaces.html', locals())
@ -60,8 +60,8 @@ def interface(request, compute_id, iface):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
ifaces_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -89,9 +89,9 @@ def interface(request, compute_id, iface):
return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST:
conn.delete_iface()
return HttpResponseRedirect(reverse('interfaces', args=[host_id]))
return HttpResponseRedirect(reverse('interfaces', args=[compute_id]))
conn.close()
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
return render(request, 'interface.html', locals())

View file

@ -18,7 +18,7 @@ def networks(request, compute_id):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -35,23 +35,23 @@ def networks(request, compute_id):
data = form.cleaned_data
if data['name'] in networks:
msg = _("Pool name already in use")
errors.append(msg)
error_messages.append(msg)
if data['forward'] == 'bridge' and data['bridge_name'] == '':
errors.append('Please enter bridge name')
error_messages.append('Please enter bridge name')
try:
gateway, netmask, dhcp = network_size(data['subnet'], data['dhcp'])
except:
msg = _("Input subnet pool error")
errors.append(msg)
if not errors:
error_msg = _("Input subnet pool error")
error_messages.append(error_msg)
if not error_messages:
conn.create_network(data['name'], data['forward'], gateway, netmask,
dhcp, data['bridge_name'], data['openvswitch'], data['fixed'])
return HttpResponseRedirect(reverse('network', args=[host_id, data['name']]))
return HttpResponseRedirect(reverse('network', args=[compute_id, data['name']]))
conn.close()
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
return render(request, 'network.html', locals())
return render(request, 'networks.html', locals())
def network(request, compute_id, pool):
@ -63,7 +63,7 @@ def network(request, compute_id, pool):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -81,40 +81,40 @@ def network(request, compute_id, pool):
ipv4_dhcp_range_end = conn.get_ipv4_dhcp_range_end()
ipv4_network = conn.get_ipv4_network()
fixed_address = conn.get_mac_ipaddr()
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
if request.method == 'POST':
if 'start' in request.POST:
try:
conn.start()
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'stop' in request.POST:
try:
conn.stop()
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'delete' in request.POST:
try:
conn.delete()
return HttpResponseRedirect(reverse('networks', args=[host_id]))
except libvirtError as error_msg:
errors.append(error_msg.message)
return HttpResponseRedirect(reverse('networks', args=[compute_id]))
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'set_autostart' in request.POST:
try:
conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'unset_autostart' in request.POST:
try:
conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
conn.close()

View file

@ -16,8 +16,8 @@ def secrets(request, compute_id):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
secrets_all = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -30,8 +30,8 @@ def secrets(request, compute_id):
secrt = conn.get_secret(uuid)
try:
secret_value = conn.get_secret_value(uuid)
except:
secret_value = ''
except libvirtError as lib_err:
secret_value = None
secrets_all.append({'usage': secrt.usageID(),
'uuid': secrt.UUIDString(),
'usageType': secrt.usageType(),
@ -54,6 +54,6 @@ def secrets(request, compute_id):
conn.set_secret_value(uuid, value)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
errors.append(err)
error_messages.append(err)
return render(request, 'secrets.html', locals())

View file

@ -17,7 +17,7 @@ def storages(request, compute_id):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
errors = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
try:
@ -35,15 +35,15 @@ def storages(request, compute_id):
data = form.cleaned_data
if data['name'] in storages:
msg = _("Pool name already use")
errors.append(msg)
error_messages.append(msg)
if data['stg_type'] == 'rbd':
if not data['secret']:
msg = _("You need create secret for pool")
errors.append(msg)
error_messages.append(msg)
if not data['ceph_pool'] and not data['ceph_host'] and not data['ceph_user']:
msg = _("You need input all fields for creating ceph pool")
errors.append(msg)
if not errors:
error_messages.append(msg)
if not error_messages:
if data['stg_type'] == 'rbd':
conn.create_storage_ceph(data['stg_type'], data['name'],
data['ceph_pool'], data['ceph_host'],
@ -54,10 +54,10 @@ def storages(request, compute_id):
data['source_format'], data['target'])
else:
conn.create_storage(data['stg_type'], data['name'], data['source'], data['target'])
return HttpResponseRedirect(reverse('storage', args=[host_id, data['name']]))
return HttpResponseRedirect(reverse('storage', args=[compute_id, data['name']]))
conn.close()
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
return render(request, 'storages.html', locals())
@ -78,7 +78,7 @@ def storage(request, compute_id, pool):
destination.write(chunk)
destination.close()
errors = []
error_messages = []
compute = Compute.objects.get(id=compute_id)
meta_prealloc = False
@ -107,40 +107,40 @@ def storage(request, compute_id, pool):
volumes = conn.update_volumes()
else:
volumes = None
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
if request.method == 'POST':
if 'start' in request.POST:
try:
conn.start()
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'stop' in request.POST:
try:
conn.stop()
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'delete' in request.POST:
try:
conn.delete()
return HttpResponseRedirect(reverse('storages', args=[host_id]))
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'set_autostart' in request.POST:
try:
conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'unset_autostart' in request.POST:
try:
conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'add_volume' in request.POST:
form = AddImage(request.POST)
if form.is_valid():
@ -150,20 +150,20 @@ def storage(request, compute_id, pool):
try:
conn.create_volume(data['name'], data['size'], data['format'], meta_prealloc)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
if 'del_volume' in request.POST:
volname = request.POST.get('volname', '')
try:
vol = conn.get_volume(volname)
vol.delete(0)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as error_msg:
errors.append(error_msg.message)
except libvirtError as lib_err:
error_messages.append(lib_err.message)
if 'iso_upload' in request.POST:
if str(request.FILES['file']) in conn.update_volumes():
msg = _("ISO image already exist")
errors.append(msg)
error_msg = _("ISO image already exist")
error_messages.append(error_msg)
else:
handle_uploaded_file(path, request.FILES['file'])
return HttpResponseRedirect(request.get_full_path())
@ -175,8 +175,8 @@ def storage(request, compute_id, pool):
meta_prealloc = 0
if img_name in conn.update_volumes():
msg = _("Name of volume name already use")
errors.append(msg)
if not errors:
error_messages.append(msg)
if not error_messages:
if data['convert']:
format = data['format']
if data['meta_prealloc'] and data['format'] == 'qcow2':
@ -186,8 +186,8 @@ def storage(request, compute_id, pool):
try:
conn.clone_volume(data['image'], data['name'], format, meta_prealloc)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
errors.append(err)
except libvirtError as lib_err:
error_messages.append(lib_err)
conn.close()
return render(request, 'storage.html', locals())

View file

@ -1,8 +1,8 @@
{% if errors %}
{% for error in errors %}
{% if error_messages %}
{% for error in error_messages %}
<div class="alert alert-danger alert-dismissible" role="danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Error:</strong> Better check yourself, you're not looking too good.
<strong>Error:</strong> {{ error }}
</div>
{% endfor %}
{% endif %}