Added Delete VM functionality.
This commit is contained in:
parent
24cfe707cd
commit
167e0482a9
2 changed files with 45 additions and 3 deletions
|
@ -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<vmid>\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, '<VM><MEMORY>128</MEMORY><CPU>1</CPU></VM>')
|
||||
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)
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
<th>Memory</th>
|
||||
<th>Status</th>
|
||||
<th>User Name</th>
|
||||
<th>Manage Vms</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -29,6 +30,7 @@
|
|||
<td>{{vm.template.memory}}</td>
|
||||
<td>{{vm.str_state}}</td>
|
||||
<td>{{vm.uname}}</td>
|
||||
<td><a href="{% url 'admin:deletevm' vm.id %}">Delete VM</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
|
Loading…
Reference in a new issue