Commit ab541167 by Őry Máté

dashboard: prepare non-customized vm creation

parent 44bd096f
...@@ -5,7 +5,9 @@ import re ...@@ -5,7 +5,9 @@ import re
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.contrib.messages import warning from django.contrib.messages import warning
from django.core.exceptions import PermissionDenied, SuspiciousOperation from django.core.exceptions import (
PermissionDenied, SuspiciousOperation, ObjectDoesNotExist,
)
from django.core import signing from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.http import HttpResponse, HttpResponseRedirect, Http404
...@@ -151,6 +153,8 @@ class VmDetailView(CheckedDetailView): ...@@ -151,6 +153,8 @@ class VmDetailView(CheckedDetailView):
def __set_name(self, request): def __set_name(self, request):
self.object = self.get_object() self.object = self.get_object()
if not self.object.has_level(request.user, 'owner'):
raise PermissionDenied()
new_name = request.POST.get("new_name") new_name = request.POST.get("new_name")
Instance.objects.filter(pk=self.object.pk).update( Instance.objects.filter(pk=self.object.pk).update(
**{'name': new_name}) **{'name': new_name})
...@@ -174,6 +178,8 @@ class VmDetailView(CheckedDetailView): ...@@ -174,6 +178,8 @@ class VmDetailView(CheckedDetailView):
def __add_tag(self, request): def __add_tag(self, request):
new_tag = request.POST.get('new_tag') new_tag = request.POST.get('new_tag')
self.object = self.get_object() self.object = self.get_object()
if not self.object.has_level(request.user, 'owner'):
raise PermissionDenied()
if len(new_tag) < 1: if len(new_tag) < 1:
message = u"Please input something!" message = u"Please input something!"
...@@ -194,6 +200,8 @@ class VmDetailView(CheckedDetailView): ...@@ -194,6 +200,8 @@ class VmDetailView(CheckedDetailView):
try: try:
to_remove = request.POST.get('to_remove') to_remove = request.POST.get('to_remove')
self.object = self.get_object() self.object = self.get_object()
if not self.object.has_level(request.user, 'owner'):
raise PermissionDenied()
self.object.tags.remove(to_remove) self.object.tags.remove(to_remove)
message = u"Success" message = u"Success"
...@@ -368,44 +376,60 @@ class VmCreate(TemplateView): ...@@ -368,44 +376,60 @@ class VmCreate(TemplateView):
resp = {} resp = {}
try: try:
ikwargs = { pk = request.POST.get('template-pk')
'num_cores': int(request.POST.get('cpu-count')),
'ram_size': int(request.POST.get('ram-size')),
'priority': int(request.POST.get('cpu-priority')),
}
networks = [InterfaceTemplate(vlan=Vlan.objects.get(pk=l),
managed=True)
for l in request.POST.getlist('managed-vlans')
]
networks.extend([InterfaceTemplate(vlan=Vlan.objects.get(pk=l),
managed=False)
for l in request.POST.getlist('unmanaged-vlans')
])
disks = Disk.objects.filter(pk__in=request.POST.getlist('disks'))
template = InstanceTemplate.objects.get( template = InstanceTemplate.objects.get(
pk=request.POST.get('template-pk')) pk=pk)
except InstanceTemplate.DoesNotExist as e:
inst = Instance.create_from_template(template=template, logger.warning('VmCreate.post: %s (pk=%d, user=%s)',
owner=user, networks=networks, unicode(e), unicode(request.user), pk)
disks=disks, **ikwargs) resp['error'] = True
else:
if request.user.has_perm('vm.set_resources'):
ikwargs = {
'num_cores': int(request.POST.get('cpu-count')),
'ram_size': int(request.POST.get('ram-size')),
'priority': int(request.POST.get('cpu-priority')),
}
try:
networks = [InterfaceTemplate(vlan=Vlan.objects.get(pk=l),
managed=True)
for l in request.POST.getlist('managed-vlans')
]
unmanaged = request.POST.getlist('unmanaged-vlans')
networks.extend([
InterfaceTemplate(vlan=Vlan.objects.get(pk=l),
managed=False)
for l in unmanaged])
disks = Disk.objects.filter(
pk__in=request.POST.getlist('disks'))
inst = Instance.create_from_template(
template=template, owner=user, networks=networks,
disks=disks, **ikwargs)
except ObjectDoesNotExist as e:
raise
logger.warning('VmCreate.post: %s (user=%s)',
unicode(e), unicode(request.user))
raise SuspiciousOperation()
else:
inst = Instance.create_from_template(
template=template, owner=user)
inst.deploy_async(user=request.user) inst.deploy_async(user=request.user)
resp['pk'] = inst.pk resp['pk'] = inst.pk
messages.success(request, _('VM successfully created!')) messages.success(request, _('VM successfully created!'))
except InstanceTemplate.DoesNotExist:
resp['error'] = True
except Exception, e:
print e
resp['error'] = True
if request.is_ajax(): if request.is_ajax():
return HttpResponse(json.dumps(resp), return HttpResponse(json.dumps(resp),
content_type="application/json", content_type="application/json",
status=500 if resp.get('error') else 200) status=500 if resp.get('error') else 200)
else: else:
return redirect(reverse_lazy('dashboard.views.detail', resp)) if 'error' in resp:
messages.error(request, _('Failed to create VM.'))
return redirect(reverse('dashboard.index'))
else:
return redirect(reverse('dashboard.views.detail',
args=resp.values()))
class VmDelete(DeleteView): class VmDelete(DeleteView):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment