Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VMMEService_decl |
| 3 | from models_decl import VMMETenant_decl |
| 4 | |
Omar Abdelkader | 2c16284 | 2017-08-07 19:41:59 -0600 | [diff] [blame^] | 5 | from django.db import models |
| 6 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool |
| 7 | from core.models.plcorebase import StrippedCharField |
| 8 | import os |
| 9 | from django.db import models, transaction |
| 10 | from django.forms.models import model_to_dict |
| 11 | from django.db.models import * |
| 12 | from operator import itemgetter, attrgetter, methodcaller |
| 13 | from core.models import Tag |
| 14 | from core.models.service import LeastLoadedNodeScheduler |
| 15 | import traceback |
| 16 | from xos.exceptions import * |
| 17 | from xos.config import Config |
| 18 | from django.contrib.contenttypes.models import ContentType |
| 19 | from django.contrib.contenttypes.fields import GenericForeignKey |
| 20 | |
Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 21 | class VMMEService(VMMEService_decl): |
| 22 | class Meta: |
| 23 | proxy = True |
| 24 | |
| 25 | class VMMETenant(VMMETenant_decl): |
| 26 | class Meta: |
| 27 | proxy = True |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | vmmeservice = VMMEService.get_service_objects().all() |
| 31 | if vmmeservice: |
| 32 | self._meta.get_field( |
| 33 | "provider_service").default = vmmeservice[0].id |
| 34 | super(VMMETenant, self).__init__(*args, **kwargs) |
| 35 | |
| 36 | def save(self, *args, **kwargs): |
| 37 | super(VMMETenant, self).save(*args, **kwargs) |
| 38 | # This call needs to happen so that an instance is created for this |
| 39 | # tenant is created in the slice. One instance is created per tenant. |
| 40 | model_policy_vmmetenant(self.pk) |
| 41 | |
| 42 | def delete(self, *args, **kwargs): |
| 43 | # Delete the instance that was created for this tenant |
| 44 | self.cleanup_container() |
| 45 | super(VMMETenant, self).delete(*args, **kwargs) |
| 46 | |
| 47 | def model_policy_vmmetenant(pk): |
| 48 | # This section of code is atomic to prevent race conditions |
| 49 | with transaction.atomic(): |
| 50 | # We find all of the tenants that are waiting to update |
| 51 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 52 | if not tenant: |
| 53 | return |
| 54 | # Since this code is atomic it is safe to always use the first tenant |
| 55 | tenant = tenant[0] |
Omar Abdelkader | 2c16284 | 2017-08-07 19:41:59 -0600 | [diff] [blame^] | 56 | tenant.manage_container() |