From c1a3f7281fe432a7b6ac8f20e3ef00c0a86de9b9 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 02:36:11 +0200 Subject: [PATCH] Add calculate price function to template --- opennebula_api/models.py | 12 ++++++++++++ opennebula_api/tests.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 21151dc8..1f66d5b3 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -14,6 +14,18 @@ class VirtualMachineTemplate(models.Model): core_price = models.FloatField() disk_size_price = models.FloatField() + def calculate_price(self): + manager = OpenNebulaManager() + template = manger._get_template(self.opennebula_id).template + + price = int(template.vpcu) * self.core_price + price += int(template.memory) / 1024 * self.memory_price + try: + price += int(template.disk.size) / 1024 * self.disk_size_price + except AttributeError: + for disk in template.disks: + price += int(disk.size) / 1024 * self.disk_size_price + return price class VirtualMachine(models.Model): """This class represents an opennebula virtual machine.""" diff --git a/opennebula_api/tests.py b/opennebula_api/tests.py index 95d9d08d..794e4435 100644 --- a/opennebula_api/tests.py +++ b/opennebula_api/tests.py @@ -63,6 +63,13 @@ class VirtualMachineTemplateTestCase(TestCase): new_count = VirtualMachineTemplate.objects.count() self.assertNotEqual(old_count, new_count) + def test_model_can_calculate_price(self): + price = self.cores * self.core_price + price += self.memory * self.memory_price + price += self.disk_size * self.disk_size_price + self.assertEqual(price, self.template.calculate_price()) + + class VirtualMachineTestCase(TestCase): def setUp(self):