Adapt managed service to create VMDiskProduct

This commit is contained in:
fnux 2020-03-09 16:37:56 +01:00
parent d3b7470294
commit a4cc4304f9
4 changed files with 45 additions and 15 deletions

View file

@ -36,6 +36,11 @@ class CreateVMDiskProductSerializer(serializers.ModelSerializer):
model = VMDiskProduct model = VMDiskProduct
fields = ['size_in_gb', 'image'] fields = ['size_in_gb', 'image']
class CreateManagedVMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = ['size_in_gb']
class VMDiskImageProductSerializer(serializers.ModelSerializer): class VMDiskImageProductSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = VMDiskImageProduct model = VMDiskImageProduct
@ -60,9 +65,10 @@ class ManagedVMProductSerializer(serializers.ModelSerializer):
""" """
Managed VM serializer used in ungleich_service app. Managed VM serializer used in ungleich_service app.
""" """
primary_disk = CreateManagedVMDiskProductSerializer()
class Meta: class Meta:
model = VMProduct model = VMProduct
fields = [ 'cores', 'ram_in_gb'] fields = [ 'cores', 'ram_in_gb', 'primary_disk']
class VMSnapshotProductSerializer(serializers.ModelSerializer): class VMSnapshotProductSerializer(serializers.ModelSerializer):
class Meta: class Meta:

View file

@ -89,7 +89,6 @@ class VMDiskProductViewSet(viewsets.ModelViewSet):
if size_in_gb < serializer.validated_data['image'].size_in_gb: if size_in_gb < serializer.validated_data['image'].size_in_gb:
raise ValidationError(detail={ 'error_mesage': 'Size is smaller than original image' }) raise ValidationError(detail={ 'error_mesage': 'Size is smaller than original image' })
serializer.save(owner=request.user, size_in_gb=size_in_gb) serializer.save(owner=request.user, size_in_gb=size_in_gb)
return Response(serializer.data) return Response(serializer.data)

View file

@ -2,7 +2,7 @@ import uuid
from django.db import models from django.db import models
from uncloud_pay.models import Product, RecurringPeriod from uncloud_pay.models import Product, RecurringPeriod
from uncloud_vm.models import VMProduct from uncloud_vm.models import VMProduct, VMDiskImageProduct
class MatrixServiceProduct(Product): class MatrixServiceProduct(Product):
monthly_managment_fee = 20 monthly_managment_fee = 20
@ -21,6 +21,11 @@ class MatrixServiceProduct(Product):
else: else:
raise Exception('Invalid recurring period for VM Product pricing.') 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 @staticmethod
def allowed_recurring_periods(): def allowed_recurring_periods():
return list(filter( return list(filter(

View file

@ -7,7 +7,28 @@ from .serializers import MatrixServiceProductSerializer
from uncloud_pay.helpers import ProductViewSet from uncloud_pay.helpers import ProductViewSet
from uncloud_pay.models import Order 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): class MatrixServiceProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
@ -23,25 +44,24 @@ class MatrixServiceProductViewSet(ProductViewSet):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period") order_recurring_period = serializer.validated_data.pop("recurring_period")
# Create base order. # Create base order.)
order = Order.objects.create( order = Order.objects.create(
recurring_period=order_recurring_period, recurring_period=order_recurring_period,
owner=request.user owner=request.user)
)
order.save()
# Create unerderlying VM. # Create unerderlying VM.
# TODO: move this logic to a method for use with other data = serializer.validated_data.pop('vm')
# products. vm = create_managed_vm(
vm_data = serializer.validated_data.pop('vm') order=order,
vm_data['owner'] = request.user cores=data['cores'],
vm_data['order'] = order ram=data['ram_in_gb'],
vm = VMProduct.objects.create(**vm_data) disk_size=data['primary_disk']['size_in_gb'],
image=MatrixServiceProduct.base_image())
# Create service. # Create service.
service = serializer.save( service = serializer.save(
order=order, order=order,
owner=self.request.user, owner=request.user,
vm=vm) vm=vm)
return Response(serializer.data) return Response(serializer.data)