Change to use OpenNebulaManager

This commit is contained in:
Modulos 2017-05-09 04:41:45 +02:00
parent 7010c76c72
commit 6dedf3693e

View file

@ -311,67 +311,27 @@ class HostingBill(AssignPermissionsMixin, models.Model):
def __str__(self): def __str__(self):
return "%s" % (self.customer.user.email) 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): def get_vms(self):
# Get User email = self.customer.user.email
user_email = self.customer.user.email # Get opennebula client
opennebula_client = OpenNebulaManager()
# Connect to open nebula server # Get vm pool
try: vm_pool = opennebula_client.get_vms(email)
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)
# Reset total price # Reset total price
self.total_price = 0 self.total_price = 0
vms = [] vms = []
# Add vm in vm_pool to context # Add vm in vm_pool to context
for vm in vm_pool: for vm in vm_pool:
name = vm.name vm_data = OpenNebulaManager.parse_vm(vm)
cores = int(vm.template.vcpu) self.total_price += vm_data['price']
memory = int(vm.template.memory) / 1024 vms.append(vm_data)
# 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
self.save() self.save()
return vms return vms