Refactored code to introduce different VM templates

This commit is contained in:
M.Ravi 2017-05-07 05:31:39 +05:30
parent 58d3dd6624
commit 6d4e9d8a83
2 changed files with 61 additions and 7 deletions

View file

@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _
from oca.exceptions import OpenNebulaException from oca.exceptions import OpenNebulaException
from oca.pool import WrongNameError from oca.pool import WrongNameError
from django import forms
# Get an instance of a logger # Get an instance of a logger
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -84,6 +85,7 @@ class HostingManageVMAdmin(admin.ModelAdmin):
# Include common variables for rendering the admin template. # Include common variables for rendering the admin template.
self.admin_site.each_context(request), self.admin_site.each_context(request),
vms=vm_pool, vms=vm_pool,
form=HostingManageVMForm
) )
return TemplateResponse(request, "hosting/managevms.html", context) return TemplateResponse(request, "hosting/managevms.html", context)
@ -232,6 +234,62 @@ class HostingManageVMAdmin(admin.ModelAdmin):
"Error : {0}".format(err)) "Error : {0}".format(err))
logger.error("Error : {0}".format(err)) logger.error("Error : {0}".format(err))
def set_field_html_name(cls, new_name):
"""
This creates wrapper around the normal widget rendering,
allowing for a custom field name (new_name).
"""
old_render = cls.widget.render
def _widget_render_wrapper(name, value, attrs=None):
return old_render(new_name, value, attrs)
cls.widget.render = _widget_render_wrapper
class HostingManageVMForm(forms.Form):
vm_templates = []
VM_CHOICES = (('1', 'disk = 10GB, vcpu=1, ram=1GB'),
('2', 'disk = 20GB, vcpu=2, ram=2GB'),
('3', 'disk = 40GB, vcpu=4, ram=4GB'),
('4', 'disk = 80GB, vcpu=8, ram=8GB'),
('5', 'disk = 160GB, vcpu=16, ram=16GB'),
('6', 'disk = 320GB, vcpu=32, ram=32GB'),
('7', 'disk = 640GB, vcpu=64, ram=64GB'),
('8', 'disk = 1280GB, vcpu=128, ram=128GB'))
#for i in range(0,8):
# factor = pow(2, i)
# vm_templates.append(VMTemplate(i, VM_CHOICES[i], 10000 * factor, factor , 0.1 * factor, 1024 * factor))
field = forms.ChoiceField(label="Choose a VM Template ", choices=VM_CHOICES, widget=forms.Select(attrs={"id": "vm_template"}))
set_field_html_name(field, 'vm_template')
class ManageVMView(View):
template_name = "hosting/managevms.html"
context_object_name = "managevms"
model = HostingManageVMAdmin
def get(self, *args, **kwargs):
form = HostingManageVMsForm()
context = {
'vms': model.get_vm_pool(),
'form': form,
}
return TemplateResponse(request, template_name, context)
class VMTemplate:
"""A simple representation of a VM template.
:param template_id: The id of the template
:param label: A string representation describing the template. Used as the label in view
:param disk: VM disk space in MB
:param vcpu: Virtual cpu for the VM
:param cpu: CPU for the VM
:param ram: The RAM for the VM
"""
def __init__(self, template_id, label, disk, vcpu, cpu, ram):
self.template_id = template_id
self.label = label
self.disk = disk
self.vcpu = vcpu
self.cpu = cpu
# Returns random password that is needed by OpenNebula # Returns random password that is needed by OpenNebula
def get_random_password(): def get_random_password():

View file

@ -2,13 +2,9 @@
{% block content %} {% block content %}
<form action="{% url 'admin:createvm' %}" method="post"> <form action="{% url 'admin:createvm' %}" method="post">
{% csrf_token %} {% csrf_token %}
<select id="vm_template" name="vm_template"> {{ form }}
<option value="select">Select a template</option> <input type="submit" name="create_vm" value="Create VM" />
<option value="1">disk = 10GB, vcpu=1, ram=1GB</option>
<option value="2">disk = 20GB, vcpu=2, ram=2GB</option>
</select>
<input type="submit" name="create_vm" value="Create VM" />
</form> </form>
{% if vms %} {% if vms %}
<section> <section>