Commit e28c1606 by Chif Gergő

Create flavor serializer, add delete methods to models

parent 83f92285
...@@ -55,7 +55,16 @@ class Flavor(models.Model): ...@@ -55,7 +55,16 @@ class Flavor(models.Model):
return flavor return flavor
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
raise ValueError("Couldn't create Flavor in remote cloud.") raise ValueError("Can't create Flavor in remote cloud.")
def delete(self):
interface = OSVirtualMachineManager(settings.CONNECTION)
try:
interface.delete_flavor(self.remote_id)
super(Flavor, self).delete()
except Exception as e:
if e.OpenStackError:
logger.error("Can not delete the flavor in remote cloud")
class Instance(models.Model): class Instance(models.Model):
...@@ -135,7 +144,7 @@ class Instance(models.Model): ...@@ -135,7 +144,7 @@ class Instance(models.Model):
return new_inst return new_inst
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
raise ValueError("Couldn't create Flavor in remote cloud." raise ValueError("Can't create Instance in remote cloud."
"Search the logs for more detail.") "Search the logs for more detail.")
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
...@@ -153,3 +162,12 @@ class Instance(models.Model): ...@@ -153,3 +162,12 @@ class Instance(models.Model):
timezone.now() + timedelta( timezone.now() + timedelta(
seconds=lease.delete_interval_in_sec) seconds=lease.delete_interval_in_sec)
) )
def delete(self):
interface = OSVirtualMachineManager(settings.CONNECTION)
try:
interface.destroy_vm(self.remote_id)
super(Instance, self).delete()
except Exception as e:
if e.OpenStackError:
logger.error("Can not delete the instance in remote cloud")
from rest_framework import serializers from rest_framework import serializers
from .models import Flavor, Instance from .models import Flavor, Instance, Lease
class InstanceSerializer(serializers.ModelSerializer): class InstanceSerializer(serializers.ModelSerializer):
...@@ -15,3 +15,9 @@ class FlavorSerializer(serializers.ModelSerializer): ...@@ -15,3 +15,9 @@ class FlavorSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Flavor model = Flavor
fields = "__all__" fields = "__all__"
class LeaseSerializer(serializers.ModelSerializer):
class Meta:
model = Lease
fields = '__all__'
...@@ -4,6 +4,7 @@ from rest_framework import routers ...@@ -4,6 +4,7 @@ from rest_framework import routers
router = routers.SimpleRouter() router = routers.SimpleRouter()
router.register(r'instances', views.InstanceViewSet, basename='instance') router.register(r'instances', views.InstanceViewSet, basename='instance')
router.register(r'flavors', views.FlavorViewSet, basename='flavor') router.register(r'flavors', views.FlavorViewSet, basename='flavor')
router.register(r'leases', views.LeaseViewSet, basename='lease')
urlpatterns = router.urls urlpatterns = router.urls
from instance.serializers import InstanceSerializer, FlavorSerializer from instance.serializers import InstanceSerializer, FlavorSerializer, LeaseSerializer
from django.http import Http404 from django.http import Http404
from django.conf import settings from django.conf import settings
from rest_framework.viewsets import ViewSet from rest_framework.viewsets import ViewSet, ModelViewSet
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from interface_openstack.implementation.vm.instance import ( from interface_openstack.implementation.vm.instance import (
...@@ -106,3 +106,8 @@ class FlavorViewSet(ViewSet): ...@@ -106,3 +106,8 @@ class FlavorViewSet(ViewSet):
flavor = self.get_object(pk) flavor = self.get_object(pk)
flavor.delete() flavor.delete()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
class LeaseViewSet(ModelViewSet):
queryset = Lease.objects.all()
serializer_class = LeaseSerializer
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment