From 6dedf3693e5d12d07661d4c14cd1dccb5c18bd9e Mon Sep 17 00:00:00 2001 From: Modulos Date: Tue, 9 May 2017 04:41:45 +0200 Subject: [PATCH] Change to use OpenNebulaManager --- hosting/models.py | 66 ++++++++++------------------------------------- 1 file changed, 13 insertions(+), 53 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index 4a15789e..635b15a0 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -311,67 +311,27 @@ class HostingBill(AssignPermissionsMixin, models.Model): def __str__(self): return "%s" % (self.customer.user.email) + @classmethod + def create(cls, customer=None, billing_address=None): + instance = cls.objects.create(customer=customer, billing_address=billing_address) + return instance + def get_vms(self): - # Get User - user_email = self.customer.user.email + email = self.customer.user.email + # Get opennebula client + opennebula_client = OpenNebulaManager() - # Connect to open nebula server - try: - client = oca.Client("{0}:{1}".format(settings.OPENNEBULA_USERNAME, - settings.OPENNEBULA_PASSWORD), - "{protocol}://{domain}:{port}{endpoint}".format( - protocol=settings.OPENNEBULA_PROTOCOL, - domain=settings.OPENNEBULA_DOMAIN, - port=settings.OPENNEBULA_PORT, - endpoint=settings.OPENNEBULA_ENDPOINT - )) - except OpenNebulaException as err: - logger.error("Error : {0}".format(err)) - return None - except socket.timeout: - logger.error("Socket timeout error.") - return None - - - # Get open nebula user id for given email - user_pool = oca.UserPool(client) - user_pool.info() - try: - user_id = user_pool.get_by_name(user_email).id - except WrongNameError as wrong_name_err: - logger.error("User {0} does not exist.", user_email) - return None - # Get vm_pool for given user_id - vm_pool = oca.VirtualMachinePool(client) - vm_pool.info(filter=user_id) + # Get vm pool + vm_pool = opennebula_client.get_vms(email) # Reset total price self.total_price = 0 vms = [] # Add vm in vm_pool to context for vm in vm_pool: - name = vm.name - cores = int(vm.template.vcpu) - memory = int(vm.template.memory) / 1024 - # Check if vm has more than one disk - if 'DISK' in vm.template.multiple: - disk_size = 0 - for disk in vm.template.disks: - disk_size += int(disk.size) / 1024 - else: - disk_size = int(vm.template.disk.size) / 1024 - - #TODO: Replace with vm plan - price = 0.6 * disk_size + 2 * memory + 5 * cores - vm = {} - vm['name'] = name - vm['price'] = price - vm['disk_size'] = disk_size - vm['cores'] = cores - vm['memory'] = memory - vms.append(vm) - self.total_price += price - + vm_data = OpenNebulaManager.parse_vm(vm) + self.total_price += vm_data['price'] + vms.append(vm_data) self.save() return vms