integrating hosting app with opennebula integration

This commit is contained in:
Levi 2017-04-29 12:39:55 -05:00
parent 56e6e43742
commit 085133fb53
2 changed files with 55 additions and 14 deletions

View file

@ -15,6 +15,9 @@ from .managers import VMPlansManager
class VirtualMachineType(models.Model):
BASE_PRICE = 0.5
HETZNER_NUG = 'hetzner_nug'
HETZNER = 'hetzner'
HETZNER_R6 = 'hetzner_raid6'
@ -35,7 +38,6 @@ class VirtualMachineType(models.Model):
(DE_LOCATION, 'Germany'),
(CH_LOCATION, 'Switzerland'),
)
description = models.TextField()
base_price = models.FloatField()
memory_price = models.FloatField()
@ -52,11 +54,27 @@ class VirtualMachineType(models.Model):
return [vm.get_serialized_data()
for vm in cls.objects.all()]
def calculate_price(self, specifications):
price = float(specifications['cores']) * self.core_price
price += float(specifications['memory']) * self.memory_price
price += float(specifications['disk_size']) * self.disk_size_price
price += self.base_price
# def calculate_price(self, specifications):
# price = float(specifications['cores']) * self.core_price
# price += float(specifications['memory']) * self.memory_price
# price += float(specifications['disk_size']) * self.disk_size_price
# price += self.base_price
# return price
@classmethod
def get_price(cls, vm_template):
return cls.BASE_PRICE * vm_template
@classmethod
def get_specs(cls, vm_template):
return {
'memory': 1024 * vm_template,
'cores': 0.1 * vm_template,
'disk_size': 10000 * vm_template
}
def calculate_price(self, vm_template):
price = self.base_price * vm_template
return price
def defeault_price(self):

View file

@ -1,3 +1,4 @@
from collections import namedtuple
from django.shortcuts import render
from django.core.urlresolvers import reverse_lazy, reverse
@ -22,6 +23,7 @@ from utils.mailer import BaseEmail
from .models import VirtualMachineType, VirtualMachinePlan, HostingOrder
from .forms import HostingUserSignupForm, HostingUserLoginForm
from .mixins import ProcessVMSelectionMixin
from .opennebula_functions import HostingManageVMAdmin
class DjangoHostingView(ProcessVMSelectionMixin, View):
@ -246,18 +248,26 @@ class PaymentVMView(LoginRequiredMixin, FormView):
if form.is_valid():
context = self.get_context_data()
specifications = request.session.get('vm_specs')
vm_type = specifications.get('hosting_company')
vm = VirtualMachineType.objects.get(hosting_company=vm_type)
final_price = vm.calculate_price(specifications)
vm_template = specifications.get('vm_template', 1)
vm_type = VirtualMachineType.objects.first()
final_price = VirtualMachineType.get_price(vm_template)
specs = VirtualMachineType.get_specs(vm_template)
plan_data = {
'vm_type': vm,
'cores': specifications.get('cores'),
'memory': specifications.get('memory'),
'disk_size': specifications.get('disk_size'),
'configuration': specifications.get('configuration'),
'vm_type': vm_type,
'configuration': specifications.get(
'configuration',
'django'
),
'price': final_price
}
plan_data.update(specs)
token = form.cleaned_data.get('token')
# Get or create stripe customer
@ -299,6 +309,19 @@ class PaymentVMView(LoginRequiredMixin, FormView):
# If the Stripe payment was successed, set order status approved
order.set_approved()
# Create VM using oppenebula functions
_request = namedtuple('request', 'POST user')
_request.user = request.user
# user = namedtuple('user', 'email')
# email
_request.POST = {
'vm_template': vm_template
}
hosting_admin = HostingManageVMAdmin.__new__(HostingManageVMAdmin)
hosting_admin.init_opennebula_client(_request)
hosting_admin.create(_request)
# Send notification to ungleich as soon as VM has been booked
context = {
'vm': plan,