2017-05-11 02:05:58 +00:00
|
|
|
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
|
2017-05-11 03:51:30 +00:00
|
|
|
|
|
|
|
from .serializers import VirtualMachineTemplateSerializer, \
|
|
|
|
VirtualMachineSerializer
|
2017-05-12 10:07:05 +00:00
|
|
|
from .models import OpenNebulaManager
|
2017-05-14 12:09:10 +00:00
|
|
|
from rest_framework.exceptions import APIException
|
|
|
|
|
|
|
|
class ServiceUnavailable(APIException):
|
|
|
|
status_code = 503
|
|
|
|
default_detail = 'Service temporarily unavailable, try again later.'
|
|
|
|
default_code = 'service_unavailable'
|
2017-05-11 10:45:09 +00:00
|
|
|
|
2017-05-09 14:33:56 +00:00
|
|
|
|
2017-05-11 03:51:30 +00:00
|
|
|
class VmCreateView(generics.ListCreateAPIView):
|
2017-05-11 10:45:09 +00:00
|
|
|
"""This class handles the GET and POST requests."""
|
2017-05-11 03:51:30 +00:00
|
|
|
serializer_class = VirtualMachineSerializer
|
2017-05-12 10:07:05 +00:00
|
|
|
permission_classes = (permissions.IsAuthenticated, )
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
owner = self.request.user
|
|
|
|
manager = OpenNebulaManager(email=owner.email,
|
2017-05-13 04:59:57 +00:00
|
|
|
password=owner.password)
|
2017-05-14 12:09:10 +00:00
|
|
|
# We may have ConnectionRefusedError if we don't have a
|
|
|
|
# connection to OpenNebula. For now, we raise ServiceUnavailable
|
|
|
|
try:
|
|
|
|
vms = manager.get_vms()
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
raise ServiceUnavailable
|
|
|
|
return vms
|
2017-05-11 03:51:30 +00:00
|
|
|
|
|
|
|
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)
|
2017-05-11 03:51:30 +00:00
|
|
|
|
|
|
|
class VmDetailsView(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
"""This class handles the http GET, PUT and DELETE requests."""
|
2017-05-12 10:07:05 +00:00
|
|
|
permission_classes = (permissions.IsAuthenticated, )
|
2017-05-11 03:51:30 +00:00
|
|
|
|
|
|
|
serializer_class = VirtualMachineSerializer
|
2017-05-11 15:39:59 +00:00
|
|
|
|
2017-05-12 10:07:05 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
owner = self.request.user
|
|
|
|
manager = OpenNebulaManager(email=owner.email,
|
2017-05-13 04:59:57 +00:00
|
|
|
password=owner.password)
|
2017-05-14 12:09:10 +00:00
|
|
|
# We may have ConnectionRefusedError if we don't have a
|
|
|
|
# connection to OpenNebula. For now, we raise ServiceUnavailable
|
|
|
|
try:
|
|
|
|
vms = manager.get_vms()
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
raise ServiceUnavailable
|
|
|
|
return vms
|
2017-05-12 10:07:05 +00:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
owner = self.request.user
|
|
|
|
manager = OpenNebulaManager(email=owner.email,
|
2017-05-13 04:59:57 +00:00
|
|
|
password=owner.password)
|
2017-05-14 12:09:10 +00:00
|
|
|
# We may have ConnectionRefusedError if we don't have a
|
|
|
|
# connection to OpenNebula. For now, we raise ServiceUnavailable
|
|
|
|
try:
|
|
|
|
vm = manager.get_vm(self.kwargs.get('pk'))
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
raise ServiceUnavailable
|
|
|
|
return vm
|
2017-05-12 10:07:05 +00:00
|
|
|
|
2017-05-11 15:39:59 +00:00
|
|
|
def perform_destroy(self, instance):
|
2017-05-12 10:07:05 +00:00
|
|
|
owner = self.request.user
|
2017-05-11 15:39:59 +00:00
|
|
|
manager = OpenNebulaManager(email=owner.email,
|
2017-05-13 04:59:57 +00:00
|
|
|
password=owner.password)
|
2017-05-14 12:09:10 +00:00
|
|
|
# We may have ConnectionRefusedError if we don't have a
|
|
|
|
# connection to OpenNebula. For now, we raise ServiceUnavailable
|
|
|
|
try:
|
|
|
|
manager.delete_vm(instance.id)
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
raise ServiceUnavailable
|
2017-05-11 15:39:59 +00:00
|
|
|
|