Fallback to obtain VM_ID from order if not in metadata
This commit is contained in:
parent
3c215638f5
commit
d64b6329ab
1 changed files with 53 additions and 40 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue