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
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:

View file

@ -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)

View file

@ -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(

View file

@ -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)