uncloud-mravi/uncloud/uncloud_vm/views.py
Nico Schottelius bcbd6f6f83 Introduce disk->image relationship
Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
2020-02-29 16:45:52 +01:00

97 lines
3.5 KiB
Python

from django.shortcuts import render
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, permissions
from rest_framework.response import Response
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
from uncloud_pay.models import Order
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer, VMDiskImageProductSerializer
import datetime
class VMHostViewSet(viewsets.ModelViewSet):
serializer_class = VMHostSerializer
queryset = VMHost.objects.all()
permission_classes = [permissions.IsAdminUser]
class VMDiskImageProductMineViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer
def get_queryset(self):
return VMDiskImageProduct.objects.filter(owner=self.request.user)
class VMDiskImageProductPublicViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer
def get_queryset(self):
return VMDiskImageProduct.objects.filter(is_public=True)
class VMProductViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMProductSerializer
def get_queryset(self):
return VMProduct.objects.filter(owner=self.request.user)
def create(self, request):
serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
# Create order
now = datetime.datetime.now()
order = Order(owner=request.user,
creation_date=now,
starting_date=now,
recurring_price=20,
one_time_price=0,
recurring_period="per_month")
order.save()
serializer.save(owner=request.user, order=order)
return Response(serializer.data)
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMSnapshotProductSerializer
def get_queryset(self):
return VMSnapshotProduct.objects.filter(owner=self.request.user)
def create(self, request):
serializer = VMSnapshotProductSerializer(data=request.data, context={'request': request})
# This verifies that the VM belongs to the request user
serializer.is_valid(raise_exception=True)
disks = VMDiskProduct.objects.filter(vm=serializer.validated_data['vm'])
ssds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'ssd'])
hdds_size = sum([d.size_in_gb for d in disks if d.storage_class == 'hdd'])
recurring_price = serializer.pricing['per_gb_ssd'] * ssds_size + serializer.pricing['per_gb_hdd'] * hdds_size
recurring_period = serializer.pricing['recurring_period']
# Create order
now = datetime.datetime.now()
order = Order(owner=request.user,
creation_date=now,
starting_date=now,
recurring_price=recurring_price,
one_time_price=0,
recurring_period=recurring_period)
order.save()
serializer.save(owner=request.user,
order=order,
gb_ssd=ssds_size,
gb_hdd=hdds_size)
return Response(serializer.data)