++ uncloud_v4 test

This commit is contained in:
Nico Schottelius 2022-01-02 13:34:16 +01:00
parent e726795495
commit c2c921663f
10 changed files with 869 additions and 0 deletions

0
uncloud_v4/__init__.py Normal file
View file

19
uncloud_v4/admin.py Normal file
View file

@ -0,0 +1,19 @@
from django.contrib import admin
# Register your models here.
from uncloud_vm.models import *
from uncloud_pay.models import Order
class VMDiskInline(admin.TabularInline):
model = VMDiskProduct
class OrderInline(admin.TabularInline):
model = Order
class VMProductAdmin(admin.ModelAdmin):
inlines = [
VMDiskInline
]
admin.site.register(VMProduct, VMProductAdmin)
admin.site.register(VMDiskProduct)

5
uncloud_v4/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class UncloudVmConfig(AppConfig):
name = 'uncloud_vm'

View file

@ -0,0 +1,119 @@
import json
import uncloud.secrets as secrets
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from uncloud_vm.models import VMSnapshotProduct, VMProduct, VMHost
from datetime import datetime
class Command(BaseCommand):
help = 'Select VM Host for VMs'
def add_arguments(self, parser):
parser.add_argument('--this-hostname', required=True)
parser.add_argument('--this-cluster', required=True)
parser.add_argument('--create-vm-snapshots', action='store_true')
parser.add_argument('--schedule-vms', action='store_true')
parser.add_argument('--start-vms', action='store_true')
def handle(self, *args, **options):
for cmd in [ 'create_vm_snapshots', 'schedule_vms', 'start_vms' ]:
if options[cmd]:
f = getattr(self, cmd)
f(args, options)
def schedule_vms(self, *args, **options):
for pending_vm in VMProduct.objects.filter(status='PENDING'):
cores_needed = pending_vm.cores
ram_needed = pending_vm.ram_in_gb
# Database filtering
possible_vmhosts = VMHost.objects.filter(physical_cores__gte=cores_needed)
# Logical filtering
possible_vmhosts = [ vmhost for vmhost in possible_vmhosts
if vmhost.available_cores >=cores_needed
and vmhost.available_ram_in_gb >= ram_needed ]
if not possible_vmhosts:
log.error("No suitable Host found - cannot schedule VM {}".format(pending_vm))
continue
vmhost = possible_vmhosts[0]
pending_vm.vmhost = vmhost
pending_vm.status = 'SCHEDULED'
pending_vm.save()
print("Scheduled VM {} on VMHOST {}".format(pending_vm, pending_vm.vmhost))
print(self)
def start_vms(self, *args, **options):
vmhost = VMHost.objects.get(hostname=options['this_hostname'])
if not vmhost:
raise Exception("No vmhost {} exists".format(options['vmhostname']))
# not active? done here
if not vmhost.status = 'ACTIVE':
return
vms_to_start = VMProduct.objects.filter(vmhost=vmhost,
status='SCHEDULED')
for vm in vms_to_start:
""" run qemu:
check if VM is not already active / qemu running
prepare / create the Qemu arguments
"""
print("Starting VM {}".format(VM))
def check_vms(self, *args, **options):
"""
Check if all VMs that are supposed to run are running
"""
def modify_vms(self, *args, **options):
"""
Check all VMs that are requested to be modified and restart them
"""
def create_vm_snapshots(self, *args, **options):
this_cluster = VMCluster(option['this_cluster'])
for snapshot in VMSnapshotProduct.objects.filter(status='PENDING',
cluster=this_cluster):
if not snapshot.extra_data:
snapshot.extra_data = {}
# TODO: implement locking here
if 'creating_hostname' in snapshot.extra_data:
pass
snapshot.extra_data['creating_hostname'] = options['this_hostname']
snapshot.extra_data['creating_start'] = str(datetime.now())
snapshot.save()
# something on the line of:
# for disk im vm.disks:
# rbd snap create pool/image-name@snapshot name
# snapshot.extra_data['snapshots']
# register the snapshot names in extra_data (?)
print(snapshot)
def check_health(self, *args, **options):
pending_vms = VMProduct.objects.filter(status='PENDING')
vmhosts = VMHost.objects.filter(status='active')
# 1. Check that all active hosts reported back N seconds ago
# 2. Check that no VM is running on a dead host
# 3. Migrate VMs if necessary
# 4. Check that no VMs have been pending for longer than Y seconds
# If VM snapshots exist without a VM -> notify user (?)
print("Nothing is good, you should implement me")

