blob: 4da52cb3e59204a087c83fa6f3c120978f9049bc [file] [log] [blame]
Yunpeng Zhang0048cf42017-08-15 23:01:28 -04001from core.models.plcorebase import *
2from models_decl import VPGWUService_decl
3from models_decl import VPGWUTenant_decl
4
5from django.db import models
6from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, SlicePrivilege, SitePrivilege
7from core.models.plcorebase import StrippedCharField
8import os
9from django.db import models, transaction
10from django.forms.models import model_to_dict
11from django.db.models import *
12from operator import itemgetter, attrgetter, methodcaller
13from core.models import Tag
14from core.models.service import LeastLoadedNodeScheduler
15import traceback
16from xos.exceptions import *
17from sets import Set
18from xos.config import Config
19from django.contrib.contenttypes.models import ContentType
20from django.contrib.contenttypes.fields import GenericForeignKey
21
22class VPGWUService(VPGWUService_decl):
23 class Meta:
24 proxy = True
25
26class 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
49def 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()