2020-02-28 13:46:33 +00:00
|
|
|
from rest_framework import viewsets, permissions
|
2020-03-03 09:51:16 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from django.db import transaction
|
2020-04-17 08:08:33 +00:00
|
|
|
from django.utils import timezone
|
2020-02-28 13:46:33 +00:00
|
|
|
|
2020-04-17 08:08:33 +00:00
|
|
|
from .models import *
|
|
|
|
from .serializers import *
|
2020-02-28 13:46:33 +00:00
|
|
|
|
2020-02-28 13:57:45 +00:00
|
|
|
from uncloud_pay.helpers import ProductViewSet
|
2020-03-03 09:51:16 +00:00
|
|
|
from uncloud_pay.models import Order
|
2020-03-09 15:37:56 +00:00
|
|
|
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
|
|
|
|
|
2020-02-28 13:57:45 +00:00
|
|
|
|
|
|
|
class MatrixServiceProductViewSet(ProductViewSet):
|
2020-02-28 13:46:33 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = MatrixServiceProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return MatrixServiceProduct.objects.filter(owner=self.request.user)
|
2020-02-28 13:57:45 +00:00
|
|
|
|
2020-03-03 09:51:16 +00:00
|
|
|
@transaction.atomic
|
2020-02-28 13:46:33 +00:00
|
|
|
def create(self, request):
|
2020-03-03 09:51:16 +00:00
|
|
|
# Extract serializer data.
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
order_recurring_period = serializer.validated_data.pop("recurring_period")
|
|
|
|
|
2020-03-09 15:37:56 +00:00
|
|
|
# Create base order.)
|
2020-03-03 09:51:16 +00:00
|
|
|
order = Order.objects.create(
|
|
|
|
recurring_period=order_recurring_period,
|
2020-04-17 08:08:33 +00:00
|
|
|
owner=request.user,
|
|
|
|
starting_date=timezone.now()
|
|
|
|
)
|
|
|
|
order.save()
|
2020-03-03 09:51:16 +00:00
|
|
|
|
|
|
|
# Create unerderlying VM.
|
2020-03-09 15:37:56 +00:00
|
|
|
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())
|
2020-03-03 09:51:16 +00:00
|
|
|
|
|
|
|
# Create service.
|
|
|
|
service = serializer.save(
|
|
|
|
order=order,
|
2020-03-09 15:37:56 +00:00
|
|
|
owner=request.user,
|
2020-03-03 09:51:16 +00:00
|
|
|
vm=vm)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|
2020-04-17 08:08:33 +00:00
|
|
|
|
|
|
|
class GenericServiceProductViewSet(ProductViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = GenericServiceProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return GenericServiceProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def create(self, request):
|
|
|
|
# Extract serializer data.
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
order_recurring_period = serializer.validated_data.pop("recurring_period")
|
|
|
|
|
|
|
|
# Create base order.
|
|
|
|
order = Order.objects.create(
|
|
|
|
recurring_period=order_recurring_period,
|
|
|
|
owner=request.user,
|
|
|
|
starting_date=timezone.now()
|
|
|
|
)
|
|
|
|
order.save()
|
|
|
|
|
|
|
|
# Create service.
|
|
|
|
print(serializer.validated_data)
|
|
|
|
service = serializer.save(order=order, owner=request.user)
|
|
|
|
|
|
|
|
# XXX: Move this to some kind of on_create hook in parent
|
|
|
|
# Product class?
|
|
|
|
order.add_record(
|
|
|
|
service.one_time_price,
|
|
|
|
service.recurring_price,
|
|
|
|
service.description)
|
|
|
|
|
|
|
|
# XXX: Move this to some kind of on_create hook in parent
|
|
|
|
# Product class?
|
|
|
|
order.add_record(
|
|
|
|
service.one_time_price,
|
|
|
|
service.recurring_price,
|
|
|
|
service.description)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|