Commit ee2742ae by Karsa Zoltán István

initail setup for port operations

parent 39db4eb2
......@@ -132,8 +132,10 @@ class DestroyDiskSerializer(serializers.Serializer):
extra_kwargs = {'instance': {'required': False, 'allow_null': True}}
class AddPortSerializer(serializers.Serializer):
port_type = serializers.CharField(max_length=5)
port_number = serializers.IntegerField()
type = serializers.CharField(max_length=5)
port_destination = serializers.IntegerField()
port_source = serializers.IntegerField(required=False)
forwarding = serializers.BooleanField(required=False)
class VMDeploySerializer(serializers.Serializer):
node = serializers.IntegerField(required=False)
......
......@@ -58,12 +58,13 @@ from .views import (
InterfaceREST, InstanceFromTemplateREST, InstanceFTforUsersREST,
DownloadDiskREST, GetInstanceREST, GetInterfaceREST, ShutdownInstanceREST,
GetLeaseREST, GetDiskRest, DeployInstanceREST, CreateDiskREST,
VlanREST, ResizeDiskREST, GetVlanREST, DestroyDiskREST,
VlanREST, ResizeDiskREST, GetVlanREST, DestroyDiskREST, InstanceFTforUsersIdREST,
StorageDetail, DiskDetail, UserREST, GroupREST, CreateTemplateREST,
InstanceActivityREST, GetInstanceActivityREST, GetGroupREST, GetUserREST,
SleepInstanceREST, WakeUpInstanceREST, DownloadPersistentDiskREST,
CreatePersistentDiskREST, GetStorageActivityREST, GetTemplateREST,
MessageList, MessageDetail, MessageCreate, MessageDelete,
SetupPortREST,
EnableTwoFactorView, DisableTwoFactorView,
AclUserGroupAutocomplete, AclUserAutocomplete,
RescheduleView, GroupImportView, GroupExportView,
......@@ -97,7 +98,9 @@ urlpatterns = [
path('acpi/vlan/<int:pk>/', GetVlanREST.as_view()),
path('acpi/interface/<int:pk>/', GetInterfaceREST.as_view()),
path('acpi/ftusers/', InstanceFTforUsersREST.as_view()),
path('acpi/ftusersid/', InstanceFTforUsersIdREST.as_view()),
path('acpi/vm/<int:pk>/downloaddisk/', DownloadDiskREST.as_view()),
path('acpi/vm/<int:vm_id>/port/<int:vlan_id>/', SetupPortREST.as_view()),
path('acpi/vm/<int:pk>/createdisk/', CreateDiskREST.as_view()),
path('acpi/vm/<int:pk>/deploy/', DeployInstanceREST.as_view()),
path('acpi/vm/<int:pk>/shutdown/', ShutdownInstanceREST.as_view()),
......
......@@ -199,6 +199,13 @@ class TemplateREST(APIView):
permission_classes = [IsAdminUser]
def get(self, request, format=None):
if request.query_params.get("name"):
try:
template = InstanceTemplate.objects.filter(name__istartswith=request.query_params.get('name')).get()
serializer = InstanceTemplateSerializer(template, many=False)
return JsonResponse(serializer.data, safe=False)
except:
return JsonResponse(state=411)
templates = InstanceTemplate.objects.all()
serializer = InstanceTemplateSerializer(templates, many=True)
return JsonResponse(serializer.data, safe=False)
......@@ -254,10 +261,11 @@ class InstanceFromTemplateREST(APIView):
**ikwargs)
for i in instances:
i.deploy._async(user=user)
if amount == 1:
serializer = InstanceSerializer(instances, many=True)
return JsonResponse(serializer.data, status=201, safe=False)
serializer = InstanceSerializer(instances, many=True)
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.data, status=201, safe=False)
class InstanceFTforUsersREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
......@@ -290,6 +298,37 @@ class InstanceFTforUsersREST(APIView):
serializer = InstanceSerializer(instances, many=True)
return JsonResponse(serializer.data, status=201)
class InstanceFTforUsersIdREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, format=None):
data = JSONParser().parse(request)
template = InstanceTemplate.objects.get(pk=int(data['template']))
ikwargs = {
'name': data['name'],
'users': data['users'],
'template': template,
'operator': data.get('operator', None),
'admin': data.get('admin', None)
}
if 'num_cores' in data:
ikwargs.update({'num_cores':data['num_cores']})
if 'ram_size' in data:
ikwargs.update({'ram_size':data['ram_size']})
if 'priority' in data:
ikwargs.update({'priority':data['priority']})
if 'max_ram_size' in data:
ikwargs.update({'max_ram_size':data['ram_size']})
missing_users, instances = Instance.mass_create_for_users_id(**ikwargs)
serializer = InstanceSerializer(instances, many=True)
return JsonResponse(serializer.data, status=201, safe=False)
class LeaseREST(APIView):
authentication_classes = [TokenAuthentication, BasicAuthentication]
......
......@@ -337,6 +337,26 @@ class SleepInstanceREST(APIView):
return JsonResponse(serializer.data, safe=False, status=201)
class SetupPortREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, vm_id, vlan_id, format=None):
instance = Instance.objects.get(pk=vm_id)
data = JSONParser().parse(request)
addport = AddPortSerializer(data=data)
vlan=Vlan.objects.get(pk=vlan_id)
interface = Interface.objects.filter(instance=instance).filter(vlan=vlan).get()
if addport.is_valid():
AddPortOperation(instance).call(port=int(data['port_destination']), proto=str(data['type']), host=interface.host)
return JsonResponse({}, status=201)
return JsonResponse(status=400)
def delete(self, request, vm_id, vlan_id, format=None):
return JsonResponse({}, safe=False, status=204)
class WakeUpInstanceREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
......
......@@ -601,6 +601,32 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
return missing_users, instances
@classmethod
def mass_create_for_users_id(cls, template, users, admin=None, operator=None, **kwargs):
"""
Create and deploy an instance of a template for each user
in a list of users. Returns the user IDs of missing users.
"""
user_instances = []
missing_users = []
instances = []
for user_id in users:
try:
user_instances.append(User.objects.get(pk=user_id))
except User.DoesNotExist:
missing_users.append(user_id)
for user in user_instances:
instance = cls.create_from_template(template, user, **kwargs)
if admin:
instance.set_level(User.objects.get(pk=admin), 'owner')
if operator:
instance.set_level(User.objects.get(pk=operator), 'operator')
instance.deploy._async(user=user)
instances.append(instance)
return missing_users, instances
def clean(self, *args, **kwargs):
self.time_of_suspend, self.time_of_delete = self.get_renew_times()
super(Instance, self).clean(*args, **kwargs)
......
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