Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame^] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VPGWUService_decl |
| 3 | from models_decl import VPGWUTenant_decl |
| 4 | |
| 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, SlicePrivilege, SitePrivilege |
| 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 sets import Set |
| 18 | from xos.config import Config |
| 19 | from django.contrib.contenttypes.models import ContentType |
| 20 | from django.contrib.contenttypes.fields import GenericForeignKey |
| 21 | |
| 22 | class VPGWUService(VPGWUService_decl): |
| 23 | class Meta: |
| 24 | proxy = True |
| 25 | |
| 26 | class VPGWUTenant(VPGWUTenant_decl): |
| 27 | class Meta: |
| 28 | proxy = True |
| 29 | |
| 30 | def __init__(self, *args, **kwargs): |
| 31 | vpgwuservice = VPGWUService.get_service_objects().all() |
| 32 | # When the tenant is created the default service in the form is set |
| 33 | # to be the first created HelloWorldServiceComplete |
| 34 | if vpgwuservice: |
| 35 | self._meta.get_field( |
| 36 | "provider_service").default = vpgwuservice[0].id |
| 37 | super(VPGWUTenant, self).__init__(*args, **kwargs) |
| 38 | |
| 39 | def save(self, *args, **kwargs): |
| 40 | # Update the instance that was created for this tenant |
| 41 | super(VPGWUTenant, self).save(*args, **kwargs) |
| 42 | model_policy_vpgwutenant(self.pk) |
| 43 | |
| 44 | def delete(self, *args, **kwargs): |
| 45 | # Delete the instance that was created for this tenant |
| 46 | self.cleanup_container() |
| 47 | super(VPGWUTenant, self).delete(*args, **kwargs) |
| 48 | |
| 49 | def model_policy_vpgwutenant(pk): |
| 50 | # This section of code is atomic to prevent race conditions |
| 51 | with transaction.atomic(): |
| 52 | # We find all of the tenants that are waiting to update |
| 53 | tenant = VPGWUTenant.objects.select_for_update().filter(pk=pk) |
| 54 | if not tenant: |
| 55 | return |
| 56 | # Since this code is atomic it is safe to always use the first tenant |
| 57 | tenant = tenant[0] |
| 58 | tenant.manage_container() |