From d64b6329abf37e4124dd80ae62bfff16ae2ed19b Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 15 Apr 2019 23:17:38 +0200 Subject: [PATCH] Fallback to obtain VM_ID from order if not in metadata --- hosting/views.py | 93 +++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index fe13ff21..da139f0b 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1234,6 +1234,7 @@ class InvoiceDetailView(LoginRequiredMixin, DetailView): def get_object(self, queryset=None): invoice_id = self.kwargs.get('invoice_id') + logger.debug("Getting invoice for %s" % invoice_id) try: invoice_obj = MonthlyHostingBill.objects.get( invoice_number=invoice_id @@ -1262,33 +1263,22 @@ class InvoiceDetailView(LoginRequiredMixin, DetailView): if obj is not None: vm_id = obj.get_vm_id() - try: - # Try to get vm details from database - vm_detail = VMDetail.objects.get(vm_id=vm_id) - context['vm'] = vm_detail.__dict__ - context['vm']['name'] = '{}-{}'.format( - context['vm']['configuration'], context['vm']['vm_id']) - price, vat, vat_percent, discount = get_vm_price_with_vat( - cpu=context['vm']['cores'], - ssd_size=context['vm']['disk_size'], - memory=context['vm']['memory'], - pricing_name=(obj.order.vm_pricing.name - if obj.order.vm_pricing else 'default') - ) - context['vm']['vat'] = vat - context['vm']['price'] = price - context['vm']['discount'] = discount - context['vm']['vat_percent'] = vat_percent - context['vm']['total_price'] = price + vat - discount['amount'] - except VMDetail.DoesNotExist: - # fallback to get it from the infrastructure + if vm_id is None: + # We did not find it in the metadata, fallback to order + if obj.order is not None: + vm_id = obj.order.vm_id + logger.debug("VM ID from order is %s" % vm_id) + else: + logger.debug("VM order is None. So, we don't have VM_ID") + else: + logger.debug("VM ID was set in metadata") + if vm_id > 0: try: - manager = OpenNebulaManager( - email=self.request.email, - password=self.request.password - ) - vm = manager.get_vm(vm_id) - context['vm'] = VirtualMachineSerializer(vm).data + # Try to get vm details from database + vm_detail = VMDetail.objects.get(vm_id=vm_id) + context['vm'] = vm_detail.__dict__ + context['vm']['name'] = '{}-{}'.format( + context['vm']['configuration'], context['vm']['vm_id']) price, vat, vat_percent, discount = get_vm_price_with_vat( cpu=context['vm']['cores'], ssd_size=context['vm']['disk_size'], @@ -1300,20 +1290,43 @@ class InvoiceDetailView(LoginRequiredMixin, DetailView): context['vm']['price'] = price context['vm']['discount'] = discount context['vm']['vat_percent'] = vat_percent - context['vm']['total_price'] = ( - price + vat - discount['amount'] - ) - except WrongIdError: - logger.error("WrongIdError while accessing " - "invoice {}".format(obj.invoice_id)) - messages.error( - self.request, - _('The VM you are looking for is unavailable at the ' - 'moment. Please contact Data Center Light support.') - ) - self.kwargs['error'] = 'WrongIdError' - context['error'] = 'WrongIdError' - return context + context['vm']['total_price'] = price + vat - discount['amount'] + except VMDetail.DoesNotExist: + # fallback to get it from the infrastructure + try: + manager = OpenNebulaManager( + email=self.request.email, + password=self.request.password + ) + vm = manager.get_vm(vm_id) + context['vm'] = VirtualMachineSerializer(vm).data + price, vat, vat_percent, discount = get_vm_price_with_vat( + cpu=context['vm']['cores'], + ssd_size=context['vm']['disk_size'], + memory=context['vm']['memory'], + pricing_name=(obj.order.vm_pricing.name + if obj.order.vm_pricing else 'default') + ) + context['vm']['vat'] = vat + context['vm']['price'] = price + context['vm']['discount'] = discount + context['vm']['vat_percent'] = vat_percent + context['vm']['total_price'] = ( + price + vat - discount['amount'] + ) + except WrongIdError: + logger.error("WrongIdError while accessing " + "invoice {}".format(obj.invoice_id)) + messages.error( + self.request, + _('The VM you are looking for is unavailable at the ' + 'moment. Please contact Data Center Light support.') + ) + self.kwargs['error'] = 'WrongIdError' + context['error'] = 'WrongIdError' + return context + else: + logger.debug("No VM_ID. So, no details available.") # add context params from monthly hosting bill context['period_start'] = obj.get_period_start()