From 78738e8175c99741fd33e779ae8d80279191358c Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:16:28 +0200 Subject: [PATCH 1/8] Change cores to SerializerMethodField and obtain it from cpu --- opennebula_api/serializers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index c7418aa5..ec7bd5d5 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -46,7 +46,7 @@ class VirtualMachineSerializer(serializers.Serializer): """Serializer to map the virtual machine instance into JSON format.""" name = serializers.SerializerMethodField() - cores = serializers.IntegerField(source='template.vcpu') + cores = serializers.SerializerMethodField() disk = serializers.IntegerField(write_only=True) set_memory = serializers.IntegerField(write_only=True, label='Memory') memory = serializers.SerializerMethodField() @@ -99,6 +99,9 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) + def get_cores(self, obj): + return (obj.template.cpu) * 10 + def get_memory(self, obj): return int(obj.template.memory) / 1024 From 316496b9694d589d34dd102708a9480afb5302b6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:22:39 +0200 Subject: [PATCH 2/8] More fix for vcpu --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index ec7bd5d5..98162d1f 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -130,7 +130,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template - price = float(template.vcpu) * 5.0 + price = float(template.cpu * 10) * 5.0 price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: price += int(disk.size) / 1024 * 0.6 From 9f58fdd4561820cc76f9a610879ee1b53159eed1 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 00:26:59 +0200 Subject: [PATCH 3/8] Wrong multiplication to string instead of number --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 98162d1f..1fa1a802 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -130,7 +130,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template - price = float(template.cpu * 10) * 5.0 + price = float(template.cpu) * 10 * 5.0 price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: price += int(disk.size) / 1024 * 0.6 From 85b251d2e6a9c984360e56da6ce8386ef6303347 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:40:06 +0200 Subject: [PATCH 4/8] Check if the template VM serializer object has template attr --- opennebula_api/serializers.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 1fa1a802..3d451538 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -137,12 +137,15 @@ class VirtualMachineSerializer(serializers.Serializer): return price def get_configuration(self, obj): - template_id = obj.template.template_id - template = OpenNebulaManager().get_template(template_id) - if template.name.startswith('public-'): - return template.name.lstrip('public-') + if hasattr(obj, 'template'): + template_id = obj.template.template_id + template = OpenNebulaManager().get_template(template_id) + if template.name.startswith('public-'): + return template.name.lstrip('public-') + else: + return template.name else: - return template.name + return "" def get_ipv4(self, obj): """ From 6a5a5ed1609abe5de0c60111e8b6c86450f88ad6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:43:06 +0200 Subject: [PATCH 5/8] Check template_id attr exists within template --- opennebula_api/serializers.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 3d451538..69c593fb 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -138,12 +138,13 @@ class VirtualMachineSerializer(serializers.Serializer): def get_configuration(self, obj): if hasattr(obj, 'template'): - template_id = obj.template.template_id - template = OpenNebulaManager().get_template(template_id) - if template.name.startswith('public-'): - return template.name.lstrip('public-') - else: - return template.name + if hasattr(obj, 'template.template_id'): + template_id = obj.template.template_id + template = OpenNebulaManager().get_template(template_id) + if template.name.startswith('public-'): + return template.name.lstrip('public-') + else: + return template.name else: return "" From fa018739c9df10c5bdea3407a7837cef04ea1ab2 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 07:47:36 +0200 Subject: [PATCH 6/8] Change cores to float before multiplication --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 69c593fb..e0f635f4 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -100,7 +100,7 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) def get_cores(self, obj): - return (obj.template.cpu) * 10 + return float(obj.template.cpu) * 10 def get_memory(self, obj): return int(obj.template.memory) / 1024 From 5b7d1301cf3cb0628cb9aa6bfca04d0e954afa30 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 08:56:10 +0200 Subject: [PATCH 7/8] Return cores as int --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index e0f635f4..3a77c0d8 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -100,7 +100,7 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) def get_cores(self, obj): - return float(obj.template.cpu) * 10 + return int(float(obj.template.cpu) * 10) def get_memory(self, obj): return int(obj.template.memory) / 1024 From a8caa035c090713fdbf5b4c5bf2c5232600c7a4e Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 9 Apr 2019 09:12:09 +0200 Subject: [PATCH 8/8] Set configuration to empty string if not available --- utils/hosting_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index ec97a320..ee65fccf 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -47,7 +47,8 @@ def get_or_create_vm_detail(user, manager, vm_id): vm_detail_obj = VMDetail.objects.create( user=user, vm_id=vm_id, disk_size=vm['disk_size'], cores=vm['cores'], memory=vm['memory'], - configuration=vm['configuration'], ipv4=vm['ipv4'], + configuration="" if vm['configuration'] is None else vm['configuration'], + ipv4=vm['ipv4'], ipv6=vm['ipv6'] ) return vm_detail_obj