Added get_or_create_vm_detail function
This commit is contained in:
		
					parent
					
						
							
								f4899ffc18
							
						
					
				
			
			
				commit
				
					
						7278a20135
					
				
			
		
					 2 changed files with 41 additions and 3 deletions
				
			
		|  | @ -9,11 +9,11 @@ from django.utils import translation | ||||||
| from django.utils.translation import ugettext_lazy as _ | from django.utils.translation import ugettext_lazy as _ | ||||||
| 
 | 
 | ||||||
| from dynamicweb.celery import app | from dynamicweb.celery import app | ||||||
| from hosting.models import HostingOrder, HostingBill | from hosting.models import HostingOrder, HostingBill, VMDetail | ||||||
| from membership.models import StripeCustomer, CustomUser | from membership.models import StripeCustomer, CustomUser | ||||||
| from opennebula_api.models import OpenNebulaManager | from opennebula_api.models import OpenNebulaManager | ||||||
| from opennebula_api.serializers import VirtualMachineSerializer | from opennebula_api.serializers import VirtualMachineSerializer | ||||||
| from utils.hosting_utils import get_all_public_keys | from utils.hosting_utils import get_all_public_keys, get_or_create_vm_detail | ||||||
| from utils.forms import UserBillingAddressForm | from utils.forms import UserBillingAddressForm | ||||||
| from utils.mailer import BaseEmail | from utils.mailer import BaseEmail | ||||||
| from utils.models import BillingAddress | from utils.models import BillingAddress | ||||||
|  | @ -174,6 +174,7 @@ def create_vm_task(self, vm_template_id, user, specs, template, | ||||||
|             logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id)) |             logger.debug("New VM ID is {vm_id}".format(vm_id=vm_id)) | ||||||
|             if new_host is not None: |             if new_host is not None: | ||||||
|                 custom_user = CustomUser.objects.get(email=user.get('email')) |                 custom_user = CustomUser.objects.get(email=user.get('email')) | ||||||
|  |                 get_or_create_vm_detail(custom_user, manager, vm_id) | ||||||
|                 if custom_user is not None: |                 if custom_user is not None: | ||||||
|                     public_keys = get_all_public_keys(custom_user) |                     public_keys = get_all_public_keys(custom_user) | ||||||
|                     keys = [{'value': key, 'state': True} for key in |                     keys = [{'value': key, 'state': True} for key in | ||||||
|  |  | ||||||
|  | @ -1,5 +1,10 @@ | ||||||
| from hosting.models import UserHostingKey | import logging | ||||||
|  | from oca.pool import WrongIdError | ||||||
| 
 | 
 | ||||||
|  | from hosting.models import UserHostingKey, VMDetail | ||||||
|  | from opennebula_api.serializers import VirtualMachineSerializer | ||||||
|  | 
 | ||||||
|  | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| def get_all_public_keys(customer): | def get_all_public_keys(customer): | ||||||
|     """ |     """ | ||||||
|  | @ -9,3 +14,35 @@ def get_all_public_keys(customer): | ||||||
|     """ |     """ | ||||||
|     return UserHostingKey.objects.filter(user_id=customer.id).values_list( |     return UserHostingKey.objects.filter(user_id=customer.id).values_list( | ||||||
|         "public_key", flat=True) |         "public_key", flat=True) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 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 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue