Commit e4964aff by Szabolcs Gelencser

Implement lease request creation

parent bad98db4
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-05-17 14:30
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dashboard', '0008_auto_20180420_1054'),
]
operations = [
migrations.RemoveField(
model_name='profile',
name='disk_quota',
),
migrations.RemoveField(
model_name='profile',
name='instance_limit',
),
migrations.RemoveField(
model_name='profile',
name='network_limit',
),
migrations.RemoveField(
model_name='profile',
name='org_id',
),
migrations.RemoveField(
model_name='profile',
name='smb_password',
),
migrations.RemoveField(
model_name='profile',
name='two_factor_secret',
),
migrations.RemoveField(
model_name='profile',
name='user',
),
migrations.AddField(
model_name='profile',
name='user_id',
field=models.CharField(default=0, max_length=100, unique=True),
preserve_default=False,
),
]
......@@ -8,7 +8,7 @@
<a class="btn btn-default" href="{{object.get_absolute_url}}" data-dismiss="modal">
{% trans "Cancel" %}
</a>
{% if object.active and lease_types and not request.token_user %}
{% if lease_types and not request.token_user %}
<a class="btn btn-primary" id="vm-renew-request-lease-button"
href="{% url "request.views.request-lease" vm_pk=object.id %}">
<i class="fa fa-forward"></i>
......
......@@ -732,7 +732,10 @@ class VmRenewView(FormOperationMixin, VmOperationView):
# Lease.objects.filter(pk=default.pk).distinct())
val = super(VmRenewView, self).get_form_kwargs()
val.update({'choices': choices, 'default': default})
val.update({
'choices': choices,
'default': default,
})
return val
def get_response_data(self, result, done, extra=None, **kwargs):
......
......@@ -31,6 +31,7 @@ from django.utils.translation import (
from django.core.urlresolvers import reverse
import requests
from openstack_api.nova import Server
from sizefield.models import FileSizeField
from model_utils.models import TimeStampedModel
from model_utils import Choices
......@@ -79,7 +80,7 @@ class Request(TimeStampedModel):
)
status = CharField(choices=STATUSES, default=STATUSES.PENDING,
max_length=10)
user = ForeignKey(User, related_name="user")
user_id = CharField(blank=False, max_length=100)
closed_by = ForeignKey(User, related_name="closed_by", null=True)
TYPES = Choices(
('resource', _('resource request')),
......@@ -219,22 +220,23 @@ class ResourceChangeAction(RequestAction):
class ExtendLeaseAction(RequestAction):
instance = ForeignKey(Instance)
os_server_id = CharField(blank=False, max_length=100)
lease_type = ForeignKey(LeaseType)
def accept(self, user):
self.instance.renew(lease=self.lease_type.lease, save=True, force=True,
user=user)
pass
# self.instance.renew(lease=self.lease_type.lease, save=True, force=True, user=user)
@property
def accept_msg(self):
return _(
'The lease of <a href="%(url)s">%(name)s</a> got extended. '
'(suspend: %(suspend)s, remove: %(remove)s)'
) % {'name': self.instance.name,
'url': self.instance.get_absolute_url(),
'suspend': self.lease_type.lease.get_readable_suspend_time(),
'remove': self.lease_type.lease.get_readable_delete_time(), }
# return _(
# 'The lease of <a href="%(url)s">%(name)s</a> got extended. '
# '(suspend: %(suspend)s, remove: %(remove)s)'
# ) % {'name': self.instance.name,
# 'url': self.instance.get_absolute_url(),
# 'suspend': self.lease_type.lease.get_readable_suspend_time(),
# 'remove': self.lease_type.lease.get_readable_delete_time(), }
return "got extended"
class TemplateAccessAction(RequestAction):
......@@ -288,6 +290,7 @@ class DiskResizeAction(RequestAction):
def send_notifications(sender, instance, created, **kwargs):
return #TODO
if not created:
return
......
......@@ -29,7 +29,7 @@ from .views import (
urlpatterns = [
url(r'^list/$', RequestList.as_view(),
name="request.views.request-list"),
url(r'^(?P<pk>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/$', RequestDetail.as_view(),
url(r'^(?P<pk>\d+)/$', RequestDetail.as_view(),
name="request.views.request-detail"),
url(r'^type/list/$', RequestTypeList.as_view(),
......
......@@ -16,6 +16,7 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals, absolute_import
import openstack_api
from braces.views._access import AccessMixin
from django.views.generic import (
UpdateView, TemplateView, DetailView, CreateView, FormView, DeleteView,
......@@ -211,38 +212,7 @@ class TemplateRequestView(LoginRequiredMixin, FormView):
return redirect(reverse("dashboard.index"))
class VmRequestMixin(LoginRequiredMixin, object):
def get_vm(self):
return get_object_or_404(Instance, pk=self.kwargs['vm_pk'])
def dispatch(self, *args, **kwargs):
vm = self.get_vm()
user = self.request.user
if not vm.has_level(user, self.user_level):
raise PermissionDenied()
if vm.destroyed_at:
message = _("Instance %(instance)s has already been destroyed.")
messages.error(self.request, message % {'instance': vm.name})
return redirect(vm.get_absolute_url())
return super(VmRequestMixin, self).dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(VmRequestMixin, self).get_context_data(**kwargs)
context['vm'] = self.get_vm()
return context
def get_form_kwargs(self):
kwargs = super(VmRequestMixin, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
def form_valid(self, form):
raise NotImplementedError
class LeaseRequestView(VmRequestMixin, FormView):
class LeaseRequestView(LoginRequiredMixin, FormView):
form_class = LeaseRequestForm
template_name = "request/request-lease.html"
user_level = "operator"
......@@ -251,16 +221,16 @@ class LeaseRequestView(VmRequestMixin, FormView):
def form_valid(self, form):
data = form.cleaned_data
user = self.request.user
vm = self.get_vm()
vm = openstack_api.nova.server_get(self.request, self.kwargs['vm_pk']) # to check priviliges
el = ExtendLeaseAction(
lease_type=data['lease'],
instance=vm,
os_server_id=vm.id,
)
el.save()
req = Request(
user=user,
user_id=user.id,
message=data['message'],
type=Request.TYPES.lease,
action=el
......@@ -268,10 +238,10 @@ class LeaseRequestView(VmRequestMixin, FormView):
req.save()
messages.success(self.request, self.success_message)
return redirect(vm.get_absolute_url())
return redirect(reverse('dashboard.views.detail', args=[self.kwargs['vm_pk']]))
class ResourceRequestView(VmRequestMixin, FormView):
class ResourceRequestView(LoginRequiredMixin, FormView):
form_class = ResourceRequestForm
template_name = "request/request-resource.html"
user_level = "user"
......@@ -316,7 +286,7 @@ class ResourceRequestView(VmRequestMixin, FormView):
return redirect(vm.get_absolute_url())
class ResizeRequestView(VmRequestMixin, FormView):
class ResizeRequestView(LoginRequiredMixin, FormView):
form_class = ResizeRequestForm
template_name = "request/_request-resize-form.html"
user_level = "owner"
......
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