Commit b89540e8 by Bodor Máté

Merge branch 'refactor_template' into 'DEV'

Refactor template models, urls and views

See merge request !19
parents 19df44bb 46d067b1
Pipeline #847 passed with stage
in 1 minute 20 seconds
...@@ -8,71 +8,53 @@ from interface_openstack.implementation.storage.openstack_snapshot_manager impor ...@@ -8,71 +8,53 @@ from interface_openstack.implementation.storage.openstack_snapshot_manager impor
class BaseTemplate(models.Model): class BaseTemplate(models.Model):
"""Virtual machine template. """Virtual machine template.
""" """
name = models.CharField( name = models.CharField(max_length=100,
max_length=100, verbose_name="name",
verbose_name="name", help_text="Human readable name of template.")
help_text="Human readable name of template." description = models.TextField(verbose_name="description",
) blank=True,
description = models.TextField( help_text="Description of the template.")
verbose_name="description", created_at = models.DateTimeField(auto_now_add=True,
blank=True, editable=False,
help_text="Description of the template." help_text="Date, when the template created.")
) created_by = models.ForeignKey(User,
# remote_id = models.CharField( on_delete=models.DO_NOTHING,
# max_length=40, related_name="created_templates",
# unique=True, help_text="The user, who create the template")
# verbose_name="remote_ID", flavor = models.ForeignKey(Flavor,
# help_text="ID, which helps access the template." help_text="Resources given to the vm",
# ) verbose_name="flavor",
created_at = models.DateTimeField( on_delete="CASCADE",
auto_now_add=True,
editable=False,
help_text="Date, when the template created."
)
created_by = models.ForeignKey(
User,
on_delete=models.DO_NOTHING,
related_name="created_templates",
help_text="The user, who create the template"
)
flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm",
verbose_name="flavor", on_delete="CASCADE",
related_name='templates') related_name='templates')
lease = models.ForeignKey(Lease, on_delete="CASCADE", lease = models.ForeignKey(Lease,
on_delete="CASCADE",
related_name='templates') related_name='templates')
network_id = models.CharField( network_id = models.CharField(max_length=100,
max_length=100, verbose_name="network_id",
verbose_name="network_id", help_text="The new instance will be in this network.",
help_text="The new instance will be in this network.", null=True,
null=True, blank=True)
blank=True
)
class DiskTemplate(BaseTemplate): class DiskTemplate(BaseTemplate):
disk = models.ForeignKey( disk = models.ForeignKey(Disk,
Disk, related_name="templates",
related_name="templates", on_delete=models.CASCADE,
on_delete=models.CASCADE, help_text="The disk where the template is located.")
help_text="The disk where the template is located."
)
@classmethod @classmethod
def create_from_volume(cls, name, description, disk, user): def create_from_volume(cls, name, description, disk, user):
interface = SnapshotManager(settings.CONNECTION) interface = SnapshotManager(settings.CONNECTION)
remote_template = interface.create_from_volume(disk.remote_id) remote_template = interface.create_from_volume(disk.remote_id)
remote_id = remote_template.id remote_id = remote_template.id
new_template = cls.create( new_template = DiskTemplate.objects.create(name=name,
name=name, description=description,
description=description, disk=disk,
disk=disk, remote_id=remote_id,
remote_id=remote_id, created_by=user)
created_by=user
)
return new_template return new_template
...@@ -82,33 +64,22 @@ class ImageTemplate(BaseTemplate): ...@@ -82,33 +64,22 @@ class ImageTemplate(BaseTemplate):
('I', 'Template created from instance'), ('I', 'Template created from instance'),
('P', '"Pure" template'), ('P', '"Pure" template'),
) )
image = models.ForeignKey(Image,
image = models.ForeignKey( related_name="templates",
Image, on_delete=models.CASCADE,
related_name="templates", help_text="")
on_delete=models.CASCADE, type = models.CharField(max_length=10,
help_text="" choices=TYPES,
) default="U")
type = models.CharField(max_length=10, choices=TYPES, default="U")
@classmethod
def create(cls, name, description, image, lease, flavor, created_by, type='U'):
inst = cls(name=name, description=description, image=image, lease=lease,
flavor=flavor, created_by=created_by, type=type)
inst.full_clean()
inst.save()
return inst
@classmethod @classmethod
def create_from_instance(cls, name, description, instance, user): def create_from_instance(cls, name, description, instance, user):
image = Image.create_from_instance(user, instance, description) image = Image.create_from_instance(user, instance, description)
new_template = cls.create( new_template = ImageTemplate.objects.create(name=name,
name=name, description=description,
description=description, created_by=user,
created_by=user, image=image,
image=image, lease=instance.lease,
lease=instance.lease, flavor=instance.flavor,
flavor=instance.flavor, type="I")
type="I"
)
return new_template return new_template
from rest_framework import routers from rest_framework import routers
from template import views from template import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
......
# from django.shortcuts import render
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from template.serializers import ImageTemplateModelSerializer from template.serializers import ImageTemplateModelSerializer
from template.models import ImageTemplate from template.models import ImageTemplate
......
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