uncloud/uncloud_vm/serializers.py

144 lines
4.3 KiB
Python
Raw Permalink Normal View History

from django.contrib.auth import get_user_model
from rest_framework import serializers
2020-04-18 11:51:31 +00:00
from .models import *
2020-04-18 11:51:31 +00:00
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
2020-04-18 11:51:31 +00:00
###
# Admin views.
2020-03-22 19:55:11 +00:00
class VMHostSerializer(serializers.HyperlinkedModelSerializer):
vms = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = VMHost
fields = '__all__'
read_only_fields = [ 'vms' ]
2020-03-22 19:55:11 +00:00
class VMClusterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = VMCluster
fields = '__all__'
2020-04-18 11:51:31 +00:00
###
# 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'
2020-03-18 14:43:01 +00:00
2020-04-18 11:51:31 +00:00
###
# 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:
2020-04-18 11:51:31 +00:00
model = VMWithOSProduct
2020-04-18 11:51:31 +00:00
fields = [ 'cores', 'ram_in_gb', 'primary_disk']
2020-03-18 14:43:01 +00:00
2020-04-18 11:51:31 +00:00
class VMProductSerializer(serializers.ModelSerializer):
primary_disk = CreateVMDiskProductSerializer()
2020-04-18 11:51:31 +00:00
snapshots = VMSnapshotProductSerializer(many=True, read_only=True)
disks = VMDiskProductSerializer(many=True, read_only=True)
class Meta:
2020-04-18 11:51:31 +00:00
model = VMWithOSProduct
fields = ['order', 'owner', 'status', 'name', 'cores',
2020-04-18 11:51:31 +00:00
'ram_in_gb', 'primary_disk', 'snapshots', 'disks', 'extra_data']
read_only_fields = ['order', 'owner', 'status']
2020-03-18 14:43:01 +00:00
2020-04-18 11:51:31 +00:00
class OrderVMProductSerializer(VMProductSerializer):
recurring_period = serializers.ChoiceField(
2020-04-18 11:51:31 +00:00
choices=VMWithOSProduct.allowed_recurring_periods())
2020-04-18 11:51:31 +00:00
def __init__(self, *args, **kwargs):
super(VMProductSerializer, self).__init__(*args, **kwargs)
class Meta:
model = VMProductSerializer.Meta.model
2020-04-18 11:51:31 +00:00
fields = VMProductSerializer.Meta.fields + [ 'recurring_period' ]
2020-04-18 11:51:31 +00:00
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',
2020-05-02 18:31:36 +00:00
'vmhost', 'vmcluster', 'snapshots',
'extra_data' ]
fields = read_only_fields + [ 'name',
'cores',
2020-05-02 18:31:36 +00:00
'ram_in_gb'
]
2020-04-18 11:51:31 +00:00
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