From ba0cc137bc64f8e5b011c63cf0a9cdcefefd95f5 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 04:06:12 +0200 Subject: [PATCH] Start serialization --- opennebula_api/models.py | 103 +++++++++++++++++++++++++++++++++++ opennebula_api/serializer.py | 32 +++++++++++ 2 files changed, 135 insertions(+) create mode 100644 opennebula_api/serializer.py diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 21151dc8..31ed7e45 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -14,12 +14,115 @@ class VirtualMachineTemplate(models.Model): core_price = models.FloatField() disk_size_price = models.FloatField() + def get_name(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_template(template_id=self.opennebula_id) + return template.name + + def get_cores(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_template(template_id=self.opennebula_id).template + return int(template.vcpu) + + def get_disk_size(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_template(template_id=self.opennebula_id).template + return int(template.disk.size) / 1024 + + def get_memory(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_template(template_id=self.opennebula_id).template + return int(template.memory) / 1024 class VirtualMachine(models.Model): """This class represents an opennebula virtual machine.""" opennebula_id = models.IntegerField() template = models.ForeignKey(VirtualMachineTemplate) + VM_STATE = { + '0': 'INIT', + '1': 'PENDING', + '2': 'HOLD', + '3': 'ACTIVE', + '4': 'STOPPED', + '5': 'SUSPENDED', + '6': 'DONE', + '8': 'POWEROFF', + '9': 'UNDEPLOYED', + '10': 'CLONING', + '11': 'CLONING_FAILURE', + } + + def get_name(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + vm = self.manager._get_vm(vm_id=self.opennebula_id) + return vm.name + + def get_cores(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_vm(vm_id=self.opennebula_id).template + return int(template.vcpu) + + def get_disk_size(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_vm(vm_id=self.opennebula_id).template + try: + return template.disk.size + except AttributeError: + disk_size = 0 + for disk in template.disks: + disk_size += disk.size + return disk_size / 1024 + + + def get_memory(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + template = self.manager._get_vm(vm_id=self.opennebula_id).template + return int(template.memory) + + def get_id(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + vm = self.manager._get_vm(vm_id=self.opennebula_id) + return vm.id + + def get_ip(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + vm = self.manager._get_vm(vm_id=self.opennebula_id) + try: + return vm.user_template.ungleich_public_ip + except AttributeError: + return '-' + + def get_state(self): + if self.manager is None: + self.manager = OpenNebulaManager() + + vm = self.manager._get_vm(vm_id=self.opennebula_id) + return self.VM_STATE.get(str(vm.state)) + + def get_pirce(self) + return 0.0 + class OpenNebulaManager(): """This class represents an opennebula manager.""" diff --git a/opennebula_api/serializer.py b/opennebula_api/serializer.py new file mode 100644 index 00000000..98db0daa --- /dev/null +++ b/opennebula_api/serializer.py @@ -0,0 +1,32 @@ +from res_framework import serializers +from .models import VirtualMachine, VirtualMachineTemplate + +class VirtualMachineSerializer(serializers.ModelSerializer): + """Serializer to map the virtual machine instance into JSON format.""" + + cores = serializers.IntegerField(read_only=True, source='get_cores') + name = serializers.CharField(read_only=True, source='get_name') + disk_size = serializers.IntegerField(read_only=True, source='get_disk_size') + memory = serializers.IntegerField(read_only=True, source='get_memory') + #TODO: See if we can change to IPAddressField + ip = serializers.CharField(read_only=True, source='get_ip') + deploy_id = serializers.IntegerField(read_only=True, source='get_deploy_id') + id = serializers.IntegerField(read_only=True, source='get_id') + state = serializers.CharField(read_only=True, source='get_state') + price = serializers.FloatField(read_only=True, source='get_price') + + class Meta: + model = VirtualMachine + + +class VirtualMachineTemplateSerializer(serializers.ModelSerializer): + """Serializer to map the virtual machine template instance into JSON format.""" + cores = serializers.IntegerField(read_only=True, source='get_cores') + name = serializers.CharField(read_only=True, source='get_name') + disk_size = serializers.IntegerField(read_only=True, source='get_disk_size') + memory = serializers.IntegerField(read_only=True, source='get_memory') + + class Meta: + model = VirtualMachineTemplate + fields = () +