Omar Abdelkader | d76c89d | 2017-08-02 19:39:44 -0500 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VPGWCService_decl |
| 3 | from models_decl import VPGWCTenant_decl |
| 4 | |
Omar Abdelkader | 37e8ddd | 2017-08-07 19:47:09 -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, 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 | |
Omar Abdelkader | d76c89d | 2017-08-02 19:39:44 -0500 | [diff] [blame] | 22 | class VPGWCService(VPGWCService_decl): |
| 23 | class Meta: |
| 24 | proxy = True |
| 25 | |
Omar Abdelkader | 6a4842a | 2017-08-15 16:40:48 -0600 | [diff] [blame^] | 26 | def create_tenant(self, **kwargs): |
| 27 | t = VPGWCTenant(kind="vEPC", provider_service=self, connect_method="na", **kwargs) |
| 28 | t.save() |
| 29 | return t |
| 30 | |
Omar Abdelkader | d76c89d | 2017-08-02 19:39:44 -0500 | [diff] [blame] | 31 | class VPGWCTenant(VPGWCTenant_decl): |
| 32 | class Meta: |
| 33 | proxy = True |
| 34 | |
| 35 | def __init__(self, *args, **kwargs): |
| 36 | vpgwcservice = VPGWCService.get_service_objects().all() |
| 37 | # When the tenant is created the default service in the form is set |
| 38 | # to be the first created HelloWorldServiceComplete |
| 39 | if vpgwcservice: |
| 40 | self._meta.get_field( |
| 41 | "provider_service").default = vpgwcservice[0].id |
| 42 | super(VPGWCTenant, self).__init__(*args, **kwargs) |
| 43 | |
| 44 | def save(self, *args, **kwargs): |
Omar Abdelkader | 6a4842a | 2017-08-15 16:40:48 -0600 | [diff] [blame^] | 45 | if not self.creator: |
| 46 | if not getattr(self, "caller", None): |
| 47 | raise XOSProgrammingError("VPGWCTenant's self.caller was not set") |
| 48 | self.creator = self.caller |
| 49 | if not self.creator: |
| 50 | raise XOSProgrammingError("VPGWCTenant's self.creator was not set") |
| 51 | |
Omar Abdelkader | d76c89d | 2017-08-02 19:39:44 -0500 | [diff] [blame] | 52 | # Update the instance that was created for this tenant |
| 53 | super(VPGWCTenant, self).save(*args, **kwargs) |
| 54 | model_policy_vpgwctenant(self.pk) |
| 55 | |
| 56 | def delete(self, *args, **kwargs): |
| 57 | # Delete the instance that was created for this tenant |
| 58 | self.cleanup_container() |
| 59 | super(VPGWCTenant, self).delete(*args, **kwargs) |
| 60 | |
| 61 | def model_policy_vpgwctenant(pk): |
| 62 | # This section of code is atomic to prevent race conditions |
| 63 | with transaction.atomic(): |
| 64 | # We find all of the tenants that are waiting to update |
| 65 | tenant = VPGWCTenant.objects.select_for_update().filter(pk=pk) |
| 66 | if not tenant: |
| 67 | return |
| 68 | # Since this code is atomic it is safe to always use the first tenant |
| 69 | tenant = tenant[0] |
| 70 | tenant.manage_container() |