| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  | import decimal | 
					
						
							| 
									
										
										
										
											2017-09-25 00:02:36 +05:30
										 |  |  | import logging | 
					
						
							|  |  |  | from oca.pool import WrongIdError | 
					
						
							| 
									
										
										
										
											2017-08-31 18:35:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 13:31:55 +02:00
										 |  |  | from datacenterlight.models import VMPricing | 
					
						
							| 
									
										
										
										
											2017-09-25 00:02:36 +05:30
										 |  |  | from hosting.models import UserHostingKey, VMDetail | 
					
						
							|  |  |  | from opennebula_api.serializers import VirtualMachineSerializer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = logging.getLogger(__name__) | 
					
						
							| 
									
										
										
										
											2017-08-31 18:35:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_all_public_keys(customer): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Returns all the public keys of the user | 
					
						
							|  |  |  |     :param customer: The customer whose public keys are needed | 
					
						
							|  |  |  |     :return: A list of public keys | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2017-09-05 03:29:12 +05:30
										 |  |  |     return UserHostingKey.objects.filter(user_id=customer.id).values_list( | 
					
						
							|  |  |  |         "public_key", flat=True) | 
					
						
							| 
									
										
										
										
											2017-09-23 03:16:18 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-25 00:02:36 +05:30
										 |  |  | def get_or_create_vm_detail(user, manager, vm_id): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Returns VMDetail object related to given vm_id. Creates the object | 
					
						
							|  |  |  |      if it does not exist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :param vm_id: The ID of the VM which should be greater than 0. | 
					
						
							|  |  |  |     :param user: The CustomUser object that owns this VM | 
					
						
							|  |  |  |     :param manager: The OpenNebulaManager object | 
					
						
							|  |  |  |     :return: The VMDetail object. None if vm_id is less than or equal to 0. | 
					
						
							|  |  |  |     Also, for the cases where the VMDetail does not exist and we can not | 
					
						
							|  |  |  |     fetch data about the VM from OpenNebula, the function returns None | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if vm_id <= 0: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         vm_detail_obj = VMDetail.objects.get(vm_id=vm_id) | 
					
						
							|  |  |  |     except VMDetail.DoesNotExist: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             vm_obj = manager.get_vm(vm_id) | 
					
						
							|  |  |  |         except (WrongIdError, ConnectionRefusedError) as e: | 
					
						
							|  |  |  |             logger.error(str(e)) | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |         vm = VirtualMachineSerializer(vm_obj).data | 
					
						
							|  |  |  |         vm_detail_obj = VMDetail.objects.create( | 
					
						
							|  |  |  |             user=user, vm_id=vm_id, disk_size=vm['disk_size'], | 
					
						
							|  |  |  |             cores=vm['cores'], memory=vm['memory'], | 
					
						
							|  |  |  |             configuration=vm['configuration'], ipv4=vm['ipv4'], | 
					
						
							|  |  |  |             ipv6=vm['ipv6'] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     return vm_detail_obj | 
					
						
							| 
									
										
										
										
											2017-09-28 01:35:09 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 13:31:55 +02:00
										 |  |  | def get_vm_price(cpu, memory, disk_size, hdd_size=0, pricing_name='default'): | 
					
						
							| 
									
										
										
										
											2017-09-23 03:16:18 +05:30
										 |  |  |     """
 | 
					
						
							|  |  |  |     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 | 
					
						
							| 
									
										
										
										
											2018-04-15 13:31:55 +02:00
										 |  |  |     :param disk_size: Disk space of the VM (SSD) | 
					
						
							|  |  |  |     :param hdd_size: The HDD size | 
					
						
							|  |  |  |     :param pricing_name: The pricing name to be used | 
					
						
							| 
									
										
										
										
											2017-09-23 03:16:18 +05:30
										 |  |  |     :return: The price of the VM | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2018-04-15 13:31:55 +02:00
										 |  |  |     try: | 
					
						
							|  |  |  |         pricing = VMPricing.objects.get(name=pricing_name) | 
					
						
							|  |  |  |     except Exception as ex: | 
					
						
							|  |  |  |         logger.error( | 
					
						
							|  |  |  |             "Error getting VMPricing object for {pricing_name}." | 
					
						
							|  |  |  |             "Details: {details}".format( | 
					
						
							|  |  |  |                 pricing_name=pricing_name, details=str(ex) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  |     price = ((decimal.Decimal(cpu) * pricing.cores_unit_price) + | 
					
						
							| 
									
										
										
										
											2018-04-17 20:50:41 +02:00
										 |  |  |              (decimal.Decimal(memory) * pricing.ram_unit_price) + | 
					
						
							|  |  |  |              (decimal.Decimal(disk_size) * pricing.ssd_unit_price) + | 
					
						
							|  |  |  |              (decimal.Decimal(hdd_size) * pricing.hdd_unit_price)) | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  |     cents = decimal.Decimal('.01') | 
					
						
							|  |  |  |     price = price.quantize(cents, decimal.ROUND_HALF_UP) | 
					
						
							|  |  |  |     return float(price) | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  | def get_vm_price_with_vat(cpu, memory, ssd_size, hdd_size=0, | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  |                           pricing_name='default'): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     A helper function that computes price of a VM from given cpu, ram and | 
					
						
							|  |  |  |     ssd, hdd and the pricing parameters | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :param cpu: Number of cores of the VM | 
					
						
							|  |  |  |     :param memory: RAM of the VM | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  |     :param ssd_size: Disk space of the VM (SSD) | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  |     :param hdd_size: The HDD size | 
					
						
							|  |  |  |     :param pricing_name: The pricing name to be used | 
					
						
							| 
									
										
										
										
											2018-04-17 21:36:08 +02:00
										 |  |  |     :return: The a tuple containing the price of the VM, the VAT and the | 
					
						
							|  |  |  |              VAT percentage | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         pricing = VMPricing.objects.get(name=pricing_name) | 
					
						
							|  |  |  |     except Exception as ex: | 
					
						
							|  |  |  |         logger.error( | 
					
						
							|  |  |  |             "Error getting VMPricing object for {pricing_name}." | 
					
						
							|  |  |  |             "Details: {details}".format( | 
					
						
							|  |  |  |                 pricing_name=pricing_name, details=str(ex) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-07 05:07:58 +05:30
										 |  |  |     price = ( | 
					
						
							|  |  |  |         (decimal.Decimal(cpu) * pricing.cores_unit_price) + | 
					
						
							|  |  |  |         (decimal.Decimal(memory) * pricing.ram_unit_price) + | 
					
						
							|  |  |  |         (decimal.Decimal(ssd_size) * pricing.ssd_unit_price) + | 
					
						
							| 
									
										
										
										
											2018-05-07 06:11:44 +05:30
										 |  |  |         (decimal.Decimal(hdd_size) * pricing.hdd_unit_price) | 
					
						
							| 
									
										
										
										
											2018-05-07 05:07:58 +05:30
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  |     if pricing.vat_inclusive: | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  |         vat = decimal.Decimal(0) | 
					
						
							| 
									
										
										
										
											2018-04-17 21:36:08 +02:00
										 |  |  |         vat_percent = decimal.Decimal(0) | 
					
						
							| 
									
										
										
										
											2018-04-15 20:59:31 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  |         vat = price * pricing.vat_percentage * decimal.Decimal(0.01) | 
					
						
							| 
									
										
										
										
											2018-04-17 21:36:08 +02:00
										 |  |  |         vat_percent = pricing.vat_percentage | 
					
						
							| 
									
										
										
										
											2018-04-16 03:25:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     cents = decimal.Decimal('.01') | 
					
						
							|  |  |  |     price = price.quantize(cents, decimal.ROUND_HALF_UP) | 
					
						
							|  |  |  |     vat = vat.quantize(cents, decimal.ROUND_HALF_UP) | 
					
						
							| 
									
										
										
										
											2018-05-07 06:25:50 +05:30
										 |  |  |     discount = { | 
					
						
							|  |  |  |         'name': pricing.discount_name, | 
					
						
							|  |  |  |         'amount': float(pricing.discount_amount), | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return float(price), float(vat), float(vat_percent), discount | 
					
						
							| 
									
										
										
										
											2017-10-29 13:14:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostingUtils: | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def clear_items_from_list(from_list, items_list): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         A utility function to clear items from a given list. | 
					
						
							|  |  |  |         Useful when deleting items in bulk from session. | 
					
						
							|  |  |  |         e.g.: | 
					
						
							|  |  |  |         HostingUtils.clear_items_from_list( | 
					
						
							|  |  |  |             request.session, | 
					
						
							|  |  |  |             ['token', 'billing_address_data', 'card_id',] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         :param from_list: | 
					
						
							|  |  |  |         :param items_list: | 
					
						
							|  |  |  |         :return: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         for var in items_list: | 
					
						
							|  |  |  |             if var in from_list: | 
					
						
							|  |  |  |                 del from_list[var] |