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 |
Omar Abdelkader | 5612175 | 2017-09-06 19:40:17 -0500 | [diff] [blame] | 3 | from models_decl import VPGWUVendor_decl |
Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame] | 4 | from models_decl import VPGWUTenant_decl |
| 5 | |
| 6 | from django.db import models |
| 7 | from core.models import Service, PlCoreBase, Slice, Instance, Tenant, TenantWithContainer, Node, Image, User, Flavor, NetworkParameter, NetworkParameterType, Port, AddressPool, SlicePrivilege, SitePrivilege |
| 8 | from core.models.plcorebase import StrippedCharField |
| 9 | import os |
| 10 | from django.db import models, transaction |
| 11 | from django.forms.models import model_to_dict |
| 12 | from django.db.models import * |
| 13 | from operator import itemgetter, attrgetter, methodcaller |
| 14 | from core.models import Tag |
| 15 | from core.models.service import LeastLoadedNodeScheduler |
| 16 | import traceback |
| 17 | from xos.exceptions import * |
| 18 | from sets import Set |
| 19 | from xos.config import Config |
Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame] | 20 | |
| 21 | class VPGWUService(VPGWUService_decl): |
| 22 | class Meta: |
| 23 | proxy = True |
| 24 | |
Omar Abdelkader | cbcd943 | 2017-08-30 10:45:07 -0600 | [diff] [blame] | 25 | def create_tenant(self, **kwargs): |
| 26 | t = VPGWUTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 27 | t.save() |
| 28 | return t |
| 29 | |
Omar Abdelkader | 5612175 | 2017-09-06 19:40:17 -0500 | [diff] [blame] | 30 | class VPGWUVendor(VPGWUVendor_decl): |
| 31 | class Meta: |
| 32 | proxy = True |
| 33 | |
Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame] | 34 | class VPGWUTenant(VPGWUTenant_decl): |
| 35 | class Meta: |
| 36 | proxy = True |
| 37 | |
| 38 | def __init__(self, *args, **kwargs): |
| 39 | vpgwuservice = VPGWUService.get_service_objects().all() |
| 40 | # When the tenant is created the default service in the form is set |
| 41 | # to be the first created HelloWorldServiceComplete |
| 42 | if vpgwuservice: |
| 43 | self._meta.get_field( |
| 44 | "provider_service").default = vpgwuservice[0].id |
| 45 | super(VPGWUTenant, self).__init__(*args, **kwargs) |
| 46 | |
Omar Abdelkader | 5612175 | 2017-09-06 19:40:17 -0500 | [diff] [blame] | 47 | @property |
| 48 | def image(self): |
| 49 | if not self.vpgwu_vendor: |
| 50 | return super(VPGWUTenant, self).image |
| 51 | return self.vpgwu_vendor.image |
| 52 | |
| 53 | def save_instance(self, instance): |
| 54 | if self.vpgwu_vendor: |
| 55 | instance.flavor = self.vpgwu_vendor.flavor |
| 56 | super(VPGWUTenant, self).save_instance(instance) |
| 57 | |
Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame] | 58 | def save(self, *args, **kwargs): |
Omar Abdelkader | cbcd943 | 2017-08-30 10:45:07 -0600 | [diff] [blame] | 59 | if not self.creator: |
| 60 | if not getattr(self, "caller", None): |
| 61 | raise XOSProgrammingError("VPGWUTenant's self.caller was not set") |
| 62 | self.creator = self.caller |
| 63 | if not self.creator: |
| 64 | raise XOSProgrammingError("VPGWUTenant's self.creator was not set") |
| 65 | |
Yunpeng Zhang | 0048cf4 | 2017-08-15 23:01:28 -0400 | [diff] [blame] | 66 | # Update the instance that was created for this tenant |
| 67 | super(VPGWUTenant, self).save(*args, **kwargs) |
| 68 | model_policy_vpgwutenant(self.pk) |
| 69 | |
| 70 | def delete(self, *args, **kwargs): |
| 71 | # Delete the instance that was created for this tenant |
| 72 | self.cleanup_container() |
| 73 | super(VPGWUTenant, self).delete(*args, **kwargs) |
| 74 | |
| 75 | def model_policy_vpgwutenant(pk): |
| 76 | # This section of code is atomic to prevent race conditions |
| 77 | with transaction.atomic(): |
| 78 | # We find all of the tenants that are waiting to update |
| 79 | tenant = VPGWUTenant.objects.select_for_update().filter(pk=pk) |
| 80 | if not tenant: |
| 81 | return |
| 82 | # Since this code is atomic it is safe to always use the first tenant |
| 83 | tenant = tenant[0] |
| 84 | tenant.manage_container() |