uncloud-mravi/uncloud/uncloud_vm/serializers.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

50 lines
1.5 KiB
Python

from django.contrib.auth import get_user_model
from rest_framework import serializers
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
class VMHostSerializer(serializers.ModelSerializer):
class Meta:
model = VMHost
fields = '__all__'
class VMProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMProduct
fields = '__all__'
class VMDiskProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMDiskProduct
fields = '__all__'
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.uuid))
return value
pricing = {}
pricing['per_gb_ssd'] = 0.012
pricing['per_gb_hdd'] = 0.0006
pricing['recurring_period'] = 'per_day'