VRE Backend API and Scheduler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

165 lines
5.9 KiB

from django.conf import settings
from rest_framework import serializers
from drf_yasg.utils import swagger_serializer_method
from apps.virtual_machine.models import (VirtualMachine,
VirtualMachineOperatingSystem,
VirtualMachineProfile,
VirtualMachineMemory,
VirtualMachineNetwork,
VirtualMachineStorage,
VirtualMachineGPU,
VirtualMachineAccess)
from apps.researcher.serializers import ResearcherDetailSerializer
from apps.study.serializers import StudyDetailSerializer
class VirtualMachineProviderSerializer(serializers.Serializer):
id = serializers.ReadOnlyField(help_text='The provider key/ID')
name = serializers.ReadOnlyField(help_text='The provider name')
class VirtualMachineProviderDetailMixin():
@swagger_serializer_method(serializer_or_field=VirtualMachineProviderSerializer(many=False))
def get_provider(self, instance):
try:
data = settings.CLOUD_PROVIDERS[instance.provider]
data['id'] = instance.provider
except:
data = {'id': '_', 'name': 'Provider not available'}
return VirtualMachineProviderSerializer(data).data
class VirtualMachineOperatingSystemSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
class Meta:
model = VirtualMachineOperatingSystem
exclude = ['is_available', 'cloud_id']
class VirtualMachineMemorySerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
unit_value = serializers.ReadOnlyField()
class Meta:
model = VirtualMachineMemory
exclude = ['is_available', 'cloud_id']
class VirtualMachineNetworkSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
unit_value = serializers.ReadOnlyField()
class Meta:
model = VirtualMachineNetwork
exclude = ['is_available', 'cloud_id']
class VirtualMachineStorageSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
unit_value = serializers.ReadOnlyField()
class Meta:
model = VirtualMachineStorage
exclude = ['is_available', 'cloud_id']
class VirtualMachineGPUSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
class Meta:
model = VirtualMachineGPU
exclude = ['is_available', 'cloud_id']
class VirtualMachineProfileSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
provider = serializers.SerializerMethodField()
description = serializers.ReadOnlyField()
os = VirtualMachineOperatingSystemSerializer()
memory_type = VirtualMachineMemorySerializer()
networks = VirtualMachineNetworkSerializer(many=True)
storage_type = VirtualMachineStorageSerializer()
gpu_type = VirtualMachineGPUSerializer()
class Meta:
model = VirtualMachineProfile
exclude = ['is_available']
class VirtualMachineAccessSerializer(serializers.ModelSerializer):
username = serializers.ReadOnlyField(help_text='Username to login with')
class Meta:
model = VirtualMachineAccess
exclude = ['researcher', 'virtual_machine']
class VirtualMachineDetailSerializer(VirtualMachineProviderDetailMixin, serializers.ModelSerializer):
remote_id = serializers.ReadOnlyField()
provider = serializers.SerializerMethodField()
profile = VirtualMachineProfileSerializer()
operating_system = VirtualMachineOperatingSystemSerializer()
base_memory_type = VirtualMachineMemorySerializer()
additional_memory_type = VirtualMachineMemorySerializer()
base_storage_type = VirtualMachineStorageSerializer()
additional_storage_type = VirtualMachineStorageSerializer()
networks = VirtualMachineNetworkSerializer(many=True)
total_memory = serializers.ReadOnlyField()
total_storage = serializers.ReadOnlyField()
researcher = ResearcherDetailSerializer()
study = StudyDetailSerializer()
# TODO: For every virtual machine, we get all the access accounts. So maybe we need to filter here for only the active logged in user.
access = VirtualMachineAccessSerializer(
many=True,
read_only=True,
)
class Meta:
model = VirtualMachine
fields = '__all__'
def create(self, validated_data):
networks = validated_data.pop('networks')
virtual_machine = VirtualMachine.objects.create(**validated_data)
for network in networks:
virtual_machine.networks.add(network)
return virtual_machine
class VirtualMachineSerializer(serializers.ModelSerializer):
# remote_id = serializers.ReadOnlyField()
# profile_name = serializers.ReadOnlyField(source='profile.name')
# operating_system_name = serializers.ReadOnlyField(source='operating_system.name')
# total_memory = serializers.ReadOnlyField()
# total_storage = serializers.ReadOnlyField()
# # TODO: For every virtual machine, we get all the access accounts. So maybe we need to filter here for only the active logged in user.
# access = VirtualMachineAccessSerializer(
# many=True,
# read_only=True,
# )
class Meta:
model = VirtualMachine
exclude = ['created_at', 'updated_at', 'id', 'researcher']
def create(self, validated_data):
networks = validated_data.pop('networks')
virtual_machine = VirtualMachine.objects.create(**validated_data)
for network in networks:
virtual_machine.networks.add(network)
return virtual_machine