Commit f99a7459 by Chif Gergő

Modify serializers add delete signal to instances

parent 407d7029
...@@ -4,7 +4,9 @@ from django.contrib.auth.models import User ...@@ -4,7 +4,9 @@ from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from datetime import timedelta from datetime import timedelta
from image.models import Disk from image.models import Disk
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
import openstack import openstack
...@@ -115,8 +117,8 @@ class Instance(models.Model): ...@@ -115,8 +117,8 @@ class Instance(models.Model):
@classmethod @classmethod
def create_instance_from_template(cls, params, template, owner, lease, def create_instance_from_template(cls, params, template, owner, lease,
disks, networks, flavor): disks, networks, flavor):
conn = openstack.connect(cloud="openstack") # TODO: attach disks when the remote instance created
interface = OSVirtualMachineManager(conn) interface = OSVirtualMachineManager(settings.CONNECTION)
remote_inst = interface.create_vm_from_template(params["name"], remote_inst = interface.create_vm_from_template(params["name"],
template.remote_ID, template.remote_ID,
flavor.remote_id, flavor.remote_id,
...@@ -138,7 +140,7 @@ class Instance(models.Model): ...@@ -138,7 +140,7 @@ class Instance(models.Model):
lease = self.lease lease = self.lease
return ( return (
timezone.now() + timedelta( timezone.now() + timedelta(
milliseconds=lease.suspend_interval_in_sec), seconds=lease.suspend_interval_in_sec),
timezone.now() + timedelta( timezone.now() + timedelta(
milliseconds=lease.delete_interval_in_sec) seconds=lease.delete_interval_in_sec)
) )
from rest_framework import serializers from rest_framework import serializers
from .models import Flavor from .models import Flavor, Instance
class InstanceSerializer(serializers.Serializer): class InstanceSerializer(serializers.ModelSerializer):
name = serializers.CharField() lease = serializers.PrimaryKeyRelatedField(read_only=True)
description = serializers.CharField() flavor = serializers.PrimaryKeyRelatedField(read_only=True)
system = serializers.CharField()
lease = serializers.RelatedField(read_only=True) class Meta:
flavor = serializers.RelatedField(read_only=True) model = Instance
fields = ("name", "description", "system", "lease", "flavor")
class FlavorSerializer(serializers.ModelSerializer):
class Meta: class FlavorSerializer(serializers.ModelSerializer):
model = Flavor class Meta:
fields = '__all__' model = Flavor
fields = "__all__"
from django.dispatch import receiver
from django.db.models.signals import pre_delete
from django.conf import settings
from .models import Instance
from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
@receiver(pre_delete, sender=Instance)
def delete_remote_instance(sender, instance, **kwargs):
interface = OSVirtualMachineManager(settings.CONNECTION)
interface.destroy_vm(instance.remote_id)
...@@ -4,7 +4,9 @@ from django.http import Http404 ...@@ -4,7 +4,9 @@ from django.http import Http404
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from implementation.vm.instance import OSVirtualMachineManager from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
from template.models import InstanceTemplate from template.models import InstanceTemplate
from instance.models import Instance, Flavor, Lease from instance.models import Instance, Flavor, Lease
from django.contrib.auth.models import User from django.contrib.auth.models import User
......
Subproject commit e01d873c78ac17fed0438936f979de3cbaca6a5e Subproject commit 068a7155cd97ba5c6d0a06b9c0a6274b22fe8d3c
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