Added functionality to select templates for the VMs being created

This commit is contained in:
M.Ravi 2017-04-18 13:34:29 +05:30
parent b68fe31361
commit c8614f4f63
2 changed files with 33 additions and 20 deletions

View file

@ -137,21 +137,30 @@ class HostingManageVMsAdmin(admin.ModelAdmin):
# Creating VM by using method allocate(client, template) # Creating VM by using method allocate(client, template)
def create_vm(self, request): def create_vm(self, request):
message = '' message = ''
try : # check if the request contains the template parameter, if it is
# Lets create a test VM with 128MB of ram and 1 CPU # not set warn the user of setting this.
vm_id = oca.VirtualMachine.allocate(self.client, '<VM><MEMORY>128</MEMORY><CPU>1</CPU></VM>') vm_template = request.POST.get('vm_template')
message = "Created with id = " + str(vm_id) if vm_template == 'select' :
vm_pool = self.get_vms() messages.add_message(request, messages.ERROR, "Please select a vm template")
messages.add_message(request, messages.SUCCESS, message) else :
# Lets print the VMs available in the pool try :
# print("Printing the available VMs in the pool.") # We do have the vm_template param set. Get and parse it
# vm_pool = oca.VirtualMachinePool(client) # and check it to be in the desired range.
# for vm in vm_pool: vm_template_int = int(vm_template)
# print("%s (memory: %s MB)" % ( vm.name, vm.template.memory)) if vm_template_int >=1 and vm_template_int <= 8:
except socket.timeout: # Lets create a test VM with 128MB of ram and 1 CPU
messages.add_message(request, messages.ERROR, "Socket timeout error.") vm_id = oca.VirtualMachine.allocate(self.client, '<VM><MEMORY>' + str(1024 * vm_template_int) + '</MEMORY><VCPU>' + str(vm_template_int)+ '</VCPU><CPU>' + str(0.1 * vm_template_int) + '</CPU><DISK><TYPE>fs</TYPE><SIZE>' + str(10000 * vm_template_int) + '</SIZE></DISK></VM>')
except OpenNebulaException: message = "Created with id = " + str(vm_id)
messages.add_message(request, messages.ERROR, "OpenNebulaException occurred.") vm_pool = self.get_vms()
messages.add_message(request, messages.SUCCESS, message)
else:
messages.add_message(request, messages.ERROR, "Please select an appropriate value for vm template.")
except socket.timeout:
messages.add_message(request, messages.ERROR, "Socket timeout error.")
except OpenNebulaException:
messages.add_message(request, messages.ERROR, "OpenNebulaException occurred.")
except ValueError:
messages.add_message(request, messages.ERROR, "Please select an appropriate value for vm template.")
return redirect('admin:showvms') return redirect('admin:showvms')
# Retrives virtual machine pool information # Retrives virtual machine pool information

View file

@ -1,11 +1,15 @@
{% extends "admin/base_site.html" %} {% extends "admin/base_site.html" %}
{% load staticfiles bootstrap3 i18n %}
{% block content %} {% block content %}
<form action="{% url 'admin:createvm' %}" method="post">
{% csrf_token %}
<a href="{% url 'admin:createvm' %}">Create VM</a> <select id="vm_template" name="vm_template">
<option value="select">Select a template</option>
<option value="1">disk = 10GB, vcpu=1, ram=2GB</option>
<option value="2">disk = 20GB, vcpu=2, ram=4GB</option>
</select>
<input type="submit" name="create_vm" value="Create VM" />
</form>
{% if vms %} {% if vms %}
<section> <section>
<table class="table table-striped"> <table class="table table-striped">