uncloud-mravi/uncloud/opennebula/views.py

28 lines
973 B
Python
Raw Normal View History

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