2020-02-23 18:00:42 +00:00
|
|
|
from rest_framework import viewsets, permissions
|
2020-02-23 15:52:30 +00:00
|
|
|
from rest_framework.response import Response
|
2020-02-25 06:50:49 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2020-02-21 15:33:37 +00:00
|
|
|
|
2020-02-23 15:52:30 +00:00
|
|
|
from .models import VM
|
|
|
|
from .serializers import VMSerializer, OpenNebulaVMSerializer
|
2020-02-23 14:33:26 +00:00
|
|
|
|
2020-02-23 18:00:42 +00:00
|
|
|
|
2020-02-23 14:33:26 +00:00
|
|
|
class RawVMViewSet(viewsets.ModelViewSet):
|
2020-02-21 15:33:37 +00:00
|
|
|
queryset = VM.objects.all()
|
|
|
|
serializer_class = VMSerializer
|
2020-02-23 15:52:30 +00:00
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-21 15:33:37 +00:00
|
|
|
|
|
|
|
|
2020-02-25 06:50:49 +00:00
|
|
|
class VMViewSet(viewsets.ViewSet):
|
2020-02-23 14:33:26 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-02-23 15:52:30 +00:00
|
|
|
|
|
|
|
def list(self, request):
|
2020-02-25 06:50:49 +00:00
|
|
|
queryset = VM.objects.filter(owner=request.user)
|
|
|
|
serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request})
|
2020-02-23 15:52:30 +00:00
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
def retrieve(self, request, pk=None):
|
2020-02-25 06:50:49 +00:00
|
|
|
queryset = VM.objects.filter(owner=request.user)
|
2020-02-25 16:03:20 +00:00
|
|
|
vm = get_object_or_404(queryset, pk=pk)
|
|
|
|
serializer = OpenNebulaVMSerializer(vm, context={'request': request})
|
2020-02-23 15:52:30 +00:00
|
|
|
return Response(serializer.data)
|