dynamicweb/opennebula_api/views.py

61 lines
2.3 KiB
Python
Raw Normal View History

from rest_framework import generics
2017-05-11 10:45:09 +00:00
from rest_framework import permissions
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import authenticate, login
from utils.views import LoginViewMixin
from membership.models import CustomUser, StripeCustomer
from guardian.mixins import PermissionRequiredMixin
from .serializers import VirtualMachineTemplateSerializer, \
VirtualMachineSerializer
from .models import VirtualMachineTemplate, VirtualMachine, OpenNebulaManager
2017-05-11 10:45:09 +00:00
from .permissions import IsOwner
class TemplateCreateView(generics.ListCreateAPIView):
2017-05-11 10:45:09 +00:00
"""This class handles the GET and POST requests."""
queryset = VirtualMachineTemplate.objects.all()
serializer_class = VirtualMachineTemplateSerializer
2017-05-11 10:45:09 +00:00
permission_classes = (permissions.IsAuthenticated, permissions.IsAdminUser)
def perform_create(self, serializer):
"""Save the post data when creating a new template."""
serializer.save()
class TemplateDetailsView(generics.RetrieveUpdateDestroyAPIView):
"""This class handles the http GET, PUT and DELETE requests."""
queryset = VirtualMachineTemplate.objects.all()
serializer_class = VirtualMachineTemplateSerializer
2017-05-11 10:45:09 +00:00
permission_classes = (permissions.IsAuthenticated)
class VmCreateView(generics.ListCreateAPIView):
2017-05-11 10:45:09 +00:00
"""This class handles the GET and POST requests."""
queryset = VirtualMachine.objects.all()
serializer_class = VirtualMachineSerializer
2017-05-11 10:45:09 +00:00
permission_classes = (permissions.IsAuthenticated, IsOwner)
def perform_create(self, serializer):
"""Save the post data when creating a new template."""
2017-05-11 10:45:09 +00:00
serializer.save(owner=self.request.user)
class VmDetailsView(generics.RetrieveUpdateDestroyAPIView):
"""This class handles the http GET, PUT and DELETE requests."""
2017-05-11 10:45:09 +00:00
permission_classes = (permissions.IsAuthenticated, IsOwner)
queryset = VirtualMachine.objects.all()
serializer_class = VirtualMachineSerializer
def perform_destroy(self, instance):
owner = instance.owner
manager = OpenNebulaManager(email=owner.email,
password=owner.password[0:20],
create_user = True)
manager.delete_vm(instance.opennebula_id)
instance.delete()