View file

@ -0,0 +1,111 @@
# Generated by Django 3.1 on 2020-12-13 10:38
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='VMCluster',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('extra_data', models.JSONField(blank=True, editable=False, null=True)),
('name', models.CharField(max_length=128, unique=True)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='VMDiskImageProduct',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('extra_data', models.JSONField(blank=True, editable=False, null=True)),
('name', models.CharField(max_length=256)),
('is_os_image', models.BooleanField(default=False)),
('is_public', models.BooleanField(default=False, editable=False)),
('size_in_gb', models.FloatField(blank=True, null=True)),
('import_url', models.URLField(blank=True, null=True)),
('image_source', models.CharField(max_length=128, null=True)),
('image_source_type', models.CharField(max_length=128, null=True)),
('storage_class', models.CharField(choices=[('HDD', 'HDD'), ('SSD', 'SSD')], default='SSD', max_length=32)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32)),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='VMHost',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('extra_data', models.JSONField(blank=True, editable=False, null=True)),
('hostname', models.CharField(max_length=253, unique=True)),
('physical_cores', models.IntegerField(default=0)),
('usable_cores', models.IntegerField(default=0)),
('usable_ram_in_gb', models.FloatField(default=0)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('AWAITING_PAYMENT', 'Awaiting payment'), ('BEING_CREATED', 'Being created'), ('SCHEDULED', 'Scheduled'), ('ACTIVE', 'Active'), ('MODIFYING', 'Modifying'), ('DELETED', 'Deleted'), ('DISABLED', 'Disabled'), ('UNUSABLE', 'Unusable')], default='PENDING', max_length=32)),
('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='VMProduct',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=32, null=True)),
('cores', models.IntegerField()),
('ram_in_gb', models.FloatField()),
('vmcluster', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmcluster')),
('vmhost', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmhost')),
],
),
migrations.CreateModel(
name='VMSnapshotProduct',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('gb_ssd', models.FloatField(editable=False)),
('gb_hdd', models.FloatField(editable=False)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='snapshots', to='uncloud_vm.vmproduct')),
],
),
migrations.CreateModel(
name='VMNetworkCard',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('mac_address', models.BigIntegerField()),
('ip_address', models.GenericIPAddressField(blank=True, null=True)),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmproduct')),
],
),
migrations.CreateModel(
name='VMDiskProduct',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('size_in_gb', models.FloatField(blank=True)),
('disk_type', models.CharField(choices=[('ceph/ssd', 'Ceph Ssd'), ('ceph/hdd', 'Ceph Hdd'), ('local/ssd', 'Local Ssd'), ('local/hdd', 'Local Hdd')], default='ceph/ssd', max_length=20)),
('image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmdiskimageproduct')),
('vm', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmproduct')),
],
),
migrations.CreateModel(
name='VMWithOSProduct',
fields=[
('vmproduct_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_vm.vmproduct')),
('primary_disk', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='uncloud_vm.vmdiskproduct')),
],
bases=('uncloud_vm.vmproduct',),
),
]

View file

113
uncloud_v4/models.py Normal file
View file

