2020-02-25 19:53:12 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
2020-02-29 15:45:52 +00:00
|
|
|
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
|
2020-02-25 19:53:12 +00:00
|
|
|
|
2020-02-29 15:45:52 +00:00
|
|
|
class VMHostSerializer(serializers.ModelSerializer):
|
2020-02-25 19:53:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = VMHost
|
|
|
|
fields = '__all__'
|
2020-02-25 21:01:55 +00:00
|
|
|
|
|
|
|
|
2020-02-29 15:45:52 +00:00
|
|
|
class VMProductSerializer(serializers.ModelSerializer):
|
2020-02-25 21:01:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = VMProduct
|
|
|
|
fields = '__all__'
|
2020-02-27 10:36:50 +00:00
|
|
|
|
2020-02-29 15:45:52 +00:00
|
|
|
class VMDiskProductSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = VMDiskProduct
|
|
|
|
fields = '__all__'
|
2020-02-27 14:29:15 +00:00
|
|
|
|
2020-02-29 15:45:52 +00:00
|
|
|
class VMDiskImageProductSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = VMDiskImageProduct
|
|
|
|
fields = '__all__'
|
2020-02-27 14:29:15 +00:00
|
|
|
|
2020-02-27 11:31:20 +00:00
|
|
|
class VMSnapshotProductSerializer(serializers.ModelSerializer):
|
2020-02-27 10:36:50 +00:00
|
|
|
class Meta:
|
|
|
|
model = VMSnapshotProduct
|
|
|
|
fields = '__all__'
|
2020-02-27 14:29:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# verify that vm.owner == user.request
|
|
|
|
def validate_vm(self, value):
|
2020-02-29 15:45:52 +00:00
|
|
|
|
|
|
|
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'
|