From 3bb7f09d41a4f05550f4652a103aa341ad0e175d Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 23 Sep 2017 03:16:18 +0530 Subject: [PATCH 1/4] Refactored obtaining vm price to utils.hosting_utils.get_vm_price --- utils/hosting_utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/utils/hosting_utils.py b/utils/hosting_utils.py index 7c1a83ad..dae8e412 100644 --- a/utils/hosting_utils.py +++ b/utils/hosting_utils.py @@ -9,3 +9,16 @@ def get_all_public_keys(customer): """ return UserHostingKey.objects.filter(user_id=customer.id).values_list( "public_key", flat=True) + + +def get_vm_price(cpu, memory, disk_size): + """ + A helper function that computes price of a VM from given cpu, ram and + ssd parameters + + :param cpu: Number of cores of the VM + :param memory: RAM of the VM + :param disk_size: Disk space of the VM + :return: The price of the VM + """ + return (cpu * 5) + (memory * 2) + (disk_size * 0.6) From 285d66466052f84ac5348f3fd1582c60556108ed Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 23 Sep 2017 03:17:21 +0530 Subject: [PATCH 2/4] Refactored obtaining stripe plan name to utils.stripe_utils.get_stripe_plan_name --- utils/stripe_utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index f35a6b9c..cb4f6eff 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -238,7 +238,7 @@ class StripeUtils(object): @staticmethod def get_stripe_plan_id(cpu, ram, ssd, version, app='dcl', hdd=None): """ - Returns the stripe plan id string of the form + Returns the Stripe plan id string of the form `dcl-v1-cpu-2-ram-5gb-ssd-10gb` based on the input parameters :param cpu: The number of cores @@ -260,3 +260,14 @@ class StripeUtils(object): version=version, plan=dcl_plan_string) return stripe_plan_id_string + + @staticmethod + def get_stripe_plan_name(cpu, memory, disk_size): + """ + Returns the Stripe plan name + :return: + """ + return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( + cpu=cpu, + memory=memory, + disk_size=disk_size) From 99e5cf5587e203152b0d737c4273a98bffb4fa01 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 23 Sep 2017 03:18:05 +0530 Subject: [PATCH 3/4] Using refactored get_vm_price and get_vm_plan_name functions --- datacenterlight/tests.py | 12 ++++++------ datacenterlight/views.py | 12 ++++++------ hosting/views.py | 16 ++++++++-------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/datacenterlight/tests.py b/datacenterlight/tests.py index c34c56ba..edde2db8 100644 --- a/datacenterlight/tests.py +++ b/datacenterlight/tests.py @@ -12,6 +12,7 @@ from datacenterlight.models import VMTemplate from datacenterlight.tasks import create_vm_task from membership.models import StripeCustomer from opennebula_api.serializers import VMTemplateSerializer +from utils.hosting_utils import get_vm_price from utils.models import BillingAddress from utils.stripe_utils import StripeUtils @@ -94,12 +95,11 @@ class CeleryTaskTestCase(TestCase): cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') - amount_to_be_charged = (cpu * 5) + (memory * 2) + (disk_size * 0.6) - plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( - cpu=cpu, - memory=memory, - disk_size=disk_size) - + amount_to_be_charged = get_vm_price(cpu=cpu, memory=memory, + disk_size=disk_size) + plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, + memory=memory, + disk_size=disk_size) stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, ram=memory, ssd=disk_size, diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 0521ffef..152e3e75 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -17,6 +17,7 @@ from opennebula_api.models import OpenNebulaManager from opennebula_api.serializers import VirtualMachineTemplateSerializer, \ VMTemplateSerializer from utils.forms import BillingAddressForm +from utils.hosting_utils import get_vm_price from utils.mailer import BaseEmail from utils.stripe_utils import StripeUtils from utils.tasks import send_plain_email_task @@ -532,12 +533,11 @@ class OrderConfirmationView(DetailView): cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') - amount_to_be_charged = (cpu * 5) + (memory * 2) + (disk_size * 0.6) - plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( - cpu=cpu, - memory=memory, - disk_size=disk_size) - + amount_to_be_charged = get_vm_price(cpu=cpu, memory=memory, + disk_size=disk_size) + plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, + memory=memory, + disk_size=disk_size) stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, ram=memory, ssd=disk_size, diff --git a/hosting/views.py b/hosting/views.py index e1d2feb2..f91b2e5c 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -35,6 +35,7 @@ from utils.mailer import BaseEmail from utils.stripe_utils import StripeUtils from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, \ LoginViewMixin +from utils.hosting_utils import get_vm_price from .forms import HostingUserSignupForm, HostingUserLoginForm, \ UserHostingKeyForm, generate_ssh_key_name from .mixins import ProcessVMSelectionMixin @@ -724,12 +725,11 @@ class OrdersHostingDetailView(LoginRequiredMixin, cpu = specs.get('cpu') memory = specs.get('memory') disk_size = specs.get('disk_size') - amount_to_be_charged = (cpu * 5) + (memory * 2) + (disk_size * 0.6) - plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( - cpu=cpu, - memory=memory, - disk_size=disk_size) - + amount_to_be_charged = get_vm_price(cpu=cpu, memory=memory, + disk_size=disk_size) + plan_name = StripeUtils.get_stripe_plan_name(cpu=cpu, + memory=memory, + disk_size=disk_size) stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu, ram=memory, ssd=disk_size, @@ -775,8 +775,8 @@ class OrdersHostingDetailView(LoginRequiredMixin, 'redirect': reverse('hosting:virtual_machines'), 'msg_title': str(_('Thank you for the order.')), 'msg_body': str(_('Your VM will be up and running in a few moments.' - ' We will send you a confirmation email as soon as' - ' it is ready.')) + ' We will send you a confirmation email as soon as' + ' it is ready.')) } return HttpResponse(json.dumps(response), From 9ab7622a9c245c9c712e8f1ca3840487200fa3bf Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 23 Sep 2017 03:25:48 +0530 Subject: [PATCH 4/4] Reformatted stripe_utils.py --- utils/stripe_utils.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index cb4f6eff..98f85d62 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -256,9 +256,10 @@ class StripeUtils(object): if hdd is not None: dcl_plan_string = '{dcl_plan_string}-hdd-{hdd}gb'.format( dcl_plan_string=dcl_plan_string, hdd=hdd) - stripe_plan_id_string = '{app}-v{version}-{plan}'.format(app=app, - version=version, - plan=dcl_plan_string) + stripe_plan_id_string = '{app}-v{version}-{plan}'.format( + app=app, + version=version, + plan=dcl_plan_string) return stripe_plan_id_string @staticmethod @@ -268,6 +269,6 @@ class StripeUtils(object): :return: """ return "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format( - cpu=cpu, - memory=memory, - disk_size=disk_size) + cpu=cpu, + memory=memory, + disk_size=disk_size)