Commit cb5e6c89 by Szabolcs Gelencser

Add vm creation from template

parent 3199d66f
from django.template import Library from django.template import Library
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
register = Library() register = Library()
...@@ -29,4 +30,8 @@ def get_status_display(instance): ...@@ -29,4 +30,8 @@ def get_status_display(instance):
'BUILDING': _('building'), 'BUILDING': _('building'),
} }
return DISPLAYS[instance.status] if instance.status in DISPLAYS else instance.status return DISPLAYS[instance.status] if instance.status in DISPLAYS else instance.status
\ No newline at end of file
@register.filter
def get_absolute_url(instance):
return reverse('dashboard.views.detail', args=[instance.id])
\ No newline at end of file
...@@ -117,10 +117,14 @@ class VmDetailView(LoginRequiredMixin, GraphMixin, DetailView): ...@@ -117,10 +117,14 @@ class VmDetailView(LoginRequiredMixin, GraphMixin, DetailView):
ops = get_operations(instance, user, self.request) ops = get_operations(instance, user, self.request)
hide_tutorial = self.request.COOKIES.get( hide_tutorial = self.request.COOKIES.get(
"hide_tutorial_for_%s" % instance.id) == "True" "hide_tutorial_for_%s" % instance.id) == "True"
vnc_console = openstack_api.nova.server_vnc_console(self.request, instance.id) vnc_console = None
try:
vnc_console = openstack_api.nova.server_vnc_console(self.request, instance.id)
except:
pass #TODO
context.update({ context.update({
'graphite_enabled': settings.GRAPHITE_URL is not None, 'graphite_enabled': settings.GRAPHITE_URL is not None,
'vnc_url': vnc_console.url, 'vnc_url': vnc_console.url if vnc_console else None,
'ops': ops, 'ops': ops,
'op': {i.op: i for i in ops}, 'op': {i.op: i for i in ops},
# 'connect_commands': user.profile.get_connect_commands(instance), # 'connect_commands': user.profile.get_connect_commands(instance),
...@@ -1077,7 +1081,7 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1077,7 +1081,7 @@ class VmCreate(LoginRequiredMixin, TemplateView):
pk=int(pk)) pk=int(pk))
except (ValueError, InstanceTemplate.DoesNotExist): except (ValueError, InstanceTemplate.DoesNotExist):
raise Http404() raise Http404()
if not template.has_level(request.user, 'user'): if not request.user.id == template.owner_id:
raise PermissionDenied() raise PermissionDenied()
return template return template
...@@ -1117,16 +1121,25 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1117,16 +1121,25 @@ class VmCreate(LoginRequiredMixin, TemplateView):
}) })
return self.render_to_response(context) return self.render_to_response(context)
def _create_from_plain_image(self, request, *args, **kwargs): def __create_normal(self, request, template, *args, **kwargs):
pass server_created = openstack_api.nova.server_create(
request,
template.name,
template.image_id,
template.flavor_id,
)
def __create_normal(self, request, *args, **kwargs): from dashboard.templatetags.instance_tags import get_absolute_url
instances = [Instance.create_from_template( messages.success(request, _("VM successfully created."))
template=template, path = get_absolute_url(server_created)
owner=request.user)]
return self.__deploy(request, instances)
def __create_customized(self, request, *args, **kwargs): if request.is_ajax():
return HttpResponse(json.dumps({'redirect': path}),
content_type="application/json")
else:
return HttpResponseRedirect("%s#activity" % path)
def __create_customized(self, request, template, *args, **kwargs):
user = request.user user = request.user
# no form yet, using POST directly: # no form yet, using POST directly:
form = self.form_class( form = self.form_class(
...@@ -1171,7 +1184,7 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1171,7 +1184,7 @@ class VmCreate(LoginRequiredMixin, TemplateView):
messages.success(request, ungettext_lazy( messages.success(request, ungettext_lazy(
"Successfully created %(count)d VM.", # this should not happen "Successfully created %(count)d VM.", # this should not happen
"Successfully created %(count)d VMs.", len(instances)) % { "Successfully created %(count)d VMs.", len(instances)) % {
'count': len(instances)}) 'count': len(instances)})
path = "%s?stype=owned" % reverse("dashboard.views.vm-list") path = "%s?stype=owned" % reverse("dashboard.views.vm-list")
else: else:
messages.success(request, _("VM successfully created.")) messages.success(request, _("VM successfully created."))
...@@ -1187,13 +1200,13 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1187,13 +1200,13 @@ class VmCreate(LoginRequiredMixin, TemplateView):
#TODO: check if user can create VM #TODO: check if user can create VM
#TODO: limit chekcs #TODO: limit chekcs
create_func = (self._create_from_plain_image if template = self.get_template(request, request.POST.get("template"))
request.POST.get("from_plain_image") is not None else
self.__create_normal if create_func = (self.__create_normal if
request.POST.get("customized") is None else request.POST.get("customized") is None else
self.__create_customized) self.__create_customized)
return create_func(request, *args, **kwargs) return create_func(request, template, *args, **kwargs)
# @require_GET # @require_GET
......
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