Commit 60007ab6 by Karsa Zoltán István

download diks via rest

parent ac664c40
......@@ -59,4 +59,9 @@ class VlanSerializer(serializers.ModelSerializer):
class CreateDiskSerializer(serializers.Serializer):
size = serializers.IntegerField()
name = serializers.CharField(max_length=100)
class DownloadDiskSerializer(serializers.Serializer):
url = serializers.CharField(max_length=500)
name = serializers.CharField(max_length=100)
\ No newline at end of file
......@@ -56,7 +56,8 @@ from .views import (
NodeActivityView,
UserList, TemplateREST, LeaseREST, DiskRest, InstanceREST,
InterfaceREST, InstanceFromTemplateREST, InstanceFTforUsersREST,
DownloadDiskREST, GetInstanceREST,
DownloadDiskREST, GetInstanceREST, GetInterfaceREST,
GetLeaseREST, GetDiskRest,
StorageDetail, DiskDetail,
MessageList, MessageDetail, MessageCreate, MessageDelete,
EnableTwoFactorView, DisableTwoFactorView,
......@@ -72,10 +73,13 @@ urlpatterns = [
path('acpi/template/', TemplateREST.as_view()),
path('acpi/ft/', InstanceFromTemplateREST.as_view()),
path('acpi/lease/', LeaseREST.as_view()),
path('acpi/lease/<int:pk>/', GetLeaseREST.as_view()),
path('acpi/disk/', DiskRest.as_view()),
path('acpi/disk/<int:pk>/', GetDiskRest.as_view()),
path('acpi/interface/', InterfaceREST.as_view()),
path('acpi/interface/<int:pk>/', GetInterfaceREST.as_view()),
path('acpi/ftusers/', InstanceFTforUsersREST.as_view()),
path('acpi/vm/<int:pk>/createdisk/', DownloadDiskREST.as_view()),
path('acpi/vm/<int:pk>/downloaddisk/', DownloadDiskREST.as_view()),
url(r'^$', IndexView.as_view(), name="dashboard.index"),
url(r"^profile/list/$", UserList.as_view(),
name="dashboard.views.user-list"),
......
......@@ -154,7 +154,7 @@ class DiskRest(APIView):
def get(self, request, format=None):
templates = Disk.objects.all()
serializer = DiskSerializer(templates, many=True)
return JsonResponse({ 'disks': serializer.data}, safe=False)
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
......@@ -162,4 +162,18 @@ class DiskRest(APIView):
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
\ No newline at end of file
return JsonResponse(serializer.errors, status=400)
class GetDiskRest(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
disk = Disk.objects.get(pk=pk)
serializer = DiskSerializer(disk, many=False)
return JsonResponse(serializer.data, safe=False)
def delete(self, request, pk, format=None):
return JsonResponse(status=204)
......@@ -200,7 +200,7 @@ class TemplateREST(APIView):
def get(self, request, format=None):
templates = InstanceTemplate.objects.all()
serializer = InstanceTemplateSerializer(templates, many=True)
return JsonResponse({ 'templates': serializer.data}, safe=False)
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
......@@ -243,7 +243,7 @@ class InstanceFromTemplateREST(APIView):
i.deploy._async(user=user)
serializer = InstanceSerializer(instances, many=True)
return JsonResponse({'instances': serializer.data}, status=201)
return JsonResponse(serializer.data, status=201)
class InstanceFTforUsersREST(APIView):
......@@ -275,7 +275,7 @@ class InstanceFTforUsersREST(APIView):
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)
return JsonResponse(serializer.data, status=201)
class LeaseREST(APIView):
......@@ -285,7 +285,7 @@ class LeaseREST(APIView):
def get(self, request, format=None):
templates = Lease.objects.all()
serializer = LeaseSerializer(templates, many=True)
return JsonResponse({ 'leases': serializer.data}, safe=False)
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
......@@ -296,6 +296,16 @@ class LeaseREST(APIView):
return JsonResponse(serializer.errors, status=400)
class GetLeaseREST(APIView):
authentication_classes = [TokenAuthentication, BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
lease = Lease.objects.get(pk=pk)
serializer = LeaseSerializer(lease, many=False)
return JsonResponse(serializer.data, safe=False)
class TemplateAclUpdateView(AclUpdateView):
model = InstanceTemplate
......
......@@ -43,6 +43,7 @@ from django.views.generic import (
)
from braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from vm.operations import DownloadDiskOperation
from common.models import (
create_readable, HumanReadableException, fetch_human_exception,
......@@ -94,7 +95,7 @@ from rest_framework.parsers import JSONParser
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser
from dashboard.serializers import InstanceSerializer, InterfaceSerializer, CreateDiskSerializer
from dashboard.serializers import InstanceSerializer, InterfaceSerializer, CreateDiskSerializer, DownloadDiskSerializer
class InterfaceREST(APIView):
......@@ -104,16 +105,16 @@ class InterfaceREST(APIView):
def get(self, request, format=None):
templates = Interface.objects.all()
serializer = InterfaceSerializer(templates, many=True)
return JsonResponse({ 'interfaces': serializer.data}, safe=False)
return JsonResponse(serializer.data, safe=False)
class GetInstanceREST(APIView):
class GetInterfaceREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
template = Instance.objects.get(pk=pk)
serializer = InstanceSerializer(template, many=False)
interface = Interface.objects.get(pk=pk)
serializer = InterfaceSerializer(interface, many=False)
return JsonResponse(serializer.data, safe=False)
......@@ -124,7 +125,7 @@ class InstanceREST(APIView):
def get(self, request, format=None):
templates = Instance.objects.exclude(status='DESTROYED').all()
serializer = InstanceSerializer(templates, many=True)
return JsonResponse({ 'instances': serializer.data}, safe=False)
return JsonResponse(serializer.data, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
......@@ -163,12 +164,40 @@ class InstanceREST(APIView):
return JsonResponse(serializer.errors, status=400)
class GetInstanceREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
instance = Instance.objects.get(pk=pk)
serializer = InstanceSerializer(instance, many=False)
return JsonResponse(serializer.data, safe=False)
class DownloadDiskREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, pk, format=None):
data = JSONParser().parse(request)
serializer = DownloadDiskSerializer(data=data)
if serializer.is_valid():
vm_id = pk
disk_url = str(data['url'])
disk_name = str(data['name'])
instance = Instance.objects.get(pk=vm_id)
DownloadDiskOperation(instance)._async(name=disk_name, url=disk_url, user=instance.owner)
ret = InstanceSerializer(instance, many=False)
return JsonResponse(ret.data, status=201)
return JsonResponse(serializer.errors, status=400)
class CreateDiskREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def post(self, request, pk, format=None):
data = JSONParser().parse(request)
serializer = CreateDiskSerializer(data=data)
if serializer.is_valid():
vm_id = pk
......
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