@ -0,0 +1,113 @@
from django.db import models
from django.contrib.auth import get_user_model
# 1st mixin: owner alike?
# 2nd: time frames
class Nextcloudv1(models.Model):
name = models.CharField(max_length=128, unique=True)
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, editable=False
)
domain = models.CharField(max_length=253)
cpu_cores = models.FloatField(null=True, blank=True)
ram_in_gb = models.FloatField(null=True, blank=True)
storage_in_gb = models.FloatField(null=True, blank=True)
def __str__(self):
return f"Nextcloud {domain}: {cores}C {ram_in_gb}GB RAM {storage} GB storage from {owner}"
class Order(models.Model):
# name = models.CharField(max_length=128, unique=True)
owner = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, editable=False
)
creation_date = models.DateTimeField(auto_now_add=True)
starting_date = models.DateTimeField(default=timezone.now)
ending_date = models.DateTimeField(blank=True, null=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
resourceconfigs = models.ManyToManyField(ResourceConfig)
textconfigs = models.ManyToManyField(ResourceConfig)
class Product(models.Model):
"""
Describes a product a user can buy, its pricings and its configurations
"""
name = models.CharField(max_length=128, unique=True) # Nextcloud, Matrix, etc.
textconfig = models.ManyToManyField(ProductTextConfiguration)
textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration)
timeframes = models.ManyToManyField(TimeFrame)
resources = models.ManyToManyField(Resource)
class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM
unit = models.CharField(max_length=128, unique=True) # Count, GB
minimum_units = models.FloatField(null=True, blank=True) # might have min
maximum_units = models.FloatField(null=True, blank=True) # might have max
step_size = models.FloatField(default=1, null=True, blank=True) # might/must step size
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
price = models.FloatField() # Per unit and per time frame
class ResourceConfig(models.Model):
"""
Describes an actual setting of a resource
"""
value = models.FloatField()
resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
class ProductTextConfiguration(models.Model):
name = models.CharField(max_length=128, unique=True) # Domain, etc.
class ProductTextFieldConfiguration(models.Model):
name = models.CharField(max_length=128, unique=True) # homeserver.Yaml, etc.
class ProductTextConfig(models.Model):
"""
Describes an actual setting of a resource
"""
value = models.CharField(max_length=256)
config = models.ForeignKey(ProductTextConfiguration, on_delete=models.CASCADE)
class ProductTextFieldConfig(models.Model):
"""
Describes an actual setting of a resource
"""
value = models.TextField()
config = models.ForeignKey(ProductTextFieldConfiguration, on_delete=models.CASCADE)
class TimeFrame(models.Model):
name = models.CharField(max_length=128, unique=True)
duration_in_seconds = models.IntegerField(null=True, blank=True)
# 3600
# 86400
# 30*86400
# class Nextcloudv2(Product):
# domain = models.CharField(max_length=253)
# def __str__(self):
# return f"Nextcloud {domain} {owner}"
# class ProductPricing(models.Model):
# """
# Defines the allowed time frames and the available resources
# """
# name = models.CharField(max_length=128, unique=True) # cpu 2022, ipv4 2022
# timeframes = models.ManyToManyField(TimeFrame)
# resources = models.ManyToManyField(Resource)

143
uncloud_v4/serializers.py Normal file
View file

@ -0,0 +1,143 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers
from .models import *
from uncloud_pay.models import RecurringPeriod, BillingAddress
# XXX: does not seem to be used?
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
###
# Admin views.
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
vms = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = VMHost
fields = '__all__'
read_only_fields = [ 'vms' ]
class VMClusterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VMCluster
fields = '__all__'
###
# Disks.
class VMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = '__all__'
class CreateVMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
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
fields = '__all__'
class VMSnapshotProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMSnapshotProduct
fields = '__all__'
# verify that vm.owner == user.request
def validate_vm(self, value):
if not value.owner == self.context['request'].user:
raise serializers.ValidationError("VM {} not found for owner {}.".format(value,
self.context['request'].user))
disks = VMDiskProduct.objects.filter(vm=value)
if len(disks) == 0:
raise serializers.ValidationError("VM {} does not have any disks, cannot snapshot".format(value.id))
return value
pricing = {}
pricing['per_gb_ssd'] = 0.012
pricing['per_gb_hdd'] = 0.0006
pricing['recurring_period'] = 'per_day'
###
# VMs
# Helper used in uncloud_service for services allocating VM.
class ManagedVMProductSerializer(serializers.ModelSerializer):
"""
Managed VM serializer used in ungleich_service app.
"""
primary_disk = CreateManagedVMDiskProductSerializer()
class Meta:
model = VMWithOSProduct
fields = [ 'cores', 'ram_in_gb', 'primary_disk']
class VMProductSerializer(serializers.ModelSerializer):
primary_disk = CreateVMDiskProductSerializer()
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
disks = VMDiskProductSerializer(many=True, read_only=True)
class Meta:
model = VMWithOSProduct
fields = ['order', 'owner', 'status', 'name', 'cores',
'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
read_only_fields = ['order', 'owner', 'status']
class OrderVMProductSerializer(VMProductSerializer):
# recurring_period = serializers.ChoiceField(
# choices=VMWithOSProduct.allowed_recurring_periods())
def __init__(self, *args, **kwargs):
super(VMProductSerializer, self).__init__(*args, **kwargs)
class Meta:
model = VMProductSerializer.Meta.model
fields = VMProductSerializer.Meta.fields + [ 'recurring_period' ]
read_only_fields = VMProductSerializer.Meta.read_only_fields
# Nico's playground.
class NicoVMProductSerializer(serializers.ModelSerializer):
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
order = serializers.StringRelatedField()
class Meta:
model = VMProduct
read_only_fields = ['order', 'owner', 'status',
'vmhost', 'vmcluster', 'snapshots',
'extra_data' ]
fields = read_only_fields + [ 'name',
'cores',
'ram_in_gb'
]
class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer):
"""
Create an interface similar to standard DCL
"""
# Custom field used at creation (= ordering) only.
# recurring_period = serializers.ChoiceField(
# choices=VMProduct.allowed_recurring_periods())
os_disk_uuid = serializers.UUIDField()
# os_disk_size =
class Meta:
model = VMProduct

