From 6d4e9d8a83ab5a87b1c09e56464eb213f3cc9244 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 7 May 2017 05:31:39 +0530 Subject: [PATCH] Refactored code to introduce different VM templates --- hosting/opennebula_functions.py | 58 ++++++++++++++++++++++++ hosting/templates/hosting/managevms.html | 10 ++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/hosting/opennebula_functions.py b/hosting/opennebula_functions.py index f71d7249..7759096f 100644 --- a/hosting/opennebula_functions.py +++ b/hosting/opennebula_functions.py @@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _ from oca.exceptions import OpenNebulaException from oca.pool import WrongNameError +from django import forms # Get an instance of a logger logger = logging.getLogger(__name__) @@ -84,6 +85,7 @@ class HostingManageVMAdmin(admin.ModelAdmin): # Include common variables for rendering the admin template. self.admin_site.each_context(request), vms=vm_pool, + form=HostingManageVMForm ) return TemplateResponse(request, "hosting/managevms.html", context) @@ -232,6 +234,62 @@ class HostingManageVMAdmin(admin.ModelAdmin): "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 def get_random_password(): diff --git a/hosting/templates/hosting/managevms.html b/hosting/templates/hosting/managevms.html index d1b76ac2..54270316 100644 --- a/hosting/templates/hosting/managevms.html +++ b/hosting/templates/hosting/managevms.html @@ -2,13 +2,9 @@ {% block content %}
- {% csrf_token %} - - + {% csrf_token %} + {{ form }} +
{% if vms %}