From c92960a3e61ff6ed83a668cce42c0c649ae2debc Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 19:38:38 +0200 Subject: [PATCH 1/9] Added VMTemplate model --- datacenterlight/models.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/datacenterlight/models.py b/datacenterlight/models.py index 605a944c..0c6a586a 100644 --- a/datacenterlight/models.py +++ b/datacenterlight/models.py @@ -9,12 +9,13 @@ class BetaAccessVMType(models.Model): def __str__(self): return "ID: %s - SSD %s - RAM %s - CPU %s - Price %s " % \ - (self.id, str(self.ssd), self.ram, self.cpu, self.price) + (self.id, str(self.ssd), self.ram, self.cpu, self.price) class BetaAccess(models.Model): email = models.CharField(max_length=250) name = models.CharField(max_length=250) + # vm = models.ForeignKey(BetaAccessVM) def __str__(self): @@ -48,3 +49,13 @@ class BetaAccessVM(models.Model): amount=vm[VM_AMOUNT], type=vm_type)) return created_vms + + +class VMTemplate(models.Model): + name = models.CharField(max_length=50) + opennebula_vm_id = models.IntegerField() + + @classmethod + def create(cls, name, opennebula_vm_id): + vm_template = cls(name=name, opennebula_vm_id=opennebula_vm_id) + return vm_template From bc2ad28969a98a7e55afb5eb38456243aef68144 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 19:41:05 +0200 Subject: [PATCH 2/9] Fixed bug where we need to fetch VMs whose names strictly begin with 'public-' --- opennebula_api/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 383062f1..60f3159c 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -59,7 +59,7 @@ class OpenNebulaManager(): domain=settings.OPENNEBULA_DOMAIN, port=settings.OPENNEBULA_PORT, endpoint=settings.OPENNEBULA_ENDPOINT - )) + )) def _get_opennebula_client(self, username, password): return oca.Client("{0}:{1}".format( @@ -71,7 +71,7 @@ class OpenNebulaManager(): domain=settings.OPENNEBULA_DOMAIN, port=settings.OPENNEBULA_PORT, endpoint=settings.OPENNEBULA_ENDPOINT - )) + )) def _get_user(self, user): """Get the corresponding opennebula user for a CustomUser object @@ -325,7 +325,7 @@ class OpenNebulaManager(): public_templates = [ template for template in self._get_template_pool() - if 'public-' in template.name + if template.name.startswith('public-') ] return public_templates except ConnectionRefusedError: @@ -396,7 +396,7 @@ class OpenNebulaManager(): def delete_template(self, template_id): self.oneadmin_client.call(oca.VmTemplate.METHODS[ - 'delete'], template_id, False) + 'delete'], template_id, False) def change_user_password(self, new_password): self.oneadmin_client.call( From 762c8b14cca3936925b8217ea20ea1f6aa4f8533 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 19:52:02 +0200 Subject: [PATCH 3/9] Added fetchvmtemplates management command in datacenterlight --- .../management/commands/fetchvmtemplates.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 datacenterlight/management/commands/fetchvmtemplates.py diff --git a/datacenterlight/management/commands/fetchvmtemplates.py b/datacenterlight/management/commands/fetchvmtemplates.py new file mode 100644 index 00000000..15b76fc1 --- /dev/null +++ b/datacenterlight/management/commands/fetchvmtemplates.py @@ -0,0 +1,29 @@ +from django.core.management.base import BaseCommand +from opennebula_api.models import OpenNebulaManager +from datacenterlight.models import VMTemplate +import logging + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = 'Fetches the VM templates from OpenNebula and populates the dcl VMTemplate model' + + def handle(self, *args, **options): + try: + manager = OpenNebulaManager() + templates = manager.get_templates() + dcl_vm_templates = [] + for template in templates: + template_name = template.name.strip('public-') + template_id = template.id + dcl_vm_template = VMTemplate.create(template_name, template_id) + dcl_vm_templates.append(dcl_vm_template) + + old_vm_templates = VMTemplate.objects.all() + old_vm_templates.delete() + + for dcl_vm_template in dcl_vm_templates: + dcl_vm_template.save() + except Exception as e: + logger.error('Error connecting to OpenNebula. Error Details: {err}'.format(err=str(e))) From 71e4fece58ed685ab1362ea6e40f3861f78b1e95 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 20:35:12 +0200 Subject: [PATCH 4/9] Renamed opennebula_vm_id to opennebula_vm_template_id --- datacenterlight/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datacenterlight/models.py b/datacenterlight/models.py index 0c6a586a..fdfebc96 100644 --- a/datacenterlight/models.py +++ b/datacenterlight/models.py @@ -53,9 +53,9 @@ class BetaAccessVM(models.Model): class VMTemplate(models.Model): name = models.CharField(max_length=50) - opennebula_vm_id = models.IntegerField() + opennebula_vm_template_id = models.IntegerField() @classmethod - def create(cls, name, opennebula_vm_id): - vm_template = cls(name=name, opennebula_vm_id=opennebula_vm_id) + def create(cls, name, opennebula_vm_template_id): + vm_template = cls(name=name, opennebula_vm_template_id=opennebula_vm_template_id) return vm_template From f3cfe19e68f32d9f94d3e1f3e966acdc33dd44f5 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 20:38:53 +0200 Subject: [PATCH 5/9] Reformatted the template file and used opennebula_vm_template_id instead of template_id --- .../datacenterlight/calculator_form.html | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/calculator_form.html b/datacenterlight/templates/datacenterlight/calculator_form.html index de97f295..009a358d 100644 --- a/datacenterlight/templates/datacenterlight/calculator_form.html +++ b/datacenterlight/templates/datacenterlight/calculator_form.html @@ -2,7 +2,7 @@
{% csrf_token %}
-