98
uncloud_v4/tests.py Normal file
View file

@ -0,0 +1,98 @@
import datetime
import parsedatetime
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.utils import timezone
from django.core.exceptions import ValidationError
from uncloud_vm.models import VMDiskImageProduct, VMDiskProduct, VMProduct, VMHost
from uncloud_pay.models import Order, RecurringPeriod
User = get_user_model()
cal = parsedatetime.Calendar()
# If you want to check the test database using some GUI/cli tool
# then use the following connecting parameters
# host: localhost
# database: test_uncloud
# user: root
# password:
# port: 5432
class VMTestCase(TestCase):
@classmethod
def setUpClass(cls):
# Setup vm host
cls.vm_host, created = VMHost.objects.get_or_create(
hostname='serverx.placey.ungleich.ch', physical_cores=32, usable_cores=320,
usable_ram_in_gb=512.0, status='active'
)
super().setUpClass()
def setUp(self) -> None:
# Setup two users as it is common to test with different user
self.user = User.objects.create_user(
username='testuser', email='test@test.com', first_name='Test', last_name='User'
)
self.user2 = User.objects.create_user(
username='Meow', email='meow123@test.com', first_name='Meow', last_name='Cat'
)
super().setUp()
def create_sample_vm(self, owner):
one_month_later, parse_status = cal.parse("1 month later")
return VMProduct.objects.create(
vmhost=self.vm_host, cores=2, ram_in_gb=4, owner=owner,
order=Order.objects.create(
owner=owner,
creation_date=datetime.datetime.now(tz=timezone.utc),
starting_date=datetime.datetime.now(tz=timezone.utc),
ending_date=datetime.datetime(*one_month_later[:6], tzinfo=timezone.utc),
recurring_period=RecurringPeriod.PER_MONTH
)
)
# TODO: the logic tested by this test is not implemented yet.
# def test_disk_product(self):
# """Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
# that is in status 'active'"""
#
# vm = self.create_sample_vm(owner=self.user)
#
# pending_disk_image = VMDiskImageProduct.objects.create(
# owner=self.user, name='pending_disk_image', is_os_image=True, is_public=True, size_in_gb=10,
# status='pending'
# )
# try:
# vm_disk_product = VMDiskProduct.objects.create(
# owner=self.user, vm=vm, image=pending_disk_image, size_in_gb=10
# )
# except ValidationError:
# vm_disk_product = None
#
# self.assertIsNone(
# vm_disk_product,
# msg='VMDiskProduct created with disk image whose status is not active.'
# )
# TODO: the logic tested by this test is not implemented yet.
# def test_vm_disk_product_creation_for_someone_else(self):
# """Ensure that a user can only create a VMDiskProduct for his/her own VM"""
#
# # Create a VM which is ownership of self.user2
# someone_else_vm = self.create_sample_vm(owner=self.user2)
#
# # 'self.user' would try to create a VMDiskProduct for 'user2's VM
# with self.assertRaises(ValidationError, msg='User created a VMDiskProduct for someone else VM.'):
# vm_disk_product = VMDiskProduct.objects.create(
# owner=self.user, vm=someone_else_vm,
# size_in_gb=10,
# image=VMDiskImageProduct.objects.create(
# owner=self.user, name='disk_image', is_os_image=True, is_public=True, size_in_gb=10,
# status='active'
# )
# )

