From b052507134ccd6e62c71a00ef434e1e2ba834e95 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 01:11:49 +0200 Subject: [PATCH 1/7] Add create template function --- opennebula_api/models.py | 25 +++++++++++++++++++++++++ opennebula_api/tests.py | 12 +++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 21151dc8..c4943c3e 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -83,3 +83,28 @@ class OpenNebulaManager(): protocol=settings.OPENNEBULA_PROTOCOL) ) return user_pool + def create_template(self, name, cores, memory, disk_size): + template_string_formatter = """ + """ + template_id = oca.VmTemplate.allocate( + self.oneadmin_client, + template_string_formatter.format( + name=name, + vcpu=cores, + cpu=0.1*cores, + size=1024 * disk_size, + memory=1024 * memory + ) + ) + + return template_id diff --git a/opennebula_api/tests.py b/opennebula_api/tests.py index 95d9d08d..b0c41429 100644 --- a/opennebula_api/tests.py +++ b/opennebula_api/tests.py @@ -18,7 +18,11 @@ class OpenNebulaManagerTestCases(TestCase): def test_model_can_connect_to_server(self): """Test the opennebula manager model can connect to a server.""" - self.assertFalse(self.manager is None) + try: + version = self.manager.version() + except: + version = None + self.assertFalse(version is None) def test_model_can_create_user(self): """Test the opennebula manager model can create a new user.""" @@ -46,8 +50,10 @@ class VirtualMachineTemplateTestCase(TestCase): self.disk_size = 10.0 self.manager = OpenNebulaManager(email=None, password=None, create_user=False) - self.opennebula_id = self.manager.create_template(self.cores, self.memory, - self.disk_size) + self.opennebula_id = self.manager.create_template(name=self.template_name, + cores=self.cores, + memory=self.memory, + disk_size=self.disk_size) self.template = VirtualMachineTemplate(opennebula_id=self.opennebula_id, base_price=self.base_price, From c1a3f7281fe432a7b6ac8f20e3ef00c0a86de9b9 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 02:36:11 +0200 Subject: [PATCH 2/7] 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): From f2a7ac258d020fdc0caad419251d694a4ad6f070 Mon Sep 17 00:00:00 2001 From: Modulos Date: Wed, 10 May 2017 02:49:03 +0200 Subject: [PATCH 3/7] Fix typo and add function description --- opennebula_api/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index c4943c3e..447a7f75 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -67,12 +67,12 @@ class OpenNebulaManager(): except WrongNameError as wrong_name_err: opennebula_user = self.oneadmin_client.call(oca.User.METHODS['allocate'], email, password, 'core') + return opennebula_user except ConnectionRefusedError: print('Could not connect to host: {host} via protocol {protocol}'.format( host=settings.OPENNEBULA_DOMAIN, protocol=settings.OPENNEBULA_PROTOCOL) ) - return opennebula_user def _get_user_pool(self): try: user_pool = oca.UserPool(self.oneadmin_client) @@ -84,6 +84,13 @@ class OpenNebulaManager(): ) return user_pool def create_template(self, name, cores, memory, disk_size): + """Create and add a new template to opennebula. + :param name: A string representation describing the template. + Used as label in view. + :param cores: Amount of virtual cpu cores for the VM. + :param memory: Amount of RAM for the VM (MB) + :param disk_size: Amount of disk space for VM (MB) + """ template_string_formatter = """