Add calculate price function to template

This commit is contained in:
Modulos 2017-05-10 02:36:11 +02:00
parent ec945cc31b
commit c1a3f7281f
2 changed files with 19 additions and 0 deletions

View file

@ -14,6 +14,18 @@ class VirtualMachineTemplate(models.Model):
core_price = models.FloatField() core_price = models.FloatField()
disk_size_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): class VirtualMachine(models.Model):
"""This class represents an opennebula virtual machine.""" """This class represents an opennebula virtual machine."""

View file

@ -63,6 +63,13 @@ class VirtualMachineTemplateTestCase(TestCase):
new_count = VirtualMachineTemplate.objects.count() new_count = VirtualMachineTemplate.objects.count()
self.assertNotEqual(old_count, new_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): class VirtualMachineTestCase(TestCase):
def setUp(self): def setUp(self):