261
uncloud_v4/views.py Normal file
View file

@ -0,0 +1,261 @@
from django.db import transaction
from django.shortcuts import render
from django.utils import timezone
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 rest_framework.exceptions import ValidationError
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct, VMCluster
from uncloud_pay.models import Order, BillingAddress
from .serializers import *
from uncloud_pay.helpers import ProductViewSet
import datetime
###
# Generic disk image views. Do not require orders / billing.
class VMDiskImageProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMDiskImageProduct.objects.all()
else:
obj = VMDiskImageProduct.objects.filter(owner=self.request.user) | VMDiskImageProduct.objects.filter(is_public=True)
return obj
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)
###
# User VM disk and snapshots.
class VMDiskProductViewSet(viewsets.ModelViewSet):
"""
Let a user modify their own VMDisks
"""
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMDiskProduct.objects.all()
else:
obj = VMDiskProduct.objects.filter(owner=self.request.user)
return obj
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)
class VMSnapshotProductViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = VMSnapshotProductSerializer
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMSnapshotProduct.objects.all()
else:
obj = VMSnapshotProduct.objects.filter(owner=self.request.user)
return obj
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)
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'])
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,
recurring_period=recurring_period)
order.save()
order.add_record(one_time_price=0,
recurring_price=recurring_price,
description="Snapshot of VM {} from {}".format(vm, now))
serializer.save(owner=request.user,
order=order,
gb_ssd=ssds_size,
gb_hdd=hdds_size)
return Response(serializer.data)
###
# User VMs.
class VMProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
if self.request.user.is_superuser:
obj = VMWithOSProduct.objects.all()
else:
obj = VMWithOSProduct.objects.filter(owner=self.request.user)
return obj
def get_serializer_class(self):
if self.action == 'create':
return OrderVMProductSerializer
else:
return VMProductSerializer
# 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 = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
order_recurring_period = serializer.validated_data.pop("recurring_period")
# Create disk image.
disk = VMDiskProduct(owner=request.user,
**serializer.validated_data.pop("primary_disk"))
vm = VMWithOSProduct(owner=request.user, primary_disk=disk,
**serializer.validated_data)
disk.vm = vm # XXX: Is this really needed?
# Create VM and Disk orders.
vm_order = Order.from_product(
vm,
recurring_period=order_recurring_period,
starting_date=timezone.now()
)
disk_order = Order.from_product(
disk,
recurring_period=order_recurring_period,
starting_date=timezone.now(),
depends_on=vm_order
)
# Commit to DB.
vm.order = vm_order
vm.save()
vm_order.save()
disk.order = disk_order
disk_order.save()
disk.save()
return Response(VMProductSerializer(vm, context={'request': request}).data)
class NicoVMProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated]
serializer_class = NicoVMProductSerializer
def get_queryset(self):
obj = VMProduct.objects.filter(owner=self.request.user)
return obj
def create(self, request):
serializer = self.serializer_class(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
vm = serializer.save(owner=request.user)
return Response(serializer.data)
###
# Admin stuff.
class VMHostViewSet(viewsets.ModelViewSet):
serializer_class = VMHostSerializer
queryset = VMHost.objects.all()
permission_classes = [permissions.IsAdminUser]
class VMClusterViewSet(viewsets.ModelViewSet):
serializer_class = VMClusterSerializer
queryset = VMCluster.objects.all()
permission_classes = [permissions.IsAdminUser]
##
# Nico's playground.
# 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)
return Response(serializer.data)