2020-02-26 10:31:17 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
2020-03-03 10:23:29 +00:00
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
|
2020-03-03 09:14:56 +00:00
|
|
|
from uncloud_pay.models import RecurringPeriod
|
2020-02-26 10:31:17 +00:00
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
GB_SSD_PER_DAY=0.012
|
|
|
|
GB_HDD_PER_DAY=0.0006
|
|
|
|
|
|
|
|
GB_SSD_PER_DAY=0.012
|
|
|
|
GB_HDD_PER_DAY=0.0006
|
|
|
|
|
|
|
|
|
|
|
|
class VMHostSerializer(serializers.ModelSerializer):
|
2020-03-05 13:58:45 +00:00
|
|
|
vms = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = VMHost
|
|
|
|
fields = '__all__'
|
2020-03-05 13:58:45 +00:00
|
|
|
read_only_fields = [ 'vms' ]
|
2020-02-26 10:31:17 +00:00
|
|
|
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
class VMDiskProductSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = VMDiskProduct
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
class VMDiskImageProductSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = VMDiskImageProduct
|
|
|
|
fields = '__all__'
|
|
|
|
|
2020-02-25 21:01:55 +00:00
|
|
|
class VMProductSerializer(serializers.HyperlinkedModelSerializer):
|
2020-03-03 09:14:56 +00:00
|
|
|
# Custom field used at creation (= ordering) only.
|
|
|
|
recurring_period = serializers.ChoiceField(
|
2020-03-03 09:51:16 +00:00
|
|
|
choices=VMProduct.allowed_recurring_periods())
|
2020-03-03 09:14:56 +00:00
|
|
|
|
2020-02-25 21:01:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = VMProduct
|
2020-02-28 15:26:45 +00:00
|
|
|
fields = ['uuid', 'order', 'owner', 'status', 'name', \
|
2020-03-03 09:14:56 +00:00
|
|
|
'cores', 'ram_in_gb', 'recurring_period']
|
2020-02-28 15:26:45 +00:00
|
|
|
read_only_fields = ['uuid', 'order', 'owner', 'status']
|
2020-02-27 14:29:15 +00:00
|
|
|
|
2020-03-06 10:10:20 +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
|
|
|
|
|
2020-03-03 09:51:16 +00:00
|
|
|
class ManagedVMProductSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
Managed VM serializer used in ungleich_service app.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = VMProduct
|
|
|
|
fields = [ 'cores', 'ram_in_gb']
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
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.uuid))
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
pricing = {}
|
|
|
|
pricing['per_gb_ssd'] = 0.012
|
|
|
|
pricing['per_gb_hdd'] = 0.0006
|
|
|
|
pricing['recurring_period'] = 'per_day'
|