Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 1 | from django.db import models |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 2 | from core.models import PlCoreBase,SingletonModel,PlCoreBaseManager |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 3 | from core.models.plcorebase import StrippedCharField |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 4 | from xos.exceptions import * |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 5 | from operator import attrgetter |
Scott Baker | 7211f5b | 2015-04-14 17:18:51 -0700 | [diff] [blame] | 6 | import json |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 7 | |
Scott Baker | c24f86d | 2015-08-14 09:10:11 -0700 | [diff] [blame] | 8 | COARSE_KIND="coarse" |
| 9 | |
Scott Baker | 9d1c6d9 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 10 | class AttributeMixin(object): |
| 11 | # helper for extracting things from a json-encoded service_specific_attribute |
| 12 | def get_attribute(self, name, default=None): |
| 13 | if self.service_specific_attribute: |
| 14 | attributes = json.loads(self.service_specific_attribute) |
| 15 | else: |
| 16 | attributes = {} |
| 17 | return attributes.get(name, default) |
| 18 | |
| 19 | def set_attribute(self, name, value): |
| 20 | if self.service_specific_attribute: |
| 21 | attributes = json.loads(self.service_specific_attribute) |
| 22 | else: |
| 23 | attributes = {} |
| 24 | attributes[name]=value |
| 25 | self.service_specific_attribute = json.dumps(attributes) |
| 26 | |
| 27 | def get_initial_attribute(self, name, default=None): |
| 28 | if self._initial["service_specific_attribute"]: |
| 29 | attributes = json.loads(self._initial["service_specific_attribute"]) |
| 30 | else: |
| 31 | attributes = {} |
| 32 | return attributes.get(name, default) |
| 33 | |
Scott Baker | eb098e6 | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 34 | @classmethod |
| 35 | def setup_simple_attributes(cls): |
| 36 | for (attrname, default) in cls.simple_attributes: |
Scott Baker | e2879d3 | 2015-07-13 14:27:51 -0700 | [diff] [blame] | 37 | setattr(cls, attrname, property(lambda self, attrname=attrname, default=default: self.get_attribute(attrname, default), |
| 38 | lambda self, value, attrname=attrname: self.set_attribute(attrname, value), |
| 39 | None, |
| 40 | attrname)) |
Scott Baker | eb098e6 | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 41 | |
Scott Baker | 9d1c6d9 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 42 | class Service(PlCoreBase, AttributeMixin): |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 43 | # when subclassing a service, redefine KIND to describe the new service |
| 44 | KIND = "generic" |
| 45 | |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 46 | description = models.TextField(max_length=254,null=True, blank=True,help_text="Description of Service") |
| 47 | enabled = models.BooleanField(default=True) |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 48 | kind = StrippedCharField(max_length=30, help_text="Kind of service", default=KIND) |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 49 | name = StrippedCharField(max_length=30, help_text="Service Name") |
| 50 | versionNumber = StrippedCharField(max_length=30, help_text="Version of Service Definition") |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 51 | published = models.BooleanField(default=True) |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 52 | view_url = StrippedCharField(blank=True, null=True, max_length=1024) |
| 53 | icon_url = StrippedCharField(blank=True, null=True, max_length=1024) |
Scott Baker | 5b04461 | 2015-04-30 14:30:56 -0700 | [diff] [blame] | 54 | public_key = models.TextField(null=True, blank=True, max_length=1024, help_text="Public key string") |
Scott Baker | dc63fb3 | 2015-11-12 16:22:52 -0800 | [diff] [blame] | 55 | private_key_fn = StrippedCharField(blank=True, null=True, max_length=1024) |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 56 | |
Scott Baker | b9040e9 | 2015-07-13 12:33:28 -0700 | [diff] [blame] | 57 | # Service_specific_attribute and service_specific_id are opaque to XOS |
| 58 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
| 59 | service_specific_attribute = models.TextField(blank=True, null=True) |
| 60 | |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 61 | def __init__(self, *args, **kwargs): |
| 62 | # for subclasses, set the default kind appropriately |
| 63 | self._meta.get_field("kind").default = self.KIND |
| 64 | super(Service, self).__init__(*args, **kwargs) |
| 65 | |
| 66 | @classmethod |
| 67 | def get_service_objects(cls): |
| 68 | return cls.objects.filter(kind = cls.KIND) |
| 69 | |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 70 | @classmethod |
Scott Baker | cd32ad0 | 2015-10-19 21:18:53 -0700 | [diff] [blame] | 71 | def get_deleted_service_objects(cls): |
| 72 | return cls.deleted_objects.filter(kind = cls.KIND) |
| 73 | |
| 74 | @classmethod |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 75 | def get_service_objects_by_user(cls, user): |
| 76 | return cls.select_by_user(user).filter(kind = cls.KIND) |
| 77 | |
| 78 | @classmethod |
| 79 | def select_by_user(cls, user): |
| 80 | if user.is_admin: |
| 81 | return cls.objects.all() |
| 82 | else: |
| 83 | service_ids = [sp.slice.id for sp in ServicePrivilege.objects.filter(user=user)] |
| 84 | return cls.objects.filter(id__in=service_ids) |
| 85 | |
Scott Baker | bcea8cf | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 86 | @property |
| 87 | def serviceattribute_dict(self): |
| 88 | attrs = {} |
| 89 | for attr in self.serviceattributes.all(): |
| 90 | attrs[attr.name] = attr.value |
| 91 | return attrs |
| 92 | |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 93 | def __unicode__(self): return u'%s' % (self.name) |
| 94 | |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 95 | def can_update(self, user): |
| 96 | return user.can_update_service(self, allow=['admin']) |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 97 | |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 98 | def get_scalable_nodes(self, slice, max_per_node=None, exclusive_slices=[]): |
| 99 | """ |
| 100 | Get a list of nodes that can be used to scale up a slice. |
| 101 | |
| 102 | slice - slice to scale up |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 103 | max_per_node - maximum numbers of instances that 'slice' can have on a single node |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 104 | exclusive_slices - list of slices that must have no nodes in common with 'slice'. |
| 105 | """ |
| 106 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 107 | from core.models import Node, Instance # late import to get around order-of-imports constraint in __init__.py |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 108 | |
| 109 | nodes = list(Node.objects.all()) |
| 110 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 111 | conflicting_instances = Instance.objects.filter(slice__in = exclusive_slices) |
| 112 | conflicting_nodes = Node.objects.filter(instances__in = conflicting_instances) |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 113 | |
| 114 | nodes = [x for x in nodes if x not in conflicting_nodes] |
| 115 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 116 | # If max_per_node is set, then limit the number of instances this slice |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 117 | # can have on a single node. |
| 118 | if max_per_node: |
| 119 | acceptable_nodes = [] |
| 120 | for node in nodes: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 121 | existing_count = node.instances.filter(slice=slice).count() |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 122 | if existing_count < max_per_node: |
| 123 | acceptable_nodes.append(node) |
| 124 | nodes = acceptable_nodes |
| 125 | |
| 126 | return nodes |
| 127 | |
| 128 | def pick_node(self, slice, max_per_node=None, exclusive_slices=[]): |
| 129 | # Pick the best node to scale up a slice. |
| 130 | |
| 131 | nodes = self.get_scalable_nodes(slice, max_per_node, exclusive_slices) |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 132 | nodes = sorted(nodes, key=lambda node: node.instances.all().count()) |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 133 | if not nodes: |
| 134 | return None |
| 135 | return nodes[0] |
| 136 | |
| 137 | def adjust_scale(self, slice_hint, scale, max_per_node=None, exclusive_slices=[]): |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 138 | from core.models import Instance # late import to get around order-of-imports constraint in __init__.py |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 139 | |
| 140 | slices = [x for x in self.slices.all() if slice_hint in x.name] |
| 141 | for slice in slices: |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 142 | while slice.instances.all().count() > scale: |
| 143 | s = slice.instances.all()[0] |
| 144 | # print "drop instance", s |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 145 | s.delete() |
| 146 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 147 | while slice.instances.all().count() < scale: |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 148 | node = self.pick_node(slice, max_per_node, exclusive_slices) |
| 149 | if not node: |
| 150 | # no more available nodes |
| 151 | break |
| 152 | |
| 153 | image = slice.default_image |
| 154 | if not image: |
| 155 | raise XOSConfigurationError("No default_image for slice %s" % slice.name) |
| 156 | |
| 157 | flavor = slice.default_flavor |
| 158 | if not flavor: |
| 159 | raise XOSConfigurationError("No default_flavor for slice %s" % slice.name) |
| 160 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 161 | s = Instance(slice=slice, |
Scott Baker | 2575722 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 162 | node=node, |
| 163 | creator=slice.creator, |
| 164 | image=image, |
| 165 | flavor=flavor, |
| 166 | deployment=node.site_deployment.deployment) |
| 167 | s.save() |
| 168 | |
Tony Mack | d851547 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 169 | # print "add instance", s |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 170 | |
Scott Baker | cbf4c78 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 171 | def get_vtn_src_nets(self): |
| 172 | nets=[] |
| 173 | for slice in self.slices.all(): |
| 174 | for ns in slice.networkslices.all(): |
| 175 | if not ns.network: |
| 176 | continue |
Scott Baker | eb3bad9 | 2016-01-12 19:59:12 -0800 | [diff] [blame] | 177 | # if ns.network.template.access in ["direct", "indirect"]: |
| 178 | # # skip access networks; we want to use the private network |
| 179 | # continue |
Scott Baker | cbf4c78 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 180 | if ns.network.name in ["wan_network", "lan_network"]: |
| 181 | # we don't want to attach to the vCPE's lan or wan network |
| 182 | # we only want to attach to its private network |
| 183 | # TODO: fix hard-coding of network name |
| 184 | continue |
| 185 | for cn in ns.network.controllernetworks.all(): |
| 186 | if cn.net_id: |
| 187 | net = {"name": ns.network.name, "net_id": cn.net_id} |
| 188 | nets.append(net) |
| 189 | return nets |
| 190 | |
Scott Baker | 1b7d98b | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 191 | def get_vtn_nets(self): |
| 192 | nets=[] |
| 193 | for slice in self.slices.all(): |
| 194 | for ns in slice.networkslices.all(): |
| 195 | if not ns.network: |
| 196 | continue |
Scott Baker | cbf4c78 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 197 | if ns.network.template.access not in ["direct", "indirect"]: |
| 198 | # skip anything that's not an access network |
| 199 | continue |
Scott Baker | 1b7d98b | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 200 | for cn in ns.network.controllernetworks.all(): |
| 201 | if cn.net_id: |
| 202 | net = {"name": ns.network.name, "net_id": cn.net_id} |
| 203 | nets.append(net) |
| 204 | return nets |
| 205 | |
| 206 | def get_vtn_dependencies_nets(self): |
| 207 | provider_nets = [] |
Scott Baker | 012c54b | 2015-12-08 19:27:50 -0800 | [diff] [blame] | 208 | for tenant in self.subscribed_tenants.all(): |
| 209 | if tenant.provider_service: |
Scott Baker | 1b7d98b | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 210 | for net in tenant.provider_service.get_vtn_nets(): |
| 211 | if not net in provider_nets: |
| 212 | provider_nets.append(net) |
| 213 | return provider_nets |
| 214 | |
| 215 | def get_vtn_dependencies_ids(self): |
| 216 | return [x["net_id"] for x in self.get_vtn_dependencies_nets()] |
| 217 | |
| 218 | def get_vtn_dependencies_names(self): |
| 219 | return [x["name"]+"_"+x["net_id"] for x in self.get_vtn_dependencies_nets()] |
| 220 | |
Scott Baker | cbf4c78 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 221 | def get_vtn_src_ids(self): |
| 222 | return [x["net_id"] for x in self.get_vtn_src_nets()] |
Scott Baker | 1b7d98b | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 223 | |
Scott Baker | cbf4c78 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 224 | def get_vtn_src_names(self): |
| 225 | return [x["name"]+"_"+x["net_id"] for x in self.get_vtn_src_nets()] |
Scott Baker | 012c54b | 2015-12-08 19:27:50 -0800 | [diff] [blame] | 226 | |
| 227 | |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 228 | class ServiceAttribute(PlCoreBase): |
Scott Baker | bcea8cf | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 229 | name = models.CharField(help_text="Attribute Name", max_length=128) |
Tony Mack | 50e1221 | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 230 | value = StrippedCharField(help_text="Attribute Value", max_length=1024) |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 231 | service = models.ForeignKey(Service, related_name='serviceattributes', help_text="The Service this attribute is associated with") |
| 232 | |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 233 | class ServiceRole(PlCoreBase): |
| 234 | ROLE_CHOICES = (('admin','Admin'),) |
| 235 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 236 | |
| 237 | def __unicode__(self): return u'%s' % (self.role) |
| 238 | |
| 239 | class ServicePrivilege(PlCoreBase): |
| 240 | user = models.ForeignKey('User', related_name='serviceprivileges') |
| 241 | service = models.ForeignKey('Service', related_name='serviceprivileges') |
| 242 | role = models.ForeignKey('ServiceRole',related_name='serviceprivileges') |
| 243 | |
| 244 | class Meta: |
Tony Mack | 02683de | 2015-05-13 12:21:28 -0400 | [diff] [blame] | 245 | unique_together = ('user', 'service', 'role') |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 246 | |
| 247 | def __unicode__(self): return u'%s %s %s' % (self.service, self.user, self.role) |
| 248 | |
| 249 | def can_update(self, user): |
| 250 | if not self.service.enabled: |
| 251 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
| 252 | return self.service.can_update(user) |
| 253 | |
| 254 | def save(self, *args, **kwds): |
| 255 | if not self.service.enabled: |
| 256 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
| 257 | super(ServicePrivilege, self).save(*args, **kwds) |
| 258 | |
| 259 | def delete(self, *args, **kwds): |
| 260 | if not self.service.enabled: |
| 261 | raise PermissionDenied, "Cannot modify permission(s) of a disabled service" |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 262 | super(ServicePrivilege, self).delete(*args, **kwds) |
| 263 | |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 264 | @classmethod |
| 265 | def select_by_user(cls, user): |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 266 | if user.is_admin: |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 267 | qs = cls.objects.all() |
Tony Mack | 950b449 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 268 | else: |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 269 | qs = cls.objects.filter(user=user) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 270 | return qs |
| 271 | |
Scott Baker | 9d1c6d9 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 272 | class TenantRoot(PlCoreBase, AttributeMixin): |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 273 | """ A tenantRoot is one of the things that can sit at the root of a chain |
| 274 | of tenancy. This object represents a node. |
| 275 | """ |
| 276 | |
| 277 | KIND= "generic" |
| 278 | kind = StrippedCharField(max_length=30, default=KIND) |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 279 | name = StrippedCharField(max_length=255, help_text="name", blank=True, null=True) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 280 | |
Scott Baker | 29415a8 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 281 | service_specific_attribute = models.TextField(blank=True, null=True) |
| 282 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 283 | |
Scott Baker | 126ad47 | 2015-07-07 17:59:44 -0700 | [diff] [blame] | 284 | def __init__(self, *args, **kwargs): |
| 285 | # for subclasses, set the default kind appropriately |
| 286 | self._meta.get_field("kind").default = self.KIND |
| 287 | super(TenantRoot, self).__init__(*args, **kwargs) |
| 288 | |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 289 | def __unicode__(self): |
| 290 | if not self.name: |
| 291 | return u"%s-tenant_root-#%s" % (str(self.kind), str(self.id)) |
| 292 | else: |
| 293 | return self.name |
| 294 | |
| 295 | def can_update(self, user): |
| 296 | return user.can_update_tenant_root(self, allow=['admin']) |
| 297 | |
Scott Baker | 29415a8 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 298 | def get_subscribed_tenants(self, tenant_class): |
| 299 | ids = self.subscribed_tenants.filter(kind=tenant_class.KIND) |
| 300 | return tenant_class.objects.filter(id__in = ids) |
| 301 | |
| 302 | def get_newest_subscribed_tenant(self, kind): |
| 303 | st = list(self.get_subscribed_tenants(kind)) |
| 304 | if not st: |
| 305 | return None |
| 306 | return sorted(st, key=attrgetter('id'))[0] |
| 307 | |
| 308 | @classmethod |
| 309 | def get_tenant_objects(cls): |
| 310 | return cls.objects.filter(kind = cls.KIND) |
| 311 | |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 312 | @classmethod |
| 313 | def get_tenant_objects_by_user(cls, user): |
| 314 | return cls.select_by_user(user).filter(kind = cls.KIND) |
| 315 | |
| 316 | @classmethod |
| 317 | def select_by_user(cls, user): |
| 318 | if user.is_admin: |
| 319 | return cls.objects.all() |
| 320 | else: |
| 321 | tr_ids = [trp.tenant_root.id for trp in TenantRootPrivilege.objects.filter(user=user)] |
| 322 | return cls.objects.filter(id__in=tr_ids) |
| 323 | |
Scott Baker | 9d1c6d9 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 324 | class Tenant(PlCoreBase, AttributeMixin): |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 325 | """ A tenant is a relationship between two entities, a subscriber and a |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 326 | provider. This object represents an edge. |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 327 | |
| 328 | The subscriber can be a User, a Service, or a Tenant. |
| 329 | |
| 330 | The provider is always a Service. |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 331 | |
| 332 | TODO: rename "Tenant" to "Tenancy" |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 333 | """ |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 334 | |
Scott Baker | ef58a84 | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 335 | CONNECTIVITY_CHOICES = (('public', 'Public'), ('private', 'Private'), ('na', 'Not Applicable')) |
| 336 | |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 337 | # when subclassing a service, redefine KIND to describe the new service |
| 338 | KIND = "generic" |
| 339 | |
| 340 | kind = StrippedCharField(max_length=30, default=KIND) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 341 | provider_service = models.ForeignKey(Service, related_name='provided_tenants') |
| 342 | |
| 343 | # The next four things are the various type of objects that can be subscribers of this Tenancy |
| 344 | # relationship. One and only one can be used at a time. |
| 345 | subscriber_service = models.ForeignKey(Service, related_name='subscribed_tenants', blank=True, null=True) |
| 346 | subscriber_tenant = models.ForeignKey("Tenant", related_name='subscribed_tenants', blank=True, null=True) |
| 347 | subscriber_user = models.ForeignKey("User", related_name='subscribed_tenants', blank=True, null=True) |
| 348 | subscriber_root = models.ForeignKey("TenantRoot", related_name="subscribed_tenants", blank=True, null=True) |
| 349 | |
| 350 | # Service_specific_attribute and service_specific_id are opaque to XOS |
Scott Baker | 1b7c6f1 | 2015-05-06 19:49:31 -0700 | [diff] [blame] | 351 | service_specific_id = StrippedCharField(max_length=30, blank=True, null=True) |
| 352 | service_specific_attribute = models.TextField(blank=True, null=True) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 353 | |
| 354 | # Connect_method is only used by Coarse tenants |
Scott Baker | ef58a84 | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 355 | connect_method = models.CharField(null=False, blank=False, max_length=30, choices=CONNECTIVITY_CHOICES, default="na") |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 356 | |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 357 | def __init__(self, *args, **kwargs): |
| 358 | # for subclasses, set the default kind appropriately |
| 359 | self._meta.get_field("kind").default = self.KIND |
| 360 | super(Tenant, self).__init__(*args, **kwargs) |
| 361 | |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 362 | def __unicode__(self): |
Scott Baker | fe91f62 | 2015-05-20 20:42:04 -0700 | [diff] [blame] | 363 | return u"%s-tenant-%s" % (str(self.kind), str(self.id)) |
Scott Baker | 91e8588 | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 364 | |
Scott Baker | 0d30672 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 365 | @classmethod |
| 366 | def get_tenant_objects(cls): |
| 367 | return cls.objects.filter(kind = cls.KIND) |
| 368 | |
Scott Baker | eb50ee3 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 369 | @classmethod |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 370 | def get_tenant_objects_by_user(cls, user): |
| 371 | return cls.select_by_user(user).filter(kind = cls.KIND) |
| 372 | |
| 373 | @classmethod |
Scott Baker | eb50ee3 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 374 | def get_deleted_tenant_objects(cls): |
| 375 | return cls.deleted_objects.filter(kind = cls.KIND) |
| 376 | |
Scott Baker | bcea8cf | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 377 | @property |
| 378 | def tenantattribute_dict(self): |
| 379 | attrs = {} |
| 380 | for attr in self.tenantattributes.all(): |
| 381 | attrs[attr.name] = attr.value |
| 382 | return attrs |
| 383 | |
Scott Baker | 7f8ef8f | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 384 | # helper function to be used in subclasses that want to ensure service_specific_id is unique |
| 385 | def validate_unique_service_specific_id(self): |
| 386 | if self.pk is None: |
| 387 | if self.service_specific_id is None: |
| 388 | raise XOSMissingField("subscriber_specific_id is None, and it's a required field", fields={"service_specific_id": "cannot be none"}) |
| 389 | |
| 390 | conflicts = self.get_tenant_objects().filter(service_specific_id=self.service_specific_id) |
| 391 | if conflicts: |
| 392 | raise XOSDuplicateKey("service_specific_id %s already exists" % self.service_specific_id, fields={"service_specific_id": "duplicate key"}) |
| 393 | |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 394 | def save(self, *args, **kwargs): |
| 395 | subCount = sum( [1 for e in [self.subscriber_service, self.subscriber_tenant, self.subscriber_user, self.subscriber_root] if e is not None]) |
| 396 | if (subCount > 1): |
| 397 | raise XOSConflictingField("Only one of subscriber_service, subscriber_tenant, subscriber_user, subscriber_root should be set") |
| 398 | |
| 399 | super(Tenant, self).save(*args, **kwargs) |
| 400 | |
| 401 | def get_subscribed_tenants(self, tenant_class): |
| 402 | ids = self.subscribed_tenants.filter(kind=tenant_class.KIND) |
| 403 | return tenant_class.objects.filter(id__in = ids) |
| 404 | |
| 405 | def get_newest_subscribed_tenant(self, kind): |
| 406 | st = list(self.get_subscribed_tenants(kind)) |
| 407 | if not st: |
| 408 | return None |
| 409 | return sorted(st, key=attrgetter('id'))[0] |
| 410 | |
Scott Baker | c8914bf | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 411 | class Scheduler(object): |
| 412 | # XOS Scheduler Abstract Base Class |
| 413 | # Used to implement schedulers that pick which node to put instances on |
| 414 | |
| 415 | def __init__(self, slice): |
| 416 | self.slice = slice |
| 417 | |
| 418 | def pick(self): |
| 419 | # this method should return a tuple (node, parent) |
| 420 | # node is the node to instantiate on |
| 421 | # parent is for container_vm instances only, and is the VM that will |
| 422 | # hold the container |
| 423 | |
| 424 | raise Exception("Abstract Base") |
| 425 | |
| 426 | class LeastLoadedNodeScheduler(Scheduler): |
| 427 | # This scheduler always return the node with the fewest number of instances. |
| 428 | |
| 429 | def __init__(self, slice): |
| 430 | super(LeastLoadedNodeScheduler, self).__init__(slice) |
| 431 | |
| 432 | def pick(self): |
| 433 | from core.models import Node |
| 434 | nodes = list(Node.objects.all()) |
Scott Baker | bcea8cf | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 435 | |
Scott Baker | c8914bf | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 436 | # TODO: logic to filter nodes by which nodes are up, and which |
| 437 | # nodes the slice can instantiate on. |
| 438 | nodes = sorted(nodes, key=lambda node: node.instances.all().count()) |
| 439 | return [nodes[0], None] |
| 440 | |
| 441 | class ContainerVmScheduler(Scheduler): |
| 442 | # This scheduler picks a VM in the slice with the fewest containers inside |
| 443 | # of it. If no VMs are suitable, then it creates a VM. |
| 444 | |
| 445 | # this is a hack and should be replaced by something smarter... |
| 446 | LOOK_FOR_IMAGES=["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image |
| 447 | "Ubuntu 14.04 LTS", # portal |
| 448 | "Ubuntu-14.04-LTS", # ONOS demo machine |
| 449 | "trusty-server-multi-nic", # CloudLab |
| 450 | ] |
| 451 | |
| 452 | MAX_VM_PER_CONTAINER = 10 |
| 453 | |
| 454 | def __init__(self, slice): |
| 455 | super(ContainerVmScheduler, self).__init__(slice) |
| 456 | |
| 457 | @property |
| 458 | def image(self): |
| 459 | from core.models import Image |
| 460 | |
| 461 | look_for_images = self.LOOK_FOR_IMAGES |
| 462 | for image_name in look_for_images: |
| 463 | images = Image.objects.filter(name = image_name) |
| 464 | if images: |
| 465 | return images[0] |
| 466 | |
| 467 | raise XOSProgrammingError("No ContainerVM image (looked for %s)" % str(look_for_images)) |
| 468 | |
| 469 | def make_new_instance(self): |
| 470 | from core.models import Instance, Flavor |
| 471 | |
| 472 | flavors = Flavor.objects.filter(name="m1.small") |
| 473 | if not flavors: |
| 474 | raise XOSConfigurationError("No m1.small flavor") |
| 475 | |
| 476 | (node,parent) = LeastLoadedNodeScheduler(self.slice).pick() |
| 477 | |
| 478 | instance = Instance(slice = self.slice, |
| 479 | node = node, |
| 480 | image = self.image, |
| 481 | creator = self.slice.creator, |
| 482 | deployment = node.site_deployment.deployment, |
| 483 | flavor = flavors[0], |
| 484 | isolation = "vm", |
| 485 | parent = parent) |
| 486 | instance.save() |
| 487 | # We rely on a special naming convention to identify the VMs that will |
| 488 | # hole containers. |
| 489 | instance.name = "%s-outer-%s" % (instance.slice.name, instance.id) |
| 490 | instance.save() |
| 491 | return instance |
| 492 | |
| 493 | def pick(self): |
| 494 | from core.models import Instance, Flavor |
| 495 | |
| 496 | for vm in self.slice.instances.filter(isolation="vm"): |
| 497 | avail_vms = [] |
| 498 | if (vm.name.startswith("%s-outer-" % self.slice.name)): |
| 499 | container_count = Instance.objects.filter(parent=vm).count() |
| 500 | if (container_count < self.MAX_VM_PER_CONTAINER): |
| 501 | avail_vms.append( (vm, container_count) ) |
| 502 | # sort by least containers-per-vm |
| 503 | avail_vms = sorted(avail_vms, key = lambda x: x[1]) |
| 504 | print "XXX", avail_vms |
| 505 | if avail_vms: |
| 506 | instance = avail_vms[0][0] |
| 507 | return (instance.node, instance) |
| 508 | |
| 509 | instance = self.make_new_instance() |
| 510 | return (instance.node, instance) |
| 511 | |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 512 | class TenantWithContainer(Tenant): |
| 513 | """ A tenant that manages a container """ |
| 514 | |
| 515 | # this is a hack and should be replaced by something smarter... |
Scott Baker | f05c497 | 2015-09-09 16:43:39 -0700 | [diff] [blame] | 516 | LOOK_FOR_IMAGES=["ubuntu-vcpe4", # ONOS demo machine -- preferred vcpe image |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 517 | "Ubuntu 14.04 LTS", # portal |
| 518 | "Ubuntu-14.04-LTS", # ONOS demo machine |
Scott Baker | f05c497 | 2015-09-09 16:43:39 -0700 | [diff] [blame] | 519 | "trusty-server-multi-nic", # CloudLab |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 520 | ] |
| 521 | |
Scott Baker | 5e505a5 | 2015-12-14 10:21:53 -0800 | [diff] [blame] | 522 | LOOK_FOR_CONTAINER_IMAGES=["docker-vcpe"] |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 523 | |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 524 | class Meta: |
| 525 | proxy = True |
| 526 | |
| 527 | def __init__(self, *args, **kwargs): |
| 528 | super(TenantWithContainer, self).__init__(*args, **kwargs) |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 529 | self.cached_instance=None |
| 530 | self.orig_instance_id = self.get_initial_attribute("instance_id") |
Scott Baker | 5c125e4 | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 531 | |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 532 | @property |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 533 | def instance(self): |
| 534 | from core.models import Instance |
| 535 | if getattr(self, "cached_instance", None): |
| 536 | return self.cached_instance |
| 537 | instance_id=self.get_attribute("instance_id") |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 538 | if not instance_id: |
| 539 | return None |
| 540 | instances=Instance.objects.filter(id=instance_id) |
| 541 | if not instances: |
| 542 | return None |
| 543 | instance=instances[0] |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 544 | instance.caller = self.creator |
| 545 | self.cached_instance = instance |
| 546 | return instance |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 547 | |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 548 | @instance.setter |
| 549 | def instance(self, value): |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 550 | if value: |
| 551 | value = value.id |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 552 | if (value != self.get_attribute("instance_id", None)): |
| 553 | self.cached_instance=None |
| 554 | self.set_attribute("instance_id", value) |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 555 | |
Scott Baker | 5c125e4 | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 556 | @property |
Scott Baker | 268e2aa | 2016-02-10 12:23:53 -0800 | [diff] [blame] | 557 | def external_hostname(self): |
| 558 | return self.get_attribute("external_hostname", "") |
| 559 | |
| 560 | @external_hostname.setter |
| 561 | def external_hostname(self, value): |
| 562 | self.set_attribute("external_hostname", value) |
| 563 | |
| 564 | @property |
| 565 | def external_container(self): |
| 566 | return self.get_attribute("external_container", "") |
| 567 | |
| 568 | @external_container.setter |
| 569 | def external_container(self, value): |
| 570 | self.set_attribute("external_container", value) |
| 571 | |
| 572 | @property |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 573 | def creator(self): |
| 574 | from core.models import User |
| 575 | if getattr(self, "cached_creator", None): |
| 576 | return self.cached_creator |
| 577 | creator_id=self.get_attribute("creator_id") |
| 578 | if not creator_id: |
| 579 | return None |
| 580 | users=User.objects.filter(id=creator_id) |
| 581 | if not users: |
| 582 | return None |
| 583 | user=users[0] |
| 584 | self.cached_creator = users[0] |
| 585 | return user |
| 586 | |
| 587 | @creator.setter |
| 588 | def creator(self, value): |
| 589 | if value: |
| 590 | value = value.id |
| 591 | if (value != self.get_attribute("creator_id", None)): |
| 592 | self.cached_creator=None |
| 593 | self.set_attribute("creator_id", value) |
| 594 | |
| 595 | @property |
| 596 | def image(self): |
| 597 | from core.models import Image |
| 598 | # Implement the logic here to pick the image that should be used when |
| 599 | # instantiating the VM that will hold the container. |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 600 | |
| 601 | slice = self.provider_service.slices.all() |
| 602 | if not slice: |
| 603 | raise XOSProgrammingError("provider service has no slice") |
| 604 | slice = slice[0] |
| 605 | |
| 606 | if slice.default_isolation in ["container", "container_vm"]: |
| 607 | look_for_images = self.LOOK_FOR_CONTAINER_IMAGES |
| 608 | else: |
| 609 | look_for_images = self.LOOK_FOR_IMAGES |
| 610 | |
| 611 | for image_name in look_for_images: |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 612 | images = Image.objects.filter(name = image_name) |
| 613 | if images: |
| 614 | return images[0] |
| 615 | |
Scott Baker | c8914bf | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 616 | raise XOSProgrammingError("No VPCE image (looked for %s)" % str(look_for_images)) |
Scott Baker | 5c125e4 | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 617 | |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 618 | def save_instance(self, instance): |
| 619 | # Override this function to do custom pre-save or post-save processing, |
| 620 | # such as creating ports for containers. |
| 621 | instance.save() |
Scott Baker | 5c125e4 | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 622 | |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 623 | def pick_least_loaded_instance_in_slice(self, slices): |
| 624 | for slice in slices: |
| 625 | if slice.instances.all().count() > 0: |
| 626 | for instance in slice.instances.all(): |
| 627 | #Pick the first instance that has lesser than 5 tenants |
| 628 | if self.count_of_tenants_of_an_instance(instance) < 5: |
| 629 | return instance |
| 630 | return None |
| 631 | |
| 632 | #TODO: Ideally the tenant count for an instance should be maintained using a |
| 633 | #many-to-one relationship attribute, however this model being proxy, it does |
| 634 | #not permit any new attributes to be defined. Find if any better solutions |
| 635 | def count_of_tenants_of_an_instance(self, instance): |
| 636 | tenant_count = 0 |
| 637 | for tenant in self.get_tenant_objects().all(): |
| 638 | if tenant.get_attribute("instance_id", None) == instance.id: |
| 639 | tenant_count += 1 |
| 640 | return tenant_count |
| 641 | |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 642 | def manage_container(self): |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 643 | from core.models import Instance, Flavor |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 644 | |
| 645 | if self.deleted: |
| 646 | return |
| 647 | |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 648 | if (self.instance is not None) and (self.instance.image != self.image): |
| 649 | self.instance.delete() |
| 650 | self.instance = None |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 651 | |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 652 | if self.instance is None: |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 653 | if not self.provider_service.slices.count(): |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 654 | raise XOSConfigurationError("The service has no slices") |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 655 | |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 656 | new_instance_created = False |
| 657 | instance = None |
| 658 | if self.get_attribute("use_same_instance_for_multiple_tenants", default=False): |
| 659 | #Find if any existing instances can be used for this tenant |
| 660 | slices = self.provider_service.slices.all() |
| 661 | instance = self.pick_least_loaded_instance_in_slice(slices) |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 662 | |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 663 | if not instance: |
| 664 | flavors = Flavor.objects.filter(name="m1.small") |
| 665 | if not flavors: |
| 666 | raise XOSConfigurationError("No m1.small flavor") |
| 667 | |
Srikanth Vavilapalli | 3406fbb | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 668 | slice = self.provider_service.slices.all()[0] |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 669 | |
Srikanth Vavilapalli | 3406fbb | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 670 | if slice.default_isolation == "container_vm": |
Scott Baker | c8914bf | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 671 | (node, parent) = ContainerVmScheduler(slice).pick() |
Srikanth Vavilapalli | 3406fbb | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 672 | else: |
Scott Baker | c8914bf | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 673 | (node, parent) = LeastLoadedNodeScheduler(slice).pick() |
Scott Baker | a759fe3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 674 | |
Srikanth Vavilapalli | 3406fbb | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 675 | instance = Instance(slice = slice, |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 676 | node = node, |
| 677 | image = self.image, |
| 678 | creator = self.creator, |
| 679 | deployment = node.site_deployment.deployment, |
Srikanth Vavilapalli | 3406fbb | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 680 | flavor = flavors[0], |
| 681 | isolation = slice.default_isolation, |
| 682 | parent = parent) |
| 683 | self.save_instance(instance) |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 684 | new_instance_created = True |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 685 | |
| 686 | try: |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 687 | self.instance = instance |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 688 | super(TenantWithContainer, self).save() |
| 689 | except: |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 690 | if new_instance_created: |
| 691 | instance.delete() |
Scott Baker | c1584b8 | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 692 | raise |
| 693 | |
| 694 | def cleanup_container(self): |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 695 | if self.instance: |
Srikanth Vavilapalli | 17b5a3c | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 696 | if self.get_attribute("use_same_instance_for_multiple_tenants", default=False): |
| 697 | #Delete the instance only if this is last tenant in that instance |
| 698 | tenant_count = self.count_of_tenants_of_an_instance(self.instance) |
| 699 | if tenant_count == 0: |
| 700 | self.instance.delete() |
| 701 | else: |
| 702 | self.instance.delete() |
Tony Mack | 3201006 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 703 | self.instance = None |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 704 | |
Scott Baker | 88fa673 | 2015-12-10 23:23:07 -0800 | [diff] [blame] | 705 | def save(self, *args, **kwargs): |
| 706 | if (not self.creator) and (hasattr(self, "caller")) and (self.caller): |
| 707 | self.creator = self.caller |
| 708 | super(TenantWithContainer, self).save(*args, **kwargs) |
| 709 | |
Scott Baker | ef58a84 | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 710 | class CoarseTenant(Tenant): |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 711 | """ TODO: rename "CoarseTenant" --> "StaticTenant" """ |
Scott Baker | ef58a84 | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 712 | class Meta: |
| 713 | proxy = True |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 714 | |
Scott Baker | c24f86d | 2015-08-14 09:10:11 -0700 | [diff] [blame] | 715 | KIND = COARSE_KIND |
Scott Baker | ef58a84 | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 716 | |
| 717 | def save(self, *args, **kwargs): |
| 718 | if (not self.subscriber_service): |
| 719 | raise XOSValidationError("subscriber_service cannot be null") |
| 720 | if (self.subscriber_tenant or self.subscriber_user): |
| 721 | raise XOSValidationError("subscriber_tenant and subscriber_user must be null") |
| 722 | |
| 723 | super(CoarseTenant,self).save() |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 724 | |
| 725 | class Subscriber(TenantRoot): |
| 726 | """ Intermediate class for TenantRoots that are to be Subscribers """ |
| 727 | |
| 728 | class Meta: |
| 729 | proxy = True |
| 730 | |
| 731 | KIND = "Subscriber" |
| 732 | |
| 733 | class Provider(TenantRoot): |
| 734 | """ Intermediate class for TenantRoots that are to be Providers """ |
| 735 | |
| 736 | class Meta: |
| 737 | proxy = True |
| 738 | |
| 739 | KIND = "Provider" |
| 740 | |
Scott Baker | 1e7e348 | 2015-10-15 15:59:19 -0700 | [diff] [blame] | 741 | class TenantAttribute(PlCoreBase): |
Scott Baker | 3ab4db8 | 2015-10-20 17:12:36 -0700 | [diff] [blame] | 742 | name = models.CharField(help_text="Attribute Name", max_length=128) |
Scott Baker | 1e7e348 | 2015-10-15 15:59:19 -0700 | [diff] [blame] | 743 | value = models.TextField(help_text="Attribute Value") |
| 744 | tenant = models.ForeignKey(Tenant, related_name='tenantattributes', help_text="The Tenant this attribute is associated with") |
| 745 | |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 746 | class TenantRootRole(PlCoreBase): |
Scott Baker | 1729e34 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 747 | ROLE_CHOICES = (('admin','Admin'), ('access','Access')) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 748 | |
| 749 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 750 | |
| 751 | def __unicode__(self): return u'%s' % (self.role) |
| 752 | |
| 753 | class TenantRootPrivilege(PlCoreBase): |
| 754 | user = models.ForeignKey('User', related_name="tenant_root_privileges") |
| 755 | tenant_root = models.ForeignKey('TenantRoot', related_name="tenant_root_privileges") |
| 756 | role = models.ForeignKey('TenantRootRole', related_name="tenant_root_privileges") |
| 757 | |
| 758 | class Meta: |
| 759 | unique_together = ('user', 'tenant_root', 'role') |
| 760 | |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 761 | def __unicode__(self): return u'%s %s %s' % (self.tenant_root, self.user, self.role) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 762 | |
| 763 | def save(self, *args, **kwds): |
| 764 | if not self.user.is_active: |
| 765 | raise PermissionDenied, "Cannot modify role(s) of a disabled user" |
Scott Baker | c8e947a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 766 | super(TenantRootPrivilege, self).save(*args, **kwds) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 767 | |
| 768 | def can_update(self, user): |
Scott Baker | c8e947a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 769 | return user.can_update_tenant_root_privilege(self) |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 770 | |
Scott Baker | 27de601 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 771 | @classmethod |
| 772 | def select_by_user(cls, user): |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 773 | if user.is_admin: |
Scott Baker | 1729e34 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 774 | return cls.objects.all() |
Scott Baker | a86489f | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 775 | else: |
Scott Baker | 1729e34 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 776 | # User can see his own privilege |
| 777 | trp_ids = [trp.id for trp in cls.objects.filter(user=user)] |
| 778 | |
| 779 | # A slice admin can see the SlicePrivileges for his Slice |
| 780 | for priv in cls.objects.filter(user=user, role__role="admin"): |
| 781 | trp_ids.extend( [trp.id for trp in cls.objects.filter(tenant_root=priv.tenant_root)] ) |
| 782 | |
| 783 | return cls.objects.filter(id__in=trp_ids) |
| 784 | |
Scott Baker | b238562 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 785 | |