Merge pull request #225 from levivm/opennebula-integration
added get vms
This commit is contained in:
commit
0220867772
2 changed files with 61 additions and 2 deletions
|
@ -1,9 +1,12 @@
|
|||
import os
|
||||
import oca
|
||||
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from Crypto.PublicKey import RSA
|
||||
from stored_messages.settings import stored_messages_settings
|
||||
|
||||
|
@ -168,6 +171,61 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model):
|
|||
self.status = self.CANCELED_STATUS
|
||||
self.save(update_fields=['status'])
|
||||
|
||||
@classmethod
|
||||
def get_vms(self, email):
|
||||
# Get User
|
||||
user_email = email
|
||||
|
||||
# Connect to open nebula server
|
||||
# TODO: handle potential connection error
|
||||
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
|
||||
))
|
||||
# Get open nebula user id for given email
|
||||
user_pool = oca.UserPool(client)
|
||||
user_pool.info()
|
||||
# TODO: handle potential name error
|
||||
user_id = user_pool.get_by_name(user_email).id
|
||||
|
||||
# Get vm_pool for given user_id
|
||||
vm_pool = oca.VirtualMachinePool(client)
|
||||
vm_pool.info(filter=user_id)
|
||||
|
||||
# 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
|
||||
# self.save()
|
||||
return vms
|
||||
|
||||
|
||||
class HostingOrder(AssignPermissionsMixin, models.Model):
|
||||
|
||||
|
|
|
@ -412,8 +412,9 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView):
|
|||
ordering = '-id'
|
||||
|
||||
def get_queryset(self):
|
||||
hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
print(hosting_admin.show_vms_view(self.request))
|
||||
# hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
|
||||
# print(hosting_admin.show_vms_view(self.request))
|
||||
print(VirtualMachinePlan.get_vms(self.request.user.email))
|
||||
user = self.request.user
|
||||
self.queryset = VirtualMachinePlan.objects.active(user)
|
||||
return super(VirtualMachinesPlanListView, self).get_queryset()
|
||||
|
|
Loading…
Reference in a new issue