{% trans "VM hosting" %}

+

{% trans "VM hosting" %}

15 @@ -17,28 +17,31 @@
- - - Core - + + + Core +
- - - GB RAM - + + + GB RAM +
- - - {% trans "GB Storage (SSD)" %} - + + + {% trans "GB Storage (SSD)" %} +
@@ -46,7 +49,7 @@ @@ -56,31 +59,38 @@ -->
- - + +
{% for message in messages %} - {% if 'name' in message.tags %} -
  • + {% if 'name' in message.tags %} +
      +
    • {{ message|safe }} -
    - {% endif %} +
  • +
+ {% endif %} {% endfor %}
- - + +
{% for message in messages %} - {% if 'email' in message.tags %} -
  • + {% if 'email' in message.tags %} +
      +
    • {{ message|safe }} -
    - {% endif %} +
  • +
+ {% endif %} {% endfor %}
From 1f58da70d477cb07d7fae9e3ac14a99c923f4764 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 20:39:51 +0200 Subject: [PATCH 6/9] Removed OpenNebula code from the IndexView and code reformatted --- datacenterlight/views.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 6b0e0d7e..0c09d74e 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -1,7 +1,7 @@ from django.views.generic import FormView, CreateView, TemplateView, DetailView from django.http import HttpResponseRedirect from .forms import BetaAccessForm -from .models import BetaAccess, BetaAccessVMType, BetaAccessVM +from .models import BetaAccess, BetaAccessVMType, BetaAccessVM, VMTemplate from django.contrib import messages from django.core.urlresolvers import reverse from django.core.mail import EmailMessage @@ -199,20 +199,11 @@ class IndexView(CreateView): for session_var in ['specs', 'user', 'billing_address_data']: if session_var in request.session: del request.session[session_var] - try: - manager = OpenNebulaManager() - templates = manager.get_templates() - context = { - 'templates': VirtualMachineTemplateSerializer(templates, many=True).data - } - except: - messages.error(request, - 'We have a temporary problem to connect to our backend. \ - Please try again in a few minutes' - ) - context = { - 'error': 'connection' - } + + vm_templates = VMTemplate.objects.all() + context = { + 'templates': vm_templates + } return render(request, self.template_name, context) def post(self, request): @@ -329,7 +320,7 @@ class WhyDataCenterLightView(IndexView): ) context = { 'error': 'connection' - } + } return render(request, self.template_name, context) @@ -462,9 +453,9 @@ class OrderConfirmationView(DetailView): template_id=vm_template_id, specs=specs, vm_name="{email}-{template_name}-{date}".format( - email=user.get('email'), - template_name=template.get('name'), - date=int(datetime.now().strftime("%s"))) + email=user.get('email'), + template_name=template.get('name'), + date=int(datetime.now().strftime("%s"))) ) # Create a Hosting Order From 8485819caaf54d342cd24f0dd98b0e40323edc98 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Wed, 26 Jul 2017 20:58:08 +0200 Subject: [PATCH 7/9] Removed unneeded code from datacenterlight/views.py --- datacenterlight/views.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 0c09d74e..57f7d32a 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -304,25 +304,6 @@ class WhyDataCenterLightView(IndexView): template_name = "datacenterlight/whydatacenterlight.html" model = BetaAccess - @cache_control(no_cache=True, must_revalidate=True, no_store=True) - def get(self, request, *args, **kwargs): - try: - manager = OpenNebulaManager() - templates = manager.get_templates() - context = { - 'templates': VirtualMachineTemplateSerializer(templates, many=True).data, - } - except: - messages.error( - request, - 'We have a temporary problem to connect to our backend. \ - Please try again in a few minutes' - ) - context = { - 'error': 'connection' - } - return render(request, self.template_name, context) - class PaymentOrderView(FormView): template_name = 'hosting/payment.html' From 1a6e497e385a1b4c6bea0033ea6d764668e48f4b Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 27 Jul 2017 11:25:48 +0530 Subject: [PATCH 8/9] Removed another openebula call after submitting landing page calculator --- datacenterlight/views.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 57f7d32a..401be537 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -212,9 +212,7 @@ class IndexView(CreateView): storage = request.POST.get('storage') price = request.POST.get('total') template_id = int(request.POST.get('config')) - manager = OpenNebulaManager() - template = manager.get_template(template_id) - template_data = VirtualMachineTemplateSerializer(template).data + template_data = VMTemplate.objects.all() name = request.POST.get('name') email = request.POST.get('email') From fca291c6a5afbf548f520c560fb87ba8097679ff Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 27 Jul 2017 12:05:01 +0530 Subject: [PATCH 9/9] Added VMTemplateSerializer --- datacenterlight/views.py | 5 +++-- opennebula_api/serializers.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 401be537..017a1f3a 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -21,7 +21,7 @@ from datetime import datetime from membership.models import CustomUser, StripeCustomer from opennebula_api.models import OpenNebulaManager -from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer +from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer, VMTemplateSerializer class LandingProgramView(TemplateView): @@ -212,7 +212,8 @@ class IndexView(CreateView): storage = request.POST.get('storage') price = request.POST.get('total') template_id = int(request.POST.get('config')) - template_data = VMTemplate.objects.all() + template = VMTemplate.objects.filter(opennebula_vm_template_id=template_id).first() + template_data = VMTemplateSerializer(template).data name = request.POST.get('name') email = request.POST.get('email') diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 16ab1e76..12b313af 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -129,6 +129,12 @@ class VirtualMachineSerializer(serializers.Serializer): return obj.name.strip('public-') +class VMTemplateSerializer(serializers.Serializer): + """Serializer to map the VMTemplate instance into JSON format.""" + id = serializers.IntegerField(read_only=True, source='opennebula_vm_template_id') + name = serializers.CharField(read_only=True) + + def hexstr2int(string): return int(string.replace(':', ''), 16)