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
from django.utils import timezone
from datetime import timedelta
from image.models import Disk
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
import openstack
......@@ -115,8 +117,8 @@ class Instance(models.Model):
@classmethod
def create_instance_from_template(cls, params, template, owner, lease,
disks, networks, flavor):
conn = openstack.connect(cloud="openstack")
interface = OSVirtualMachineManager(conn)
# TODO: attach disks when the remote instance created
interface = OSVirtualMachineManager(settings.CONNECTION)
remote_inst = interface.create_vm_from_template(params["name"],
template.remote_ID,
flavor.remote_id,
......@@ -138,7 +140,7 @@ class Instance(models.Model):
lease = self.lease
return (
timezone.now() + timedelta(
milliseconds=lease.suspend_interval_in_sec),
seconds=lease.suspend_interval_in_sec),
timezone.now() + timedelta(
milliseconds=lease.delete_interval_in_sec)
seconds=lease.delete_interval_in_sec)
)
from rest_framework import serializers
from .models import Flavor
from .models import Flavor, Instance
class InstanceSerializer(serializers.Serializer):
name = serializers.CharField()
description = serializers.CharField()
system = serializers.CharField()
lease = serializers.RelatedField(read_only=True)
flavor = serializers.RelatedField(read_only=True)
class InstanceSerializer(serializers.ModelSerializer):
lease = serializers.PrimaryKeyRelatedField(read_only=True)
flavor = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Instance
fields = ("name", "description", "system", "lease", "flavor")
class FlavorSerializer(serializers.ModelSerializer):
class Meta:
model = Flavor
fields = '__all__'
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
from rest_framework.views import APIView
from rest_framework.response import Response
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 instance.models import Instance, Flavor, Lease
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