Commit cb5e6c89 by Szabolcs Gelencser

Add vm creation from template

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