Merge branch 'calculate_price' into opennebula_api
This commit is contained in:
commit
4016dcfea9
2 changed files with 19 additions and 0 deletions
|
@ -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."""
|
||||
|
|
|
@ -69,6 +69,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):
|
||||
|
|
Loading…
Reference in a new issue