Scott Baker | f57e559 | 2015-04-14 17:18:51 -0700 | [diff] [blame] | 1 | import json |
Jeremy Mowery | 11ed899 | 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 | 11ed899 | 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 | 2461bec | 2015-08-14 09:10:11 -0700 | [diff] [blame] | 11 | |
Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 12 | class AttributeMixin(object): |
Jeremy Mowery | 11ed899 | 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 | 82498c5 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 28 | attributes[name] = value |
Scott Baker | 82498c5 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 33 | attributes = json.loads( |
| 34 | self._initial["service_specific_attribute"]) |
Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 35 | else: |
| 36 | attributes = {} |
| 37 | return attributes.get(name, default) |
| 38 | |
Scott Baker | 74404fe | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 39 | @classmethod |
Scott Baker | 6dfde21 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 42 | if attrname == name: |
Scott Baker | 6dfde21 | 2016-03-01 20:10:24 -0800 | [diff] [blame] | 43 | return default |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 44 | if hasattr(cls, "default_attributes"): |
Scott Baker | f180ff3 | 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 | 6dfde21 | 2016-03-01 20:10:24 -0800 | [diff] [blame] | 49 | |
| 50 | @classmethod |
Scott Baker | 74404fe | 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 | 096dce8 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 54 | lambda self, value, attrname=attrname: self.set_attribute( |
| 55 | attrname, value), |
Scott Baker | 096dce8 | 2015-07-13 14:27:51 -0700 | [diff] [blame] | 56 | None, |
| 57 | attrname)) |
Scott Baker | 74404fe | 2015-07-13 13:54:06 -0700 | [diff] [blame] | 58 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 59 | |
Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 60 | class Service(PlCoreBase, AttributeMixin): |
Scott Baker | 008a996 | 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 | 11ed899 | 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 | 11ed899 | 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 | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 69 | name = StrippedCharField(max_length=30, help_text="Service Name") |
Scott Baker | d3e3029 | 2016-05-02 09:38:24 -0700 | [diff] [blame] | 70 | versionNumber = StrippedCharField(blank=True, null=True, |
Jeremy Mowery | 11ed899 | 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 | d84b1ff | 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 | 11ed899 | 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 | f60c010 | 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 | 2f0828e | 2015-07-13 12:33:28 -0700 | [diff] [blame] | 79 | # Service_specific_attribute and service_specific_id are opaque to XOS |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 80 | service_specific_id = StrippedCharField( |
| 81 | max_length=30, blank=True, null=True) |
Scott Baker | 2f0828e | 2015-07-13 12:33:28 -0700 | [diff] [blame] | 82 | service_specific_attribute = models.TextField(blank=True, null=True) |
| 83 | |
Scott Baker | 008a996 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 91 | return cls.objects.filter(kind=cls.KIND) |
Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 92 | |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 93 | @classmethod |
Scott Baker | 542cd6f | 2015-10-19 21:18:53 -0700 | [diff] [blame] | 94 | def get_deleted_service_objects(cls): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 95 | return cls.deleted_objects.filter(kind=cls.KIND) |
Scott Baker | 542cd6f | 2015-10-19 21:18:53 -0700 | [diff] [blame] | 96 | |
| 97 | @classmethod |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 98 | def get_service_objects_by_user(cls, user): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 99 | return cls.select_by_user(user).filter(kind=cls.KIND) |
Scott Baker | 16573d3 | 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 | 11ed899 | 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 | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 108 | return cls.objects.filter(id__in=service_ids) |
| 109 | |
Scott Baker | cce158d | 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 | 9d2ea09 | 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 | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 121 | |
Scott Baker | 9843673 | 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 | 3de59e3 | 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 | 9843673 | 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 | 11ed899 | 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 | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 133 | |
| 134 | nodes = list(Node.objects.all()) |
| 135 | |
Jeremy Mowery | 11ed899 | 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 | 9843673 | 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 | 3de59e3 | 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 | 9843673 | 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 | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 148 | existing_count = node.instances.filter(slice=slice).count() |
Scott Baker | 9843673 | 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 | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 159 | nodes = sorted(nodes, key=lambda node: node.instances.all().count()) |
Scott Baker | 9843673 | 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 | 11ed899 | 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 | 9843673 | 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 | 3de59e3 | 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 | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 173 | s.delete() |
| 174 | |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 175 | while slice.instances.all().count() < scale: |
Scott Baker | 9843673 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 183 | raise XOSConfigurationError( |
| 184 | "No default_image for slice %s" % slice.name) |
Scott Baker | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 185 | |
| 186 | flavor = slice.default_flavor |
| 187 | if not flavor: |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 188 | raise XOSConfigurationError( |
| 189 | "No default_flavor for slice %s" % slice.name) |
Scott Baker | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 190 | |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 191 | s = Instance(slice=slice, |
Jeremy Mowery | 11ed899 | 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 | 9843673 | 2015-05-11 16:36:41 -0700 | [diff] [blame] | 197 | s.save() |
| 198 | |
Tony Mack | 3de59e3 | 2015-08-19 11:58:18 -0400 | [diff] [blame] | 199 | # print "add instance", s |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 200 | |
Scott Baker | 8a67ae7 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 201 | def get_vtn_src_nets(self): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 202 | nets = [] |
Scott Baker | 8a67ae7 | 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 | f63cf1c | 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 | e6e7855 | 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 | 8a67ae7 | 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 | cbd718e | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 224 | def get_vtn_nets(self): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 225 | nets = [] |
Scott Baker | cbd718e | 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 | 8a67ae7 | 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 | cbd718e | 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 | 5380a44 | 2015-12-08 19:27:50 -0800 | [diff] [blame] | 241 | for tenant in self.subscribed_tenants.all(): |
| 242 | if tenant.provider_service: |
Scott Baker | cbd718e | 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 | 11ed899 | 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 | cbd718e | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 253 | |
Scott Baker | 8a67ae7 | 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 | cbd718e | 2015-12-08 21:31:18 -0800 | [diff] [blame] | 256 | |
Scott Baker | 8a67ae7 | 2015-12-09 22:54:52 -0800 | [diff] [blame] | 257 | def get_vtn_src_names(self): |
Jeremy Mowery | 11ed899 | 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 | 5380a44 | 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 | cce158d | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 262 | name = models.CharField(help_text="Attribute Name", max_length=128) |
Tony Mack | d84b1ff | 2015-03-09 13:03:56 -0400 | [diff] [blame] | 263 | value = StrippedCharField(help_text="Attribute Value", max_length=1024) |
Jeremy Mowery | 11ed899 | 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 | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 268 | class ServiceRole(PlCoreBase): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 269 | ROLE_CHOICES = (('admin', 'Admin'),) |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 270 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 271 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 272 | def __unicode__(self): return u'%s' % (self.role) |
| 273 | |
Tony Mack | 9d2ea09 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 278 | role = models.ForeignKey('ServiceRole', related_name='serviceprivileges') |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 279 | |
| 280 | class Meta: |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 281 | unique_together = ('user', 'service', 'role') |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 282 | |
Jeremy Mowery | 11ed899 | 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 | 9d2ea09 | 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 | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 299 | super(ServicePrivilege, self).delete(*args, **kwds) |
| 300 | |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 301 | @classmethod |
| 302 | def select_by_user(cls, user): |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 303 | if user.is_admin: |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 304 | qs = cls.objects.all() |
Tony Mack | 9d2ea09 | 2015-04-29 12:23:10 -0400 | [diff] [blame] | 305 | else: |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 306 | qs = cls.objects.filter(user=user) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 307 | return qs |
| 308 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 309 | |
Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 310 | class TenantRoot(PlCoreBase, AttributeMixin): |
Scott Baker | 4587b82 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 315 | KIND = "generic" |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 316 | kind = StrippedCharField(max_length=30, default=KIND) |
Jeremy Mowery | 11ed899 | 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 | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 319 | |
Scott Baker | efcec63 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 320 | service_specific_attribute = models.TextField(blank=True, null=True) |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 321 | service_specific_id = StrippedCharField( |
| 322 | max_length=30, blank=True, null=True) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 323 | |
Scott Baker | db66fd3 | 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 | 618a489 | 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 | efcec63 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 340 | return tenant_class.objects.filter(id__in=ids) |
Scott Baker | efcec63 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 350 | return cls.objects.filter(kind=cls.KIND) |
Scott Baker | efcec63 | 2015-07-07 12:12:42 -0700 | [diff] [blame] | 351 | |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 352 | @classmethod |
| 353 | def get_tenant_objects_by_user(cls, user): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 354 | return cls.select_by_user(user).filter(kind=cls.KIND) |
Scott Baker | 16573d3 | 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 | 11ed899 | 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 | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 363 | return cls.objects.filter(id__in=tr_ids) |
| 364 | |
Jeremy Mowery | 11ed899 | 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 | dc880f3 | 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 | 11ed899 | 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 | dc880f3 | 2016-03-31 14:45:31 -0700 | [diff] [blame] | 371 | |
| 372 | if self.service_specific_id: |
Jeremy Mowery | 11ed899 | 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 | dc880f3 | 2016-03-31 14:45:31 -0700 | [diff] [blame] | 375 | if self.pk: |
Scott Baker | e4c9d87 | 2016-04-01 16:28:41 -0700 | [diff] [blame] | 376 | conflicts = conflicts.exclude(pk=self.pk) |
Scott Baker | dc880f3 | 2016-03-31 14:45:31 -0700 | [diff] [blame] | 377 | if conflicts: |
Jeremy Mowery | 11ed899 | 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 | dc880f3 | 2016-03-31 14:45:31 -0700 | [diff] [blame] | 381 | |
Scott Baker | 82498c5 | 2015-07-13 13:07:27 -0700 | [diff] [blame] | 382 | class Tenant(PlCoreBase, AttributeMixin): |
Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 383 | """ A tenant is a relationship between two entities, a subscriber and a |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 384 | provider. This object represents an edge. |
Scott Baker | 8103d0f | 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 | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 389 | |
| 390 | TODO: rename "Tenant" to "Tenancy" |
Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 391 | """ |
Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 392 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 393 | CONNECTIVITY_CHOICES = (('public', 'Public'), |
| 394 | ('private', 'Private'), ('na', 'Not Applicable')) |
Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 395 | |
Scott Baker | 008a996 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 400 | provider_service = models.ForeignKey( |
| 401 | Service, related_name='provided_tenants') |
Scott Baker | 4587b82 | 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 | 97ca910 | 2016-04-13 16:56:39 -0700 | [diff] [blame] | 405 | # XXX these should really be changed to GenericForeignKey |
Jeremy Mowery | 11ed899 | 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 | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 416 | |
| 417 | # Service_specific_attribute and service_specific_id are opaque to XOS |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 418 | service_specific_id = StrippedCharField( |
| 419 | max_length=30, blank=True, null=True) |
Scott Baker | 76934d8 | 2015-05-06 19:49:31 -0700 | [diff] [blame] | 420 | service_specific_attribute = models.TextField(blank=True, null=True) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 421 | |
| 422 | # Connect_method is only used by Coarse tenants |
Jeremy Mowery | 11ed899 | 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 | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 425 | |
Scott Baker | 008a996 | 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 | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 431 | def __unicode__(self): |
Scott Baker | f996b76 | 2015-05-20 20:42:04 -0700 | [diff] [blame] | 432 | return u"%s-tenant-%s" % (str(self.kind), str(self.id)) |
Scott Baker | 8103d0f | 2015-04-10 16:42:26 -0700 | [diff] [blame] | 433 | |
Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 434 | @classmethod |
| 435 | def get_tenant_objects(cls): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 436 | return cls.objects.filter(kind=cls.KIND) |
Scott Baker | 008a996 | 2015-04-15 20:58:20 -0700 | [diff] [blame] | 437 | |
Scott Baker | e7fc9f5 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 438 | @classmethod |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 439 | def get_tenant_objects_by_user(cls, user): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 440 | return cls.select_by_user(user).filter(kind=cls.KIND) |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 441 | |
| 442 | @classmethod |
Scott Baker | e7fc9f5 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 443 | def get_deleted_tenant_objects(cls): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 444 | return cls.deleted_objects.filter(kind=cls.KIND) |
Scott Baker | e7fc9f5 | 2015-05-05 17:52:03 -0700 | [diff] [blame] | 445 | |
Scott Baker | cce158d | 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 | 11ed899 | 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 | d921e1c | 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 | 11ed899 | 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 | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 460 | |
Jeremy Mowery | 11ed899 | 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 | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 463 | if conflicts: |
Jeremy Mowery | 11ed899 | 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 | d921e1c | 2015-04-20 14:24:29 -0700 | [diff] [blame] | 466 | |
Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 467 | def save(self, *args, **kwargs): |
Jeremy Mowery | 11ed899 | 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 | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 470 | if (subCount > 1): |
Jeremy Mowery | 11ed899 | 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 | 618a489 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 478 | return tenant_class.objects.filter(id__in=ids) |
Scott Baker | 618a489 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 486 | |
Scott Baker | 534dd2a | 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 | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 502 | |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 503 | class LeastLoadedNodeScheduler(Scheduler): |
Jeremy Mowery | 11ed899 | 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 | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 506 | |
Scott Baker | 6526f55 | 2016-03-04 11:29:02 -0800 | [diff] [blame] | 507 | def __init__(self, slice, label=None): |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 508 | super(LeastLoadedNodeScheduler, self).__init__(slice) |
Scott Baker | 6526f55 | 2016-03-04 11:29:02 -0800 | [diff] [blame] | 509 | self.label = label |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 510 | |
| 511 | def pick(self): |
| 512 | from core.models import Node |
Pingping Lin | fa30bae | 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 | 1d7ff81 | 2016-04-21 09:02:08 -0700 | [diff] [blame] | 518 | |
Scott Baker | 6526f55 | 2016-03-04 11:29:02 -0800 | [diff] [blame] | 519 | if self.label: |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 520 | nodes = nodes.filter(nodelabels__name=self.label) |
Scott Baker | 6526f55 | 2016-03-04 11:29:02 -0800 | [diff] [blame] | 521 | |
| 522 | nodes = list(nodes) |
| 523 | |
| 524 | if not nodes: |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 525 | raise Exception( |
| 526 | "LeastLoadedNodeScheduler: No suitable nodes to pick from") |
Scott Baker | cce158d | 2015-12-07 22:20:40 -0800 | [diff] [blame] | 527 | |
Scott Baker | 534dd2a | 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 | fa30bae | 2016-03-03 09:52:24 -0800 | [diff] [blame] | 530 | # nodes = sorted(nodes, key=lambda node: node.instances.all().count()) |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 531 | return [nodes[0], None] |
| 532 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 533 | |
Scott Baker | 534dd2a | 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 | 534dd2a | 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 | a8188a7 | 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 | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 550 | |
Scott Baker | a8188a7 | 2016-05-19 17:54:52 -0700 | [diff] [blame] | 551 | raise XOPSProgrammingError("Please set a default image for %s" % self.slice.name) |
Scott Baker | 534dd2a | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 560 | (node, parent) = LeastLoadedNodeScheduler(self.slice).pick() |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 561 | |
Jeremy Mowery | 11ed899 | 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 | 534dd2a | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 585 | avail_vms.append((vm, container_count)) |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 586 | # sort by least containers-per-vm |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 587 | avail_vms = sorted(avail_vms, key=lambda x: x[1]) |
Scott Baker | 534dd2a | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 596 | |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 597 | class TenantWithContainer(Tenant): |
| 598 | """ A tenant that manages a container """ |
| 599 | |
Scott Baker | e458afd | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 605 | self.cached_instance = None |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 606 | self.orig_instance_id = self.get_initial_attribute("instance_id") |
Scott Baker | e274f6b | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 607 | |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 608 | @property |
Tony Mack | 6a782f9 | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 613 | instance_id = self.get_attribute("instance_id") |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 614 | if not instance_id: |
| 615 | return None |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 616 | instances = Instance.objects.filter(id=instance_id) |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 617 | if not instances: |
| 618 | return None |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 619 | instance = instances[0] |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 620 | instance.caller = self.creator |
| 621 | self.cached_instance = instance |
| 622 | return instance |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 623 | |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 624 | @instance.setter |
| 625 | def instance(self, value): |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 626 | if value: |
| 627 | value = value.id |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 628 | if (value != self.get_attribute("instance_id", None)): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 629 | self.cached_instance = None |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 630 | self.set_attribute("instance_id", value) |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 631 | |
Scott Baker | e274f6b | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 632 | @property |
Scott Baker | bdda0be | 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 | e458afd | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 653 | creator_id = self.get_attribute("creator_id") |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 654 | if not creator_id: |
| 655 | return None |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 656 | users = User.objects.filter(id=creator_id) |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 657 | if not users: |
| 658 | return None |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 659 | user = users[0] |
Scott Baker | e458afd | 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 | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 668 | self.cached_creator = None |
Scott Baker | e458afd | 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 | abad4c9 | 2016-05-25 10:02:29 -0700 | [diff] [blame^] | 676 | |
Scott Baker | cfaf0d3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 677 | slice = self.provider_service.slices.all() |
| 678 | if not slice: |
| 679 | raise XOSProgrammingError("provider service has no slice") |
| 680 | slice = slice[0] |
| 681 | |
Scott Baker | abad4c9 | 2016-05-25 10:02:29 -0700 | [diff] [blame^] | 682 | # If slice has default_image set then use it |
| 683 | if slice.default_image: |
| 684 | return slice.default_image |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 685 | |
Scott Baker | a8188a7 | 2016-05-19 17:54:52 -0700 | [diff] [blame] | 686 | raise XOPSProgrammingError("Please set a default image for %s" % self.slice.name) |
Scott Baker | e274f6b | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 687 | |
Scott Baker | cfaf0d3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 688 | def save_instance(self, instance): |
| 689 | # Override this function to do custom pre-save or post-save processing, |
| 690 | # such as creating ports for containers. |
| 691 | instance.save() |
Scott Baker | e274f6b | 2015-11-02 20:54:28 -0800 | [diff] [blame] | 692 | |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 693 | def pick_least_loaded_instance_in_slice(self, slices): |
| 694 | for slice in slices: |
| 695 | if slice.instances.all().count() > 0: |
| 696 | for instance in slice.instances.all(): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 697 | # Pick the first instance that has lesser than 5 tenants |
| 698 | if self.count_of_tenants_of_an_instance(instance) < 5: |
| 699 | return instance |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 700 | return None |
| 701 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 702 | # TODO: Ideally the tenant count for an instance should be maintained using a |
| 703 | # many-to-one relationship attribute, however this model being proxy, it does |
| 704 | # not permit any new attributes to be defined. Find if any better solutions |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 705 | def count_of_tenants_of_an_instance(self, instance): |
| 706 | tenant_count = 0 |
| 707 | for tenant in self.get_tenant_objects().all(): |
| 708 | if tenant.get_attribute("instance_id", None) == instance.id: |
| 709 | tenant_count += 1 |
| 710 | return tenant_count |
| 711 | |
Scott Baker | cfaf0d3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 712 | def manage_container(self): |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 713 | from core.models import Instance, Flavor |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 714 | |
| 715 | if self.deleted: |
| 716 | return |
| 717 | |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 718 | if (self.instance is not None) and (self.instance.image != self.image): |
| 719 | self.instance.delete() |
| 720 | self.instance = None |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 721 | |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 722 | if self.instance is None: |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 723 | if not self.provider_service.slices.count(): |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 724 | raise XOSConfigurationError("The service has no slices") |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 725 | |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 726 | new_instance_created = False |
| 727 | instance = None |
| 728 | if self.get_attribute("use_same_instance_for_multiple_tenants", default=False): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 729 | # Find if any existing instances can be used for this tenant |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 730 | slices = self.provider_service.slices.all() |
| 731 | instance = self.pick_least_loaded_instance_in_slice(slices) |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 732 | |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 733 | if not instance: |
Srikanth Vavilapalli | 71120a9 | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 734 | slice = self.provider_service.slices.all()[0] |
Scott Baker | cfaf0d3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 735 | |
Srikanth Vavilapalli | 2ab2d07 | 2016-02-15 01:18:09 -0500 | [diff] [blame] | 736 | flavor = slice.default_flavor |
| 737 | if not flavor: |
| 738 | flavors = Flavor.objects.filter(name="m1.small") |
| 739 | if not flavors: |
| 740 | raise XOSConfigurationError("No m1.small flavor") |
| 741 | flavor = flavors[0] |
| 742 | |
Srikanth Vavilapalli | 71120a9 | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 743 | if slice.default_isolation == "container_vm": |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 744 | (node, parent) = ContainerVmScheduler(slice).pick() |
Srikanth Vavilapalli | 71120a9 | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 745 | else: |
Scott Baker | 534dd2a | 2015-11-18 20:58:08 -0800 | [diff] [blame] | 746 | (node, parent) = LeastLoadedNodeScheduler(slice).pick() |
Scott Baker | cfaf0d3 | 2015-11-16 22:51:02 -0800 | [diff] [blame] | 747 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 748 | instance = Instance(slice=slice, |
| 749 | node=node, |
| 750 | image=self.image, |
| 751 | creator=self.creator, |
| 752 | deployment=node.site_deployment.deployment, |
| 753 | flavor=flavor, |
| 754 | isolation=slice.default_isolation, |
| 755 | parent=parent) |
Srikanth Vavilapalli | 71120a9 | 2015-11-17 13:41:38 -0600 | [diff] [blame] | 756 | self.save_instance(instance) |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 757 | new_instance_created = True |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 758 | |
| 759 | try: |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 760 | self.instance = instance |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 761 | super(TenantWithContainer, self).save() |
| 762 | except: |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 763 | if new_instance_created: |
| 764 | instance.delete() |
Scott Baker | e458afd | 2015-09-09 16:36:06 -0700 | [diff] [blame] | 765 | raise |
| 766 | |
| 767 | def cleanup_container(self): |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 768 | if self.instance: |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 769 | if self.get_attribute("use_same_instance_for_multiple_tenants", default=False): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 770 | # Delete the instance only if this is last tenant in that |
| 771 | # instance |
| 772 | tenant_count = self.count_of_tenants_of_an_instance( |
| 773 | self.instance) |
Srikanth Vavilapalli | 1d14ff0 | 2015-11-17 12:21:02 -0600 | [diff] [blame] | 774 | if tenant_count == 0: |
| 775 | self.instance.delete() |
| 776 | else: |
| 777 | self.instance.delete() |
Tony Mack | 6a782f9 | 2015-09-13 22:50:39 +0000 | [diff] [blame] | 778 | self.instance = None |
Scott Baker | 618a489 | 2015-07-06 14:27:31 -0700 | [diff] [blame] | 779 | |
Scott Baker | ee83f2a | 2015-12-10 23:23:07 -0800 | [diff] [blame] | 780 | def save(self, *args, **kwargs): |
| 781 | if (not self.creator) and (hasattr(self, "caller")) and (self.caller): |
| 782 | self.creator = self.caller |
| 783 | super(TenantWithContainer, self).save(*args, **kwargs) |
| 784 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 785 | |
Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 786 | class CoarseTenant(Tenant): |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 787 | """ TODO: rename "CoarseTenant" --> "StaticTenant" """ |
Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 788 | class Meta: |
| 789 | proxy = True |
Siobhan Tully | 00353f7 | 2013-10-08 21:53:27 -0400 | [diff] [blame] | 790 | |
Scott Baker | 2461bec | 2015-08-14 09:10:11 -0700 | [diff] [blame] | 791 | KIND = COARSE_KIND |
Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 792 | |
| 793 | def save(self, *args, **kwargs): |
| 794 | if (not self.subscriber_service): |
| 795 | raise XOSValidationError("subscriber_service cannot be null") |
| 796 | if (self.subscriber_tenant or self.subscriber_user): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 797 | raise XOSValidationError( |
| 798 | "subscriber_tenant and subscriber_user must be null") |
Scott Baker | 925a8fa | 2015-04-26 20:30:40 -0700 | [diff] [blame] | 799 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 800 | super(CoarseTenant, self).save() |
| 801 | |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 802 | |
| 803 | class Subscriber(TenantRoot): |
| 804 | """ Intermediate class for TenantRoots that are to be Subscribers """ |
| 805 | |
| 806 | class Meta: |
| 807 | proxy = True |
| 808 | |
| 809 | KIND = "Subscriber" |
| 810 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 811 | |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 812 | class Provider(TenantRoot): |
| 813 | """ Intermediate class for TenantRoots that are to be Providers """ |
| 814 | |
| 815 | class Meta: |
| 816 | proxy = True |
| 817 | |
| 818 | KIND = "Provider" |
| 819 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 820 | |
Scott Baker | 462a1d9 | 2015-10-15 15:59:19 -0700 | [diff] [blame] | 821 | class TenantAttribute(PlCoreBase): |
Scott Baker | 907f991 | 2015-10-20 17:12:36 -0700 | [diff] [blame] | 822 | name = models.CharField(help_text="Attribute Name", max_length=128) |
Scott Baker | 462a1d9 | 2015-10-15 15:59:19 -0700 | [diff] [blame] | 823 | value = models.TextField(help_text="Attribute Value") |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 824 | tenant = models.ForeignKey(Tenant, related_name='tenantattributes', |
| 825 | help_text="The Tenant this attribute is associated with") |
Scott Baker | 462a1d9 | 2015-10-15 15:59:19 -0700 | [diff] [blame] | 826 | |
Scott Baker | 1271a4d | 2016-04-06 14:34:49 -0700 | [diff] [blame] | 827 | def __unicode__(self): return u'%s-%s' % (self.name, self.id) |
| 828 | |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 829 | |
| 830 | class TenantRootRole(PlCoreBase): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 831 | ROLE_CHOICES = (('admin', 'Admin'), ('access', 'Access')) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 832 | |
| 833 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 834 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 835 | def __unicode__(self): return u'%s' % (self.role) |
| 836 | |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 837 | |
| 838 | class TenantRootPrivilege(PlCoreBase): |
| 839 | user = models.ForeignKey('User', related_name="tenant_root_privileges") |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 840 | tenant_root = models.ForeignKey( |
| 841 | 'TenantRoot', related_name="tenant_root_privileges") |
| 842 | role = models.ForeignKey( |
| 843 | 'TenantRootRole', related_name="tenant_root_privileges") |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 844 | |
| 845 | class Meta: |
| 846 | unique_together = ('user', 'tenant_root', 'role') |
| 847 | |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 848 | def __unicode__(self): return u'%s %s %s' % ( |
| 849 | self.tenant_root, self.user, self.role) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 850 | |
| 851 | def save(self, *args, **kwds): |
| 852 | if not self.user.is_active: |
| 853 | raise PermissionDenied, "Cannot modify role(s) of a disabled user" |
Scott Baker | 335882a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 854 | super(TenantRootPrivilege, self).save(*args, **kwds) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 855 | |
| 856 | def can_update(self, user): |
Scott Baker | 335882a | 2015-07-24 10:15:31 -0700 | [diff] [blame] | 857 | return user.can_update_tenant_root_privilege(self) |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 858 | |
Scott Baker | 16573d3 | 2015-07-24 15:36:02 -0700 | [diff] [blame] | 859 | @classmethod |
| 860 | def select_by_user(cls, user): |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 861 | if user.is_admin: |
Scott Baker | 5116b30 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 862 | return cls.objects.all() |
Scott Baker | 4587b82 | 2015-07-01 18:29:08 -0700 | [diff] [blame] | 863 | else: |
Scott Baker | 5116b30 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 864 | # User can see his own privilege |
| 865 | trp_ids = [trp.id for trp in cls.objects.filter(user=user)] |
| 866 | |
| 867 | # A slice admin can see the SlicePrivileges for his Slice |
| 868 | for priv in cls.objects.filter(user=user, role__role="admin"): |
Jeremy Mowery | 11ed899 | 2016-04-17 20:56:54 -0700 | [diff] [blame] | 869 | trp_ids.extend( |
| 870 | [trp.id for trp in cls.objects.filter(tenant_root=priv.tenant_root)]) |
Scott Baker | 5116b30 | 2015-07-24 15:48:03 -0700 | [diff] [blame] | 871 | |
| 872 | return cls.objects.filter(id__in=trp_ids) |
| 873 | |
Jeremy Mowery | 9563513 | 2016-04-15 17:39:49 -0700 | [diff] [blame] | 874 | |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 875 | class TenantRole(PlCoreBase): |
Jeremy Mowery | 98e97d7 | 2016-04-15 00:05:27 -0700 | [diff] [blame] | 876 | """A TenantRole option.""" |
Jeremy Mowery | 9563513 | 2016-04-15 17:39:49 -0700 | [diff] [blame] | 877 | ROLE_CHOICES = (('admin', 'Admin'), ('access', 'Access')) |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 878 | role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
Jeremy Mowery | 9563513 | 2016-04-15 17:39:49 -0700 | [diff] [blame] | 879 | |
| 880 | def __unicode__(self): return u'%s' % (self.role) |
| 881 | |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 882 | |
| 883 | class TenantPrivilege(PlCoreBase): |
Jeremy Mowery | 98e97d7 | 2016-04-15 00:05:27 -0700 | [diff] [blame] | 884 | """"A TenantPrivilege which defines how users can access a particular Tenant. |
| 885 | |
| 886 | Attributes: |
| 887 | id (models.AutoField): The ID of the privilege. |
| 888 | user (models.ForeignKey): A Foreign Key to the a User. |
| 889 | tenant (models.ForeignKey): A ForeignKey to the Tenant. |
| 890 | role (models.ForeignKey): A ForeignKey to the TenantRole. |
| 891 | """ |
Jeremy Mowery | c86352a | 2016-04-10 23:00:54 -0700 | [diff] [blame] | 892 | id = models.AutoField(primary_key=True) |
Jeremy Mowery | 5d06a23 | 2016-04-04 22:30:44 -0700 | [diff] [blame] | 893 | user = models.ForeignKey('User', related_name="tenantprivileges") |
| 894 | tenant = models.ForeignKey('Tenant', related_name="tenantprivileges") |
| 895 | role = models.ForeignKey('TenantRole', related_name="tenantprivileges") |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 896 | |
Jeremy Mowery | 9563513 | 2016-04-15 17:39:49 -0700 | [diff] [blame] | 897 | def __unicode__(self): return u'%s %s %s' % ( |
| 898 | self.tenant, self.user, self.role) |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 899 | |
| 900 | def save(self, *args, **kwds): |
| 901 | if not self.user.is_active: |
| 902 | raise PermissionDenied, "Cannot modify role(s) of a disabled user" |
| 903 | super(TenantPrivilege, self).save(*args, **kwds) |
| 904 | |
| 905 | def can_update(self, user): |
| 906 | return user.can_update_tenant_privilege(self) |
| 907 | |
| 908 | @classmethod |
| 909 | def select_by_user(cls, user): |
| 910 | if user.is_admin: |
| 911 | return cls.objects.all() |
| 912 | else: |
| 913 | # User can see his own privilege |
| 914 | trp_ids = [trp.id for trp in cls.objects.filter(user=user)] |
| 915 | |
| 916 | # A tenant admin can see the TenantPrivileges for their Tenants |
| 917 | for priv in cls.objects.filter(user=user, role__role="admin"): |
Jeremy Mowery | 9563513 | 2016-04-15 17:39:49 -0700 | [diff] [blame] | 918 | trp_ids.extend( |
| 919 | [trp.id for trp in cls.objects.filter(tenant=priv.tenant)]) |
Jeremy Mowery | b31bd9e | 2016-03-14 23:59:11 -0700 | [diff] [blame] | 920 | |
| 921 | return cls.objects.filter(id__in=trp_ids) |