Commit 0c41ced7 by Karsa Zoltán István

add vlans to rest

parent bca0eb03
......@@ -3,9 +3,14 @@ from rest_framework import serializers
from django.contrib.auth.models import Group, User
from vm.models import Instance, InstanceTemplate, Lease, Interface, Node, InstanceActivity
from firewall.models import Vlan
from firewall.models import Vlan, Rule
from storage.models import Disk, StorageActivity
class RuleSerializer(serializers.ModelSerializer):
class Meta:
model = Rule
fields = '__all__'
class InstanceActivitySerializer(serializers.ModelSerializer):
get_percentage = serializers.IntegerField()
result_data = serializers.JSONField()
......@@ -65,6 +70,7 @@ class DiskSerializer(serializers.ModelSerializer):
class InstanceSerializer(serializers.ModelSerializer):
ipv4addr = serializers.SerializerMethodField('get_ipv4')
ipv6addr = serializers.SerializerMethodField('get_ipv6')
vlans = serializers.SerializerMethodField('get_vlans')
def get_ipv4(self, i):
return str(i.ipv4)
......@@ -72,11 +78,14 @@ class InstanceSerializer(serializers.ModelSerializer):
def get_ipv6(self, i):
return str(i.ipv6)
def get_vlans(self, i):
return list(net.vlan.id for net in i.interface_set.all() if net.host)
class Meta:
model = Instance
fields = ['id', 'name', 'description', 'status', 'owner', 'access_method', 'boot_menu', 'pw', 'is_base',
'lease', 'raw_data', 'cloud_init', 'ci_meta_data', 'ci_user_data', 'system', 'req_traits',
'has_agent', 'num_cores', 'ram_size', 'max_ram_size', 'arch', 'priority', 'disks', 'node', 'ipv4addr', 'ipv6addr']
'has_agent', 'num_cores', 'ram_size', 'max_ram_size', 'arch', 'priority', 'disks', 'node', 'ipv4addr', 'ipv6addr', 'vlans']
extra_kwargs = {
'disks': {'required': False, 'allow_empty': True,},
'req_traits': {'required': False, 'allow_empty': True,}
......
......@@ -48,7 +48,7 @@ from django.views.generic import (
from braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from storage.tasks import storage_tasks
from vm.tasks.local_tasks import abortable_async_downloaddisk_operation
from vm.operations import (DeployOperation, DestroyOperation, DownloadDiskOperation, ShutdownOperation, RenewOperation,
from vm.operations import (DeployOperation, DestroyOperation, DownloadDiskOperation, RemovePortOperation, ShutdownOperation, RenewOperation,
ResizeDiskOperation, RemoveDiskOperation, SleepOperation, WakeUpOperation, AddPortOperation, SaveAsTemplateOperation,
)
......@@ -105,7 +105,7 @@ from rest_framework.permissions import IsAdminUser
from dashboard.serializers import (
CreateTemplateSerializer, DiskSerializer, InstanceSerializer, InterfaceSerializer, CreateDiskSerializer, DownloadDiskSerializer,
VMDeploySerializer, VlanSerializer, ResizeDiskSerializer, InstanceActivitySerializer, DestroyDiskSerializer, StorageActivitySerializer,
AddPortSerializer,
AddPortSerializer, RuleSerializer,
)
def size_util(size: str):
......@@ -337,6 +337,10 @@ class SleepInstanceREST(APIView):
return JsonResponse(serializer.data, safe=False, status=201)
class RulesREST(APIView):
def post():
pass
class SetupPortREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
......@@ -350,12 +354,21 @@ class SetupPortREST(APIView):
if addport.is_valid():
AddPortOperation(instance).call(port=int(data['port_destination']), proto=str(data['type']), host=interface.host, user=request.user)
rules = Rule.objects.filter(host=interface.host)
return JsonResponse({}, status=201)
serializer = RuleSerializer(rules, many=True)
return JsonResponse(serializer.data, safe=False, status=201)
return JsonResponse({}, status=400)
def delete(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():
rule = Rule.objects.filter(host=interface.host).filter(dport=int(data['port_destination'])).filter(proto=str(data['type'])).get()
RemovePortOperation(instance).call(rule=rule, user=request.user)
return JsonResponse({}, safe=False, status=204)
return JsonResponse({}, status=400)
class WakeUpInstanceREST(APIView):
......
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