Added ServiceUnvailable error handling for the case when we have no connection to OpenNebula.

This commit is contained in:
M.Ravi 2017-05-14 17:39:10 +05:30
parent 7cbe75014d
commit 62be9ae789

View file

@ -12,6 +12,12 @@ from guardian.mixins import PermissionRequiredMixin
from .serializers import VirtualMachineTemplateSerializer, \ from .serializers import VirtualMachineTemplateSerializer, \
VirtualMachineSerializer VirtualMachineSerializer
from .models import OpenNebulaManager from .models import OpenNebulaManager
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
default_code = 'service_unavailable'
class TemplateCreateView(generics.ListCreateAPIView): class TemplateCreateView(generics.ListCreateAPIView):
@ -37,7 +43,14 @@ class TemplateDetailsView(generics.RetrieveUpdateDestroyAPIView):
def get_queryset(self): def get_queryset(self):
manager = OpenNebulaManager() manager = OpenNebulaManager()
return manager.get_templates() # We may have ConnectionRefusedError if we don't have a
# connection to OpenNebula. For now, we raise ServiceUnavailable
try:
templates = manager.get_templates()
except ConnectionRefusedError:
raise ServiceUnavailable
return templates
class VmCreateView(generics.ListCreateAPIView): class VmCreateView(generics.ListCreateAPIView):
"""This class handles the GET and POST requests.""" """This class handles the GET and POST requests."""
@ -48,7 +61,13 @@ class VmCreateView(generics.ListCreateAPIView):
owner = self.request.user owner = self.request.user
manager = OpenNebulaManager(email=owner.email, manager = OpenNebulaManager(email=owner.email,
password=owner.password) password=owner.password)
return manager.get_vms() # 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
def perform_create(self, serializer): def perform_create(self, serializer):
"""Save the post data when creating a new template.""" """Save the post data when creating a new template."""
@ -64,17 +83,34 @@ class VmDetailsView(generics.RetrieveUpdateDestroyAPIView):
owner = self.request.user owner = self.request.user
manager = OpenNebulaManager(email=owner.email, manager = OpenNebulaManager(email=owner.email,
password=owner.password) password=owner.password)
return manager.get_vms() # 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
def get_object(self): def get_object(self):
owner = self.request.user owner = self.request.user
manager = OpenNebulaManager(email=owner.email, manager = OpenNebulaManager(email=owner.email,
password=owner.password) password=owner.password)
return manager.get_vm(self.kwargs.get('pk')) # 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
def perform_destroy(self, instance): def perform_destroy(self, instance):
owner = self.request.user owner = self.request.user
manager = OpenNebulaManager(email=owner.email, manager = OpenNebulaManager(email=owner.email,
password=owner.password) password=owner.password)
# We may have ConnectionRefusedError if we don't have a
# connection to OpenNebula. For now, we raise ServiceUnavailable
try:
manager.delete_vm(instance.id) manager.delete_vm(instance.id)
except ConnectionRefusedError:
raise ServiceUnavailable