From 9a345184579f57720d81e6821456e5f43c81e97e Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 8 May 2017 21:54:12 -0500 Subject: [PATCH] attached opennebula vm id to virtualmachine plan --- hosting/models.py | 3 +++ hosting/opennebula_functions.py | 5 +++-- hosting/views.py | 23 ++++------------------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/hosting/models.py b/hosting/models.py index b6692d0c..bceaaacf 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -177,12 +177,15 @@ class VirtualMachinePlan(AssignPermissionsMixin, models.Model): @classmethod def create_opennebula_vm(self, user, specs): + + # Init opennebula manager using given user opennebula_client = OpenNebulaManager( user.email, user.password[0:20], create_user=True ) + # Create a vm in opennebula using given specs vm = opennebula_client.create_vm(specs) return vm diff --git a/hosting/opennebula_functions.py b/hosting/opennebula_functions.py index 2f9f483c..1d241d59 100644 --- a/hosting/opennebula_functions.py +++ b/hosting/opennebula_functions.py @@ -37,6 +37,7 @@ class OpenNebulaManager: def __init__(self, email=None, password=None, create_user=True): + # Get oneadmin client self.oneadmin_client = self._get_opennebula_client( settings.OPENNEBULA_USERNAME, settings.OPENNEBULA_PASSWORD @@ -45,11 +46,13 @@ class OpenNebulaManager: if not create_user: return + # Get or create oppenebula user using given credentials self.opennebula_user = self._get_or_create_user( email, password ) + # If opennebula user was created/obtained, get his client if self.opennebula_user: self.client = self._get_opennebula_client( email, @@ -68,8 +71,6 @@ class OpenNebulaManager: )) def _get_or_create_user(self, email, password): - # import pdb - # pdb.set_trace() try: user_pool = oca.UserPool(self.oneadmin_client) diff --git a/hosting/views.py b/hosting/views.py index 8e07e31c..8989732f 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -340,29 +340,14 @@ class PaymentVMView(LoginRequiredMixin, FormView): # If the Stripe payment was successed, set order status approved order.set_approved() - # Create VM using oppenebula functions - # _request = namedtuple('request', 'POST user') - # _request.user = request.user - # user = namedtuple('user', 'email') - # email - # _request.POST = { - # 'vm_template': vm_template - # } - - open_vm = VirtualMachinePlan.create_opennebula_vm( + # Create a vm using logged user + oppennebula_vm_id = VirtualMachinePlan.create_opennebula_vm( self.request.user, specs ) - print(open_vm) - print(open_vm) - print(open_vm) - - - # hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin) - # hosting_admin.init_opennebula_client(request) - # oppennebula_vm_id = hosting_admin.create_vm_view(vm_type.get_specs()) - # plan.oppenebula_id = oppennebula_vm_id + plan.oppenebula_id = oppennebula_vm_id + plan.save() # Send notification to ungleich as soon as VM has been booked context = {