2020-03-02 08:30:51 +00:00
|
|
|
from django.db import transaction
|
2020-02-23 13:07:37 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.shortcuts import get_object_or_404
|
2020-02-25 19:53:12 +00:00
|
|
|
|
|
|
|
from rest_framework import viewsets, permissions
|
2020-02-23 13:07:37 +00:00
|
|
|
from rest_framework.response import Response
|
2020-03-02 06:17:04 +00:00
|
|
|
from rest_framework.exceptions import ValidationError
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-03-22 19:55:11 +00:00
|
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct, VMCluster
|
2020-03-02 06:17:04 +00:00
|
|
|
from uncloud_pay.models import Order
|
|
|
|
|
2020-03-13 09:22:49 +00:00
|
|
|
from .serializers import (VMHostSerializer, VMProductSerializer,
|
|
|
|
VMSnapshotProductSerializer, VMDiskImageProductSerializer,
|
2020-03-22 19:55:11 +00:00
|
|
|
VMDiskProductSerializer, DCLVMProductSerializer,
|
|
|
|
VMClusterSerializer)
|
2020-02-28 13:57:45 +00:00
|
|
|
from uncloud_pay.helpers import ProductViewSet
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-02-27 14:29:15 +00:00
|
|
|
|
2020-02-27 11:31:20 +00:00
|
|
|
import datetime
|
|
|
|
|
2020-02-25 19:53:12 +00:00
|
|
|
class VMHostViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = VMHostSerializer
|
|
|
|
queryset = VMHost.objects.all()
|
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-02-25 21:01:55 +00:00
|
|
|
|
2020-03-22 19:55:11 +00:00
|
|
|
class VMClusterViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = VMClusterSerializer
|
|
|
|
queryset = VMCluster.objects.all()
|
|
|
|
permission_classes = [permissions.IsAdminUser]
|
|
|
|
|
2020-03-22 16:30:55 +00:00
|
|
|
class VMDiskImageProductViewSet(viewsets.ModelViewSet):
|
2020-03-02 06:17:04 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMDiskImageProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-03-17 18:53:14 +00:00
|
|
|
if self.request.user.is_superuser:
|
|
|
|
obj = VMDiskImageProduct.objects.all()
|
|
|
|
else:
|
2020-03-22 16:30:55 +00:00
|
|
|
obj = VMDiskImageProduct.objects.filter(owner=self.request.user) | VMDiskImageProduct.objects.filter(is_public=True)
|
2020-03-17 18:53:14 +00:00
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = VMDiskImageProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
# did not specify size NOR import url?
|
|
|
|
if not serializer.validated_data['size_in_gb']:
|
|
|
|
if not serializer.validated_data['import_url']:
|
|
|
|
raise ValidationError(detail={ 'error_mesage': 'Specify either import_url or size_in_gb' })
|
|
|
|
|
|
|
|
serializer.save(owner=request.user)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
|
class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMDiskImageProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return VMDiskImageProduct.objects.filter(is_public=True)
|
|
|
|
|
|
|
|
class VMDiskProductViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
|
|
Let a user modify their own VMDisks
|
|
|
|
"""
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMDiskProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-03-18 14:43:01 +00:00
|
|
|
if self.request.user.is_superuser:
|
|
|
|
obj = VMDiskProduct.objects.all()
|
|
|
|
else:
|
|
|
|
obj = VMDiskProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
return obj
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = VMDiskProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
# get disk size from image, if not specified
|
|
|
|
if not 'size_in_gb' in serializer.validated_data:
|
|
|
|
size_in_gb = serializer.validated_data['image'].size_in_gb
|
|
|
|
else:
|
|
|
|
size_in_gb = serializer.validated_data['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' })
|
|
|
|
|
|
|
|
|
|
|
|
serializer.save(owner=request.user, size_in_gb=size_in_gb)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
2020-02-27 11:02:41 +00:00
|
|
|
|
2020-02-28 13:57:45 +00:00
|
|
|
class VMProductViewSet(ProductViewSet):
|
2020-02-25 21:01:55 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-03-17 13:50:14 +00:00
|
|
|
if self.request.user.is_superuser:
|
|
|
|
obj = VMProduct.objects.all()
|
|
|
|
else:
|
|
|
|
obj = VMProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
return obj
|
2020-02-25 21:01:55 +00:00
|
|
|
|
2020-03-02 08:30:51 +00:00
|
|
|
# Use a database transaction so that we do not get half-created structure
|
|
|
|
# if something goes wrong.
|
|
|
|
@transaction.atomic
|
2020-02-25 21:01:55 +00:00
|
|
|
def create(self, request):
|
2020-03-03 09:14:56 +00:00
|
|
|
# Extract serializer data.
|
|
|
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
order_recurring_period = serializer.validated_data.pop("recurring_period")
|
|
|
|
|
2020-02-27 19:58:12 +00:00
|
|
|
# Create base order.
|
|
|
|
order = Order.objects.create(
|
2020-03-03 09:14:56 +00:00
|
|
|
recurring_period=order_recurring_period,
|
2020-02-27 19:58:12 +00:00
|
|
|
owner=request.user
|
|
|
|
)
|
2020-03-01 11:23:04 +00:00
|
|
|
order.save()
|
2020-02-27 19:58:12 +00:00
|
|
|
|
|
|
|
# Create VM.
|
|
|
|
vm = serializer.save(owner=request.user, order=order)
|
|
|
|
|
2020-03-02 08:25:03 +00:00
|
|
|
# Add Product record to order (VM is mutable, allows to keep history in order).
|
2020-03-03 09:14:56 +00:00
|
|
|
# XXX: Move this to some kind of on_create hook in parent Product class?
|
2020-03-03 10:27:35 +00:00
|
|
|
order.add_record(vm.one_time_price,
|
2020-03-02 08:25:03 +00:00
|
|
|
vm.recurring_price(order.recurring_period), vm.description)
|
|
|
|
|
2020-02-25 21:01:55 +00:00
|
|
|
return Response(serializer.data)
|
2020-02-27 10:36:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = VMSnapshotProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-03-22 19:55:11 +00:00
|
|
|
if self.request.user.is_superuser:
|
|
|
|
obj = VMSnapshotProduct.objects.all()
|
|
|
|
else:
|
|
|
|
obj = VMSnapshotProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
return obj
|
2020-02-27 10:36:50 +00:00
|
|
|
|
|
|
|
def create(self, request):
|
2020-02-27 11:31:20 +00:00
|
|
|
serializer = VMSnapshotProductSerializer(data=request.data, context={'request': request})
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
# This verifies that the VM belongs to the request user
|
2020-02-27 10:36:50 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2020-03-17 18:53:14 +00:00
|
|
|
vm = vm=serializer.validated_data['vm']
|
|
|
|
disks = VMDiskProduct.objects.filter(vm=vm)
|
|
|
|
ssds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'ssd'])
|
|
|
|
hdds_size = sum([d.size_in_gb for d in disks if d.image.storage_class == 'hdd'])
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
recurring_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size
|
|
|
|
recurring_period = serializer.pricing['recurring_period']
|
|
|
|
|
2020-02-27 11:02:41 +00:00
|
|
|
# Create order
|
2020-02-27 11:31:20 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
order = Order(owner=request.user,
|
2020-03-02 06:17:04 +00:00
|
|
|
recurring_period=recurring_period)
|
2020-02-27 11:31:20 +00:00
|
|
|
order.save()
|
2020-03-17 18:53:14 +00:00
|
|
|
order.add_record(one_time_price=0,
|
|
|
|
recurring_price=recurring_price,
|
|
|
|
description="Snapshot of VM {} from {}".format(vm, now))
|
2020-02-27 11:31:20 +00:00
|
|
|
|
|
|
|
serializer.save(owner=request.user,
|
|
|
|
order=order,
|
2020-03-02 06:17:04 +00:00
|
|
|
gb_ssd=ssds_size,
|
|
|
|
gb_hdd=hdds_size)
|
2020-02-27 10:36:50 +00:00
|
|
|
|
|
|
|
return Response(serializer.data)
|
2020-03-06 10:10:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Also create:
|
|
|
|
# - /dcl/available_os
|
|
|
|
# Basically a view of public and my disk images
|
|
|
|
# -
|
|
|
|
class DCLCreateVMProductViewSet(ProductViewSet):
|
|
|
|
"""
|
|
|
|
This view resembles the way how DCL VMs are created by default.
|
|
|
|
|
|
|
|
The user chooses an OS, os disk size, ram, cpu and whether or not to have a mapped IPv4 address
|
|
|
|
"""
|
|
|
|
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
serializer_class = DCLVMProductSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return VMProduct.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
# Use a database transaction so that we do not get half-created structure
|
|
|
|
# if something goes wrong.
|
|
|
|
@transaction.atomic
|
|
|
|
def create(self, request):
|
|
|
|
# Extract serializer data.
|
|
|
|
serializer = VMProductSerializer(data=request.data, context={'request': request})
|
|
|
|
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
|
|
|
|
)
|
|
|
|
order.save()
|
|
|
|
|
|
|
|
# Create VM.
|
|
|
|
vm = serializer.save(owner=request.user, order=order)
|
|
|
|
|
|
|
|
# Add Product record to order (VM is mutable, allows to keep history in order).
|
|
|
|
# XXX: Move this to some kind of on_create hook in parent Product class?
|
|
|
|
order.add_record(vm.one_time_price,
|
|
|
|
vm.recurring_price(order.recurring_period), vm.description)
|
|
|
|
|
|
|
|
return Response(serializer.data)
|