From a4cc4304f9566e79c70b82eae707c8106fa5c173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 9 Mar 2020 16:37:56 +0100 Subject: [PATCH] Adapt managed service to create VMDiskProduct --- .../uncloud/uncloud_vm/serializers.py | 8 +++- .../uncloud/uncloud_vm/views.py | 1 - .../uncloud/ungleich_service/models.py | 7 ++- .../uncloud/ungleich_service/views.py | 44 ++++++++++++++----- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/uncloud_django_based/uncloud/uncloud_vm/serializers.py b/uncloud_django_based/uncloud/uncloud_vm/serializers.py index dbc9692..9435de2 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_vm/serializers.py @@ -36,6 +36,11 @@ class CreateVMDiskProductSerializer(serializers.ModelSerializer): model = VMDiskProduct fields = ['size_in_gb', 'image'] +class CreateManagedVMDiskProductSerializer(serializers.ModelSerializer): + class Meta: + model = VMDiskProduct + fields = ['size_in_gb'] + class VMDiskImageProductSerializer(serializers.ModelSerializer): class Meta: model = VMDiskImageProduct @@ -60,9 +65,10 @@ class ManagedVMProductSerializer(serializers.ModelSerializer): """ Managed VM serializer used in ungleich_service app. """ + primary_disk = CreateManagedVMDiskProductSerializer() class Meta: model = VMProduct - fields = [ 'cores', 'ram_in_gb'] + fields = [ 'cores', 'ram_in_gb', 'primary_disk'] class VMSnapshotProductSerializer(serializers.ModelSerializer): class Meta: diff --git a/uncloud_django_based/uncloud/uncloud_vm/views.py b/uncloud_django_based/uncloud/uncloud_vm/views.py index 4efb013..50e2e66 100644 --- a/uncloud_django_based/uncloud/uncloud_vm/views.py +++ b/uncloud_django_based/uncloud/uncloud_vm/views.py @@ -89,7 +89,6 @@ class VMDiskProductViewSet(viewsets.ModelViewSet): if size_in_gb < serializer.validated_data['image'].size_in_gb: raise ValidationError(detail={ 'error_mesage': 'Size is smaller than original image' }) - serializer.save(owner=request.user, size_in_gb=size_in_gb) return Response(serializer.data) diff --git a/uncloud_django_based/uncloud/ungleich_service/models.py b/uncloud_django_based/uncloud/ungleich_service/models.py index 9d6a8ac..4f26a99 100644 --- a/uncloud_django_based/uncloud/ungleich_service/models.py +++ b/uncloud_django_based/uncloud/ungleich_service/models.py @@ -2,7 +2,7 @@ import uuid from django.db import models from uncloud_pay.models import Product, RecurringPeriod -from uncloud_vm.models import VMProduct +from uncloud_vm.models import VMProduct, VMDiskImageProduct class MatrixServiceProduct(Product): monthly_managment_fee = 20 @@ -21,6 +21,11 @@ class MatrixServiceProduct(Product): else: raise Exception('Invalid recurring period for VM Product pricing.') + @staticmethod + def base_image(): + # TODO: find a way to safely reference debian 10 image. + return VMDiskImageProduct.objects.get(uuid="93e564c5-adb3-4741-941f-718f76075f02") + @staticmethod def allowed_recurring_periods(): return list(filter( diff --git a/uncloud_django_based/uncloud/ungleich_service/views.py b/uncloud_django_based/uncloud/ungleich_service/views.py index 9a7ff28..e25f3a5 100644 --- a/uncloud_django_based/uncloud/ungleich_service/views.py +++ b/uncloud_django_based/uncloud/ungleich_service/views.py @@ -7,7 +7,28 @@ from .serializers import MatrixServiceProductSerializer from uncloud_pay.helpers import ProductViewSet from uncloud_pay.models import Order -from uncloud_vm.models import VMProduct +from uncloud_vm.models import VMProduct, VMDiskProduct + +def create_managed_vm(cores, ram, disk_size, image, order): + # Create VM + disk = VMDiskProduct( + owner=order.owner, + order=order, + size_in_gb=disk_size, + image=image) + vm = VMProduct( + name="Managed Service Host", + owner=order.owner, + cores=cores, + ram_in_gb=ram, + primary_disk=disk) + disk.vm = vm + + vm.save() + disk.save() + + return vm + class MatrixServiceProductViewSet(ProductViewSet): permission_classes = [permissions.IsAuthenticated] @@ -23,25 +44,24 @@ class MatrixServiceProductViewSet(ProductViewSet): serializer.is_valid(raise_exception=True) order_recurring_period = serializer.validated_data.pop("recurring_period") - # Create base order. + # Create base order.) order = Order.objects.create( recurring_period=order_recurring_period, - owner=request.user - ) - order.save() + owner=request.user) # Create unerderlying VM. - # TODO: move this logic to a method for use with other - # products. - vm_data = serializer.validated_data.pop('vm') - vm_data['owner'] = request.user - vm_data['order'] = order - vm = VMProduct.objects.create(**vm_data) + data = serializer.validated_data.pop('vm') + vm = create_managed_vm( + order=order, + cores=data['cores'], + ram=data['ram_in_gb'], + disk_size=data['primary_disk']['size_in_gb'], + image=MatrixServiceProduct.base_image()) # Create service. service = serializer.save( order=order, - owner=self.request.user, + owner=request.user, vm=vm) return Response(serializer.data)