Commit 3ce140a8 by Karsa Zoltán István

from instance, more instance for more users

parent 80829ebb
...@@ -54,7 +54,8 @@ from .views import ( ...@@ -54,7 +54,8 @@ from .views import (
TransferTemplateOwnershipView, TransferTemplateOwnershipConfirmView, TransferTemplateOwnershipView, TransferTemplateOwnershipConfirmView,
OpenSearchDescriptionView, OpenSearchDescriptionView,
NodeActivityView, NodeActivityView,
UserList, TemplateREST, LeaseREST, DiskRest, InstanceREST, InterfaceREST, UserList, TemplateREST, LeaseREST, DiskRest, InstanceREST,
InterfaceREST, InstanceFromTemplateREST, InstanceFTforUsers,
StorageDetail, DiskDetail, StorageDetail, DiskDetail,
MessageList, MessageDetail, MessageCreate, MessageDelete, MessageList, MessageDetail, MessageCreate, MessageDelete,
EnableTwoFactorView, DisableTwoFactorView, EnableTwoFactorView, DisableTwoFactorView,
...@@ -67,9 +68,11 @@ from .views.vm import vm_ops, vm_mass_ops ...@@ -67,9 +68,11 @@ from .views.vm import vm_ops, vm_mass_ops
urlpatterns = [ urlpatterns = [
path('acpi/vm/', InstanceREST.as_view()), path('acpi/vm/', InstanceREST.as_view()),
path('acpi/template/', TemplateREST.as_view()), path('acpi/template/', TemplateREST.as_view()),
path('acpi/ft/', InstanceFromTemplateREST.as_view()),
path('acpi/lease/', LeaseREST.as_view()), path('acpi/lease/', LeaseREST.as_view()),
path('acpi/disk/', DiskRest.as_view()), path('acpi/disk/', DiskRest.as_view()),
path('acpi/interface/', InterfaceREST.as_view()), path('acpi/interface/', InterfaceREST.as_view()),
path('acpi/ftusers/', InstanceFTforUsers.as_view()),
url(r'^$', IndexView.as_view(), name="dashboard.index"), url(r'^$', IndexView.as_view(), name="dashboard.index"),
url(r"^profile/list/$", UserList.as_view(), url(r"^profile/list/$", UserList.as_view(),
name="dashboard.views.user-list"), name="dashboard.views.user-list"),
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
from datetime import timedelta from datetime import timedelta
import json import json
import logging import logging
from string import Template
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import User from django.contrib.auth.models import User
...@@ -56,7 +57,7 @@ from .util import ( ...@@ -56,7 +57,7 @@ from .util import (
GraphMixin GraphMixin
) )
from dashboard.serializers import InstanceTemplateSerializer, LeaseSerializer from dashboard.serializers import InstanceTemplateSerializer, LeaseSerializer, InstanceSerializer
from rest_framework.authentication import TokenAuthentication, BasicAuthentication from rest_framework.authentication import TokenAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser from rest_framework.permissions import IsAdminUser
...@@ -210,6 +211,52 @@ class TemplateREST(APIView): ...@@ -210,6 +211,52 @@ class TemplateREST(APIView):
return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
class InstanceFromTemplateREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, format=None):
data = JSONParser().parse(request)
user = User.objects.get(pk=request.user.pk)
template = InstanceTemplate.objects.get(pk=data['template'])
ikwargs = {
'name': data['name'],
'template': template,
'owner': user,
}
amount = data.get("amount", 1)
instances = Instance.mass_create_from_template(amount=amount,
**ikwargs)
for i in instances:
i.deploy._async(user=user)
serializer = InstanceSerializer(instances, many=True)
return JsonResponse({'instances': serializer.data}, status=201)
class InstanceFTforUsers(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, format=None):
data = JSONParser().parse(request)
user = User.objects.get(pk=request.user.pk)
template = InstanceTemplate.objects.get(pk=data['template'])
ikwargs = {
'name': data['name'],
'users': data['users'],
'template': template,
'operator': data.get('operator', None),
'admin': data.get('admin', None)
}
missing_users, instances = Instance.mass_create_for_users(**ikwargs)
serializer = InstanceSerializer(instances, many=True)
return JsonResponse({'instances': serializer.data, 'missing_users': missing_users}, status=201)
class LeaseREST(APIView): class LeaseREST(APIView):
authentication_classes = [TokenAuthentication, BasicAuthentication] authentication_classes = [TokenAuthentication, BasicAuthentication]
permission_classes = [IsAdminUser] permission_classes = [IsAdminUser]
......
...@@ -112,7 +112,7 @@ class InstanceREST(APIView): ...@@ -112,7 +112,7 @@ class InstanceREST(APIView):
permission_classes = [IsAdminUser] permission_classes = [IsAdminUser]
def get(self, request, format=None): def get(self, request, format=None):
templates = Instance.objects.all() templates = Instance.objects.exclude(status='DESTROYED').all()
serializer = InstanceSerializer(templates, many=True) serializer = InstanceSerializer(templates, many=True)
return JsonResponse({ 'instances': serializer.data}, safe=False) return JsonResponse({ 'instances': serializer.data}, safe=False)
...@@ -120,15 +120,18 @@ class InstanceREST(APIView): ...@@ -120,15 +120,18 @@ class InstanceREST(APIView):
data = JSONParser().parse(request) data = JSONParser().parse(request)
data['pw'] = pwgen() data['pw'] = pwgen()
data['status'] = 'STOPPED' data['status'] = 'STOPPED'
if 'owner' not in data:
data['owner'] = request.user.id
serializer = InstanceSerializer(data=data) serializer = InstanceSerializer(data=data)
if serializer.is_valid(): if serializer.is_valid():
inst = serializer.save() inst = serializer.save()
networks = [] networks = []
for v in data['vlans']: if 'vlans' in data:
v = Vlan.objects.filter(vid=v).get() for v in data['vlans']:
if not v.has_level(request.user, "user"): v = Vlan.objects.filter(vid=v).get()
raise PermissionDenied() if not v.has_level(request.user, "user"):
networks.append(InterfaceTemplate(vlan=v, managed=v.managed)) raise PermissionDenied()
networks.append(InterfaceTemplate(vlan=v, managed=v.managed))
def __on_commit(activity): def __on_commit(activity):
activity.resultant_state = 'PENDING' activity.resultant_state = 'PENDING'
......
...@@ -580,11 +580,15 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -580,11 +580,15 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
""" """
user_instances = [] user_instances = []
missing_users = [] missing_users = []
instances = []
for user_id in users: for user_id in users:
try: try:
user_instances.append(User.objects.get(profile__org_id=user_id)) user_instances.append(User.objects.get(profile__org_id=user_id))
except User.DoesNotExist: except User.DoesNotExist:
missing_users.append(user_id) try:
user_instances.append(User.objects.get(username=user_id))
except User.DoesNotExist:
missing_users.append(user_id)
for user in user_instances: for user in user_instances:
instance = cls.create_from_template(template, user, **kwargs) instance = cls.create_from_template(template, user, **kwargs)
...@@ -592,9 +596,10 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin, ...@@ -592,9 +596,10 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
instance.set_level(User.objects.get(username=admin), 'owner') instance.set_level(User.objects.get(username=admin), 'owner')
if operator: if operator:
instance.set_level(User.objects.get(username=operator), 'operator') instance.set_level(User.objects.get(username=operator), 'operator')
instance.deploy(user=user) instance.deploy._async(user=user)
instances.append(instance)
return missing_users return missing_users, instances
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
self.time_of_suspend, self.time_of_delete = self.get_renew_times() self.time_of_suspend, self.time_of_delete = self.get_renew_times()
......
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