From 62be9ae7890ee0cad38d541f0c0efdfaa3f23e09 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 14 May 2017 17:39:10 +0530 Subject: [PATCH] Added ServiceUnvailable error handling for the case when we have no connection to OpenNebula. --- opennebula_api/views.py | 46 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/opennebula_api/views.py b/opennebula_api/views.py index 170c9716..d982f7bb 100644 --- a/opennebula_api/views.py +++ b/opennebula_api/views.py @@ -12,6 +12,12 @@ from guardian.mixins import PermissionRequiredMixin from .serializers import VirtualMachineTemplateSerializer, \ VirtualMachineSerializer 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): @@ -37,7 +43,14 @@ class TemplateDetailsView(generics.RetrieveUpdateDestroyAPIView): def get_queryset(self): 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): """This class handles the GET and POST requests.""" @@ -48,7 +61,13 @@ class VmCreateView(generics.ListCreateAPIView): owner = self.request.user manager = OpenNebulaManager(email=owner.email, 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): """Save the post data when creating a new template.""" @@ -64,17 +83,34 @@ class VmDetailsView(generics.RetrieveUpdateDestroyAPIView): owner = self.request.user manager = OpenNebulaManager(email=owner.email, 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): owner = self.request.user manager = OpenNebulaManager(email=owner.email, 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): owner = self.request.user manager = OpenNebulaManager(email=owner.email, password=owner.password) - manager.delete_vm(instance.id) + # 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