Commit 15057b46 by Chif Gergő

Add Flavor create method, fix instance create

parent 2b3c0497
...@@ -6,6 +6,7 @@ verify_ssl = true ...@@ -6,6 +6,7 @@ verify_ssl = true
[dev-packages] [dev-packages]
httpie = "*" httpie = "*"
django-rest-swagger = "*" django-rest-swagger = "*"
coverage = "*"
[packages] [packages]
django = "*" django = "*"
......
from django.contrib import admin from django.contrib import admin
from instance.models import Instance from instance.models import Instance, Flavor, Lease
admin.site.register(Instance) admin.site.register(Instance)
admin.site.register(Flavor)
admin.site.register(Lease)
...@@ -2,8 +2,9 @@ from django.db import models ...@@ -2,8 +2,9 @@ from django.db import models
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from datetime import timedelta
from image.models import Disk from image.models import Disk
from implementation.vm.instance import OSVirtualMachineManager from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
import openstack import openstack
...@@ -37,6 +38,19 @@ class Flavor(models.Model): ...@@ -37,6 +38,19 @@ class Flavor(models.Model):
initial_disk = models.IntegerField(blank=True, null=True) initial_disk = models.IntegerField(blank=True, null=True)
priority = models.IntegerField(blank=True, null=True) priority = models.IntegerField(blank=True, null=True)
@classmethod
def create(cls, name, description, ram=0, vcpu=0,
initial_disk=0, priority=0):
conn = openstack.connect(cloud="openstack")
interface = OSVirtualMachineManager(conn)
remote_flavor = interface.create_flavor(name, ram, vcpu, initial_disk)
flavor = cls(name=name, description=description,
remote_id=remote_flavor.id, ram=ram, vcpu=vcpu,
initial_disk=initial_disk, priority=priority)
flavor.save()
return flavor
class Instance(models.Model): class Instance(models.Model):
"""Virtual machine instance. """Virtual machine instance.
...@@ -84,8 +98,10 @@ class Instance(models.Model): ...@@ -84,8 +98,10 @@ class Instance(models.Model):
owner = models.ForeignKey(User, on_delete="CASCADE", null=True) owner = models.ForeignKey(User, on_delete="CASCADE", null=True)
flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm", flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm",
verbose_name="flavor", on_delete="CASCADE") verbose_name="flavor", on_delete="CASCADE",
lease = models.ForeignKey(Lease, on_delete="CASCADE") related_name='instances')
lease = models.ForeignKey(Lease, on_delete="CASCADE",
related_name='instances')
@classmethod @classmethod
def create(cls, lease, owner, flavor, remote_id, params): def create(cls, lease, owner, flavor, remote_id, params):
...@@ -94,6 +110,8 @@ class Instance(models.Model): ...@@ -94,6 +110,8 @@ class Instance(models.Model):
inst.full_clean() inst.full_clean()
inst.save() inst.save()
return inst
@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):
...@@ -103,7 +121,6 @@ class Instance(models.Model): ...@@ -103,7 +121,6 @@ class Instance(models.Model):
template.remote_ID, template.remote_ID,
flavor.remote_id, flavor.remote_id,
networks, networks,
# disks
) )
remote_id = remote_inst.id remote_id = remote_inst.id
new_inst = cls.create(lease, owner, flavor, remote_id, params) new_inst = cls.create(lease, owner, flavor, remote_id, params)
...@@ -111,7 +128,7 @@ class Instance(models.Model): ...@@ -111,7 +128,7 @@ class Instance(models.Model):
return new_inst return new_inst
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
# self.time_of_suspend, self.time_of_delete = self.get_renew_times() self.time_of_suspend, self.time_of_delete = self.get_renew_times()
super(Instance, self).clean(*args, **kwargs) super(Instance, self).clean(*args, **kwargs)
def get_renew_times(self, lease=None): def get_renew_times(self, lease=None):
...@@ -120,5 +137,8 @@ class Instance(models.Model): ...@@ -120,5 +137,8 @@ class Instance(models.Model):
if lease is None: if lease is None:
lease = self.lease lease = self.lease
return ( return (
timezone.now() + lease.suspend_interval_in_sec, timezone.now() + timedelta(
timezone.now() + lease.delete_interval_in_sec) milliseconds=lease.suspend_interval_in_sec),
timezone.now() + timedelta(
milliseconds=lease.delete_interval_in_sec)
)
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