blob: 3c8ef237bd4d610e7cd27190354db2ecbc660a69 [file] [log] [blame]
Yunpeng Zhanga2c27332017-08-15 23:00:11 -04001from core.models.plcorebase import *
2from models_decl import VSGWUService_decl
3from models_decl import VSGWUTenant_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
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 xos.config import Config
18from django.contrib.contenttypes.models import ContentType
19from django.contrib.contenttypes.fields import GenericForeignKey
20
21class VSGWUService(VSGWUService_decl):
22 class Meta:
23 proxy = True
24
25class VSGWUTenant(VSGWUTenant_decl):
26 class Meta:
27 proxy = True
28
29 def __init__(self, *args, **kwargs):
30 vsgwuservice = VSGWUService.get_service_objects().all()
31 if vsgwuservice:
32 self._meta.get_field(
33 "provider_service").default = vsgwuservice[0].id
34 super(VSGWUTenant, self).__init__(*args, **kwargs)
35
36 def save(self, *args, **kwargs):
37 super(VSGWUTenant, 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_vsgwutenant(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(VSGWUTenant, self).delete(*args, **kwargs)
46
47def model_policy_vsgwutenant(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 = VSGWUTenant.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]
56 tenant.manage_container()
57