dynamicweb/opennebula_api/serializers.py

143 lines
5.3 KiB
Python
Raw Normal View History

import oca
from rest_framework import serializers
from oca import OpenNebulaException
from oca.template import VmTemplate
from .models import OpenNebulaManager
class VirtualMachineTemplateSerializer(serializers.Serializer):
"""Serializer to map the virtual machine template instance into JSON format."""
id = serializers.IntegerField(read_only=True)
2017-05-13 15:32:55 +00:00
set_name = serializers.CharField(read_only=True, label='Name')
2017-05-13 11:47:53 +00:00
name = serializers.SerializerMethodField()
cores = serializers.IntegerField(source='template.vcpu')
disk = serializers.IntegerField(write_only=True)
disk_size = serializers.SerializerMethodField()
2017-05-13 15:32:55 +00:00
set_memory = serializers.IntegerField(write_only=True, label='Memory')
memory = serializers.SerializerMethodField()
price = serializers.SerializerMethodField()
def create(self, validated_data):
data = validated_data
template = data.pop('template')
cores = template.pop('vcpu')
name = data.pop('name')
disk_size = data.pop('disk')
memory = template.pop('memory')
2017-05-13 04:59:57 +00:00
manager = OpenNebulaManager()
try:
opennebula_id = manager.create_template(name=name, cores=cores,
memory=memory,
disk_size=disk_size,
core_price=core_price,
disk_size_price=disk_size_price,
memory_price=memory_price)
except OpenNebulaException as err:
raise serializers.ValidationError("OpenNebulaException occured. {0}".format(err))
return manager.get_template(template_id=opennebula_id)
def get_disk_size(self, obj):
template = obj.template
disk_size = 0
try:
for disk in template.disks:
disk_size += int(disk.size)
return disk_size / 1024
except:
return 0
2017-05-13 23:21:10 +00:00
def get_price(self, obj):
template = obj.template
2017-05-13 23:21:10 +00:00
price = float(template.cpu) * 5.0
price += (int(template.memory)/1024 * 2.0)
try:
for disk in template.disks:
price += int(disk.size)/1024 * 0.6
except:
pass
return price
def get_memory(self, obj):
return int(obj.template.memory)/1024
2017-05-13 11:47:53 +00:00
def get_name(self, obj):
2017-05-13 15:32:55 +00:00
# TODO: Filter public- away
2017-05-13 11:47:53 +00:00
return obj.name
class VirtualMachineSerializer(serializers.Serializer):
"""Serializer to map the virtual machine instance into JSON format."""
name = serializers.CharField(read_only=True)
cores = serializers.IntegerField(source='template.vcpu')
disk = serializers.IntegerField(write_only=True)
2017-05-13 15:32:55 +00:00
set_memory = serializers.IntegerField(write_only=True, label='Memory')
memory = serializers.SerializerMethodField()
disk_size = serializers.SerializerMethodField()
ip = serializers.CharField(read_only=True,
source='user_template.ungleich_public_ip',
default='-')
vm_id = serializers.IntegerField(read_only=True, source='id')
state = serializers.CharField(read_only=True, source='str_state')
price = serializers.SerializerMethodField()
ssh_key = serializers.CharField(write_only=True)
template_id = serializers.ChoiceField(
choices=[(key.id, key.name) for key in
OpenNebulaManager().get_templates()],
source='template.template_id',
write_only=True
)
def create(self, validated_data):
owner = validated_data['owner']
ssh_key = validated_data['ssh_key']
cores = validated_data['template']['vcpu']
2017-05-13 15:32:55 +00:00
memory = validated_data['set_memory']
disk = validated_data['disk']
template_id = validated_data['template']['template_id']
specs = {
'cpu' : cores,
'disk_size' : disk,
'memory' : memory,
}
try:
2017-05-11 10:45:09 +00:00
manager = OpenNebulaManager(email=owner.email,
2017-05-12 17:13:18 +00:00
password=owner.password,
2017-05-13 04:59:57 +00:00
)
opennebula_id = manager.create_vm(template_id=template_id,
ssh_key=ssh_key,
specs=specs)
except OpenNebulaException as err:
raise serializers.ValidationError("OpenNebulaException occured. {0}".format(err))
return manager.get_vm(opennebula_id)
def get_memory(self, obj):
return int(obj.template.memory)/1024
def get_disk_size(self, obj):
template = obj.template
disk_size = 0
for disk in template.disks:
disk_size += int(disk.size)
return disk_size / 1024
def get_price(self, obj):
template = obj.template
2017-05-14 00:17:48 +00:00
price = float(template.vcpu) * 5.0
2017-05-13 23:21:10 +00:00
price += (int(template.memory)/1024 * 2.0)
for disk in template.disks:
2017-05-13 23:21:10 +00:00
price += int(disk.size)/1024 * 0.6
return price