Commit 47e13094 by Bodor Máté

Add create_from_instace method to template and image. Create template serializer…

Add create_from_instace method to template and image. Create template serializer and create_from_instance view
parent 736b1c95
# from django.contrib import admin
from django.contrib import admin
# Register your models here.
from image.models import Image
admin.site.register(Image)
# Generated by Django 2.2.3 on 2019-07-16 11:51
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('image', '0002_image'),
]
operations = [
migrations.RenameField(
model_name='disk',
old_name='remote_ID',
new_name='remote_id',
),
migrations.RenameField(
model_name='image',
old_name='remote_ID',
new_name='remote_id',
),
migrations.AddField(
model_name='image',
name='created_by',
field=models.ForeignKey(default=0, help_text='The user, who create the image', on_delete=django.db.models.deletion.DO_NOTHING, related_name='created_images', to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
migrations.AddField(
model_name='image',
name='uploaded_by_user',
field=models.BooleanField(default=True, editable=False, help_text='The field is false if the image created from instance'),
),
]
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
class Disk(models.Model):
......@@ -9,7 +13,7 @@ class Disk(models.Model):
name = models.CharField(
blank=True, max_length=100, verbose_name="name", help_text="Name of the disk"
)
remote_ID = models.CharField(
remote_id = models.CharField(
max_length=40,
unique=True,
verbose_name="remote_ID",
......@@ -32,7 +36,7 @@ class Image(models.Model):
blank=True,
help_text="Description of the image."
)
remote_ID = models.CharField(
remote_id = models.CharField(
max_length=40,
unique=True,
verbose_name="remote_ID",
......@@ -43,3 +47,39 @@ class Image(models.Model):
editable=False,
help_text="Date, when the image created."
)
uploaded_by_user = models.BooleanField(
default=True,
editable=False,
help_text="The field is false if the image created from instance"
)
created_by = models.ForeignKey(
User,
on_delete=models.DO_NOTHING,
related_name="created_images",
help_text="The user, who create the image"
)
@classmethod
def create(cls, name, description, remote_id, uploaded_by_user, created_by):
inst = cls(name=name, description=description, uploaded_by_user=uploaded_by_user,
remote_id=remote_id, created_by=created_by)
inst.full_clean()
inst.save()
return inst
@classmethod
def create_from_instance(cls, user, instance_remote_id, description):
interface = OSVirtualMachineManager(settings.CONNECTION)
remote_image = interface.create_image(instance_remote_id)
remote_id = remote_image.id
name = remote_image.name
new_image = cls.create(
name=name,
remote_id=remote_id,
created_by=user,
uploaded_by_user=False,
description=description
)
return new_image
......@@ -7,7 +7,7 @@ from rest_framework import status
from interface_openstack.implementation.vm.instance import (
OSVirtualMachineManager
)
from template.models import PureTemplate
from template.models import ImageTemplate
from instance.models import Instance, Flavor, Lease
......@@ -18,7 +18,7 @@ class InstanceList(APIView):
def post(self, request, format=None):
data = request.data
template = InstanceTemplate.objects.get(pk=data["template"])
template = ImageTemplate.objects.get(pk=data["template"])
flavor = Flavor.objects.get(pk=data["flavor"])
lease = Lease.objects.get(pk=data["lease"])
......
Subproject commit 7b2647531e54da62fcf1db4c61a5e0530e977da4
Subproject commit 5d4711571dd4cad9cda386b3ed65a802ed143407
......@@ -23,8 +23,10 @@ schema_view = get_swagger_view(title="RECIRCLE API")
urlpatterns = [
path("images/", include("image.urls")),
path("api/v1/", include("instance.urls")),
path("api/v1/", include("template.urls")),
path("admin/", admin.site.urls),
re_path(r"^auth/", include("djoser.urls")),
re_path(r"^auth/", include("djoser.urls.authtoken")),
path(r"swagger", schema_view),
]
\ No newline at end of file
# from django.contrib import admin
from django.contrib import admin
# Register your models here.
from template.models import ImageTemplate
admin.site.register(ImageTemplate)
# Generated by Django 2.2.3 on 2019-07-16 11:51
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('image', '0003_auto_20190716_1151'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('template', '0002_auto_20190715_1354'),
]
operations = [
migrations.RenameModel(
old_name='Template',
new_name='DiskTemplate',
),
migrations.RenameModel(
old_name='PureTemplate',
new_name='ImageTemplate',
),
migrations.RemoveField(
model_name='basetemplate',
name='remote_ID',
),
migrations.AlterField(
model_name='basetemplate',
name='created_by',
field=models.ForeignKey(help_text='The user, who create the template', on_delete=django.db.models.deletion.DO_NOTHING, related_name='created_templates', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 2.2.3 on 2019-07-16 12:27
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('template', '0003_auto_20190716_1151'),
]
operations = [
migrations.RenameField(
model_name='imagetemplate',
old_name='images',
new_name='image',
),
]
# Generated by Django 2.2.3 on 2019-07-17 09:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('instance', '0009_auto_20190715_1354'),
('template', '0004_auto_20190716_1227'),
]
operations = [
migrations.AddField(
model_name='basetemplate',
name='flavor',
field=models.ForeignKey(default=1, help_text='Reasources given to the vm', on_delete='CASCADE', related_name='templates', to='instance.Flavor', verbose_name='flavor'),
preserve_default=False,
),
migrations.AddField(
model_name='basetemplate',
name='lease',
field=models.ForeignKey(default=1, on_delete='CASCADE', related_name='templates', to='instance.Lease'),
preserve_default=False,
),
migrations.AddField(
model_name='imagetemplate',
name='type',
field=models.CharField(choices=[('U', 'User create the template from image'), ('I', 'Template created from instance'), ('D', 'Default "Pure" template')], default='I', max_length=10),
preserve_default=False,
),
]
......@@ -4,7 +4,10 @@ from django.conf import settings
from image.models import Disk
from image.models import Image
from instance.models import Lease
from instance.models import Flavor
from interface_openstack.implementation.storage.openstack_snapshot_manager import SnapshotManager
from interface_openstack.implementation.vm.instance import OSVirtualMachineManager
class BaseTemplate(models.Model):
......@@ -22,12 +25,12 @@ class BaseTemplate(models.Model):
blank=True,
help_text="Description of the template."
)
remote_ID = models.CharField(
max_length=40,
unique=True,
verbose_name="remote_ID",
help_text="ID, which helps access the template."
)
# remote_id = models.CharField(
# max_length=40,
# unique=True,
# verbose_name="remote_ID",
# help_text="ID, which helps access the template."
# )
created_at = models.DateTimeField(
auto_now_add=True,
editable=False,
......@@ -36,15 +39,17 @@ class BaseTemplate(models.Model):
created_by = models.ForeignKey(
User,
on_delete=models.DO_NOTHING,
related_name=("created_templates")
related_name="created_templates",
help_text="The user, who create the template"
)
# lease
# flavour
flavor = models.ForeignKey(Flavor, help_text="Reasources given to the vm",
verbose_name="flavor", on_delete="CASCADE",
related_name='templates')
lease = models.ForeignKey(Lease, on_delete="CASCADE",
related_name='templates')
class Template(BaseTemplate):
class DiskTemplate(BaseTemplate):
disk = models.ForeignKey(
Disk,
related_name="templates",
......@@ -55,21 +60,52 @@ class Template(BaseTemplate):
@classmethod
def create_from_volume(cls, name, description, disk, user):
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
new_template = cls.object.create(
new_template = cls.create(
name=name,
description=description,
disk=disk,
remote_ID=remote_id,
remote_id=remote_id,
created_by=user
)
return new_template
class PureTemplate(BaseTemplate):
images = models.ForeignKey(
class ImageTemplate(BaseTemplate):
TYPES = (
('U', 'User create the template from image'),
('I', 'Template created from instance'),
('D', 'Default "Pure" template'),
)
image = models.ForeignKey(
Image,
related_name="templates",
on_delete=models.CASCADE
on_delete=models.CASCADE,
help_text=""
)
type = models.CharField(max_length=10, choices=TYPES)
@classmethod
def create(cls, name, description, image, lease, flavor, created_by):
inst = cls(name=name, description=description, image=image, lease=lease, flavor=flavor, created_by=created_by)
inst.full_clean()
inst.save()
return inst
@classmethod
def create_from_instance(cls, name, description, instance_remote_id, lease, flavor, user):
image = Image.create_from_instance(user, instance_remote_id, description)
new_template = cls.create(
name=name,
description=description,
created_by=user,
image = image,
lease=lease,
flavor=flavor
)
return new_template
from rest_framework import serializers
from .models import InstanceTemplate
from template.models import ImageTemplate
class InstanceTemplateSerializer(serializers.ModelSerializer):
class InstanceTemplateSerializer(serializers.Serializer):
name = serializers.CharField()
description = serializers.CharField()
instance_remote_id = serializers.CharField()
class ImageTemplateModelSerializer(serializers.ModelSerializer):
class Meta:
model = InstanceTemplate
fields = ("name", "description", "owner", "remote_ID")
model = ImageTemplate
fields = ("name", "description", "created_at", "created_by", "image")
# from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from django.http import HttpResponse
# Create your views here.
from template.serializers import InstanceTemplateSerializer
from template.serializers import ImageTemplateModelSerializer
from template.models import ImageTemplate
class TemplateList(APIView):
def post(self, request, format=None):
serializer = InstanceTemplateSerializer(data=request.data)
serializer.is_valid()
data = serializer.validated_data
new_template = ImageTemplate.create_from_instance(data["name"], data["description"], data["instance_remote_id"], request.user)
serializer = ImageTemplateModelSerializer(instance=new_template)
return Response(serializer.data)
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