Start serialization
This commit is contained in:
parent
ec945cc31b
commit
ba0cc137bc
2 changed files with 135 additions and 0 deletions
|
@ -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."""
|
||||
|
||||
|
|
32
opennebula_api/serializer.py
Normal file
32
opennebula_api/serializer.py
Normal file
|
@ -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 = ()
|
||||
|
Loading…
Reference in a new issue