From 167e0482a91bdecb71bcda5b28c664bb3ea8b5c0 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Mon, 10 Apr 2017 13:43:13 +0530 Subject: [PATCH] Added Delete VM functionality. --- hosting/admin.py | 46 ++++++++++++++++++++++-- hosting/templates/hosting/managevms.html | 2 ++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/hosting/admin.py b/hosting/admin.py index 01e713a8..786d5d6f 100644 --- a/hosting/admin.py +++ b/hosting/admin.py @@ -113,10 +113,11 @@ class HostingManageVMsAdmin(admin.ModelAdmin): def get_urls(self): self.client = oca.Client(settings.OPENNEBULA_USERNAME + ':' + settings.OPENNEBULA_PASSWORD, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT) urls = super().get_urls() - socket.setdefaulttimeout(5) + #socket.setdefaulttimeout(5) my_urls = [ url(r'^$', self.admin_site.admin_view(self.my_view, cacheable=True)), url(r'^create_vm/$', self.admin_site.admin_view(self.create_vm, cacheable=True), name='createvm'), + url(r'^delete_vm/(?P\d+)/$', self.admin_site.admin_view(self.delete_vm, cacheable=True), name='deletevm'), #url(r'^my_views/$', self.admin_site.admin_view(self.my_view, cacheable=True)) ] return my_urls + urls @@ -149,7 +150,7 @@ class HostingManageVMsAdmin(admin.ModelAdmin): # Lets create a test VM with 128MB of ram and 1 CPU vm_id = oca.VirtualMachine.allocate(self.client, '1281') s_message = "Created with id = " + str(vm_id) - vm_pool = self.get_vms + vm_pool = self.get_vms() # Lets print the VMs available in the pool # print("Printing the available VMs in the pool.") # vm_pool = oca.VirtualMachinePool(client) @@ -170,10 +171,49 @@ class HostingManageVMsAdmin(admin.ModelAdmin): ) return TemplateResponse(request, "hosting/managevms.html", context) - def get_vms(): + def get_vms(self): vm_pool = oca.VirtualMachinePool(self.client) + vm_pool.info() return vm_pool + def delete_vm(self, request, vmid): + vm_pool = self.get_vms() + e_message = '' + s_message = '' + # get the desired vm from the pool + vm_id = int(vmid) + vm = self.get_vm_by_id(vm_id) + if vm == -1: + print("Did not find a vm with id = " + str(vm_id)) + e_message = "Did not find a vm with id = " + str(vm_id) + else : + print("Deleting vm_id = " + str(vm_id) + " state = " + vm.str_state) + if vm.str_state == 'PENDING' or vm.str_state =='POWEROFF' or vm.str_state =='ACTIVE': + vm.delete() + s_message = "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id) + else: + s_message = "Deleted from " + vm.str_state + " state vm with id = " + str(vm_id) + vm.finalize() + vm_pool = self.get_vms() + context = dict( + # Include common variables for rendering the admin template. + self.admin_site.each_context(request), + error_msg=e_message, + success_msg=s_message, + vms = vm_pool, + # Anything else you want in the context... + # key=value, + ) + return TemplateResponse(request, "hosting/managevms.html", context) + + def get_vm_by_id(self, vmid): + vms = self.get_vms() + vms + for vm in vms: + if vm.id == vmid : + return vm + return -1 + admin.site.register(HostingOrder, HostingOrderAdmin) admin.site.register(VirtualMachineType) admin.site.register(VirtualMachinePlan, VirtualMachinePlanAdmin) diff --git a/hosting/templates/hosting/managevms.html b/hosting/templates/hosting/managevms.html index 44c90adf..77769e46 100644 --- a/hosting/templates/hosting/managevms.html +++ b/hosting/templates/hosting/managevms.html @@ -19,6 +19,7 @@ Memory Status User Name +Manage Vms @@ -29,6 +30,7 @@ {{vm.template.memory}} {{vm.str_state}} {{vm.uname}} +Delete VM {% endfor %}