Commit 3ce140a8 by Karsa Zoltán István

from instance, more instance for more users

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