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