blob: 1e8c7caee9c7dccaa1057a1ed31f1a14e6bcab72 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatia4a62a2f2014-09-22 18:47:53 -04003from django.db.models import Q
Siobhan Tullyde5450d2013-06-21 11:35:33 -04004from django.contrib.contenttypes import generic
Tony Macka6928d62015-03-29 09:29:12 -04005from django.core.exceptions import PermissionDenied
Siobhan Tully567e3e62013-06-21 18:03:16 -04006from geoposition.fields import GeopositionField
Tony Mack50e12212015-03-09 13:03:56 -04007from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
8from core.models import Tag
9from core.models.plcorebase import StrippedCharField
Scott Baker5380c522014-06-06 14:49:43 -070010from core.acl import AccessControlList
Scott Baker86e132c2015-02-11 21:38:09 -080011from xos.config import Config
Sapan Bhatia1d1b2b12014-09-22 17:25:06 -040012
13config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014
Tony Mack51c4a7d2014-11-30 15:33:35 -050015class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatiac4b98072014-09-19 16:48:36 -040016 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050017 parent=super(ControllerLinkDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040018 try:
19 backend_type = config.observer_backend_type
20 except AttributeError:
21 backend_type = None
22
23 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
24 if (backend_type):
Tony Mack51c4a7d2014-11-30 15:33:35 -050025 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatiac4b98072014-09-19 16:48:36 -040026 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040027 return parent_queryset
Sapan Bhatiac4b98072014-09-19 16:48:36 -040028
29 # deprecated in django 1.7 in favor of get_queryset().
30 def get_query_set(self):
31 return self.get_queryset()
32
33
Tony Mack51c4a7d2014-11-30 15:33:35 -050034class ControllerDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040035 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050036 parent=super(ControllerDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040037
38 try:
39 backend_type = config.observer_backend_type
40 except AttributeError:
41 backend_type = None
42
43 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
44
45 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040046 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040047 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040048 return parent_queryset
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040049
50 # deprecated in django 1.7 in favor of get_queryset().
51 def get_query_set(self):
52 return self.get_queryset()
53
Tony Mack51c4a7d2014-11-30 15:33:35 -050054class ControllerLinkManager(PlCoreBaseManager):
Sapan Bhatia48df09f2014-09-19 16:47:40 -040055 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050056 parent=super(ControllerLinkManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040057
58 try:
59 backend_type = config.observer_backend_type
60 except AttributeError:
61 backend_type = None
62
63 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
64
65 if backend_type:
Tony Mack51c4a7d2014-11-30 15:33:35 -050066 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatia48df09f2014-09-19 16:47:40 -040067 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040068 return parent_queryset
Sapan Bhatia48df09f2014-09-19 16:47:40 -040069
70 # deprecated in django 1.7 in favor of get_queryset().
71 def get_query_set(self):
72 return self.get_queryset()
73
74
Tony Mack51c4a7d2014-11-30 15:33:35 -050075class ControllerManager(PlCoreBaseManager):
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040076 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050077 parent=super(ControllerManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040078
79 try:
80 backend_type = config.observer_backend_type
81 except AttributeError:
82 backend_type = None
83
84 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
85
86 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040087 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040088 else:
Sapan Bhatiaf77d01a2014-09-24 00:34:44 -040089 return parent_queryset
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040090
91 # deprecated in django 1.7 in favor of get_queryset().
92 def get_query_set(self):
93 return self.get_queryset()
94
Siobhan Tully4bc09f22013-04-10 21:15:21 -040095class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050096 """
97 A logical grouping of Nodes that are co-located at the same geographic location, which also typically corresponds to the Nodes' location in the physical network.
98 """
Tony Mack50e12212015-03-09 13:03:56 -040099 name = StrippedCharField(max_length=200, help_text="Name for this Site")
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400100 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
101 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -0400102 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400103 longitude = models.FloatField(null=True, blank=True)
104 latitude = models.FloatField(null=True, blank=True)
Tony Mack50e12212015-03-09 13:03:56 -0400105 login_base = StrippedCharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400106 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
Tony Mack50e12212015-03-09 13:03:56 -0400107 abbreviated_name = StrippedCharField(max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400108
Tony Macke4be32f2014-03-11 20:45:25 -0400109 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Tony Mack3066a952015-01-05 22:48:11 -0500110 deployments = models.ManyToManyField('Deployment', through='SiteDeployment', blank=True, help_text="Select which sites are allowed to host nodes in this deployment", related_name='sites')
Siobhan Tullyde5450d2013-06-21 11:35:33 -0400111 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400112
113 def __unicode__(self): return u'%s' % (self.name)
114
Tony Mack5b061472014-02-04 07:57:10 -0500115 def can_update(self, user):
Tony Mack3428e6e2015-02-08 21:38:41 -0500116 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500117
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400118class SiteRole(PlCoreBase):
119
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500120 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Tony Mack50e12212015-03-09 13:03:56 -0400121 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400122
123 def __unicode__(self): return u'%s' % (self.role)
124
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400125class SitePrivilege(PlCoreBase):
126
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500127 user = models.ForeignKey('User', related_name='siteprivileges')
128 site = models.ForeignKey('Site', related_name='siteprivileges')
129 role = models.ForeignKey('SiteRole',related_name='siteprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400130
131 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
132
Tony Mack00d361f2013-04-28 10:28:42 -0400133 def save(self, *args, **kwds):
Tony Macka6928d62015-03-29 09:29:12 -0400134 if not self.user.is_active:
135 raise PermissionDenied, "Cannot modify role(s) of a disabled user"
Tony Mack00d361f2013-04-28 10:28:42 -0400136 super(SitePrivilege, self).save(*args, **kwds)
137
138 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400139 super(SitePrivilege, self).delete(*args, **kwds)
140
Tony Mack5b061472014-02-04 07:57:10 -0500141 def can_update(self, user):
Tony Mack3428e6e2015-02-08 21:38:41 -0500142 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500143
Tony Mack5b061472014-02-04 07:57:10 -0500144 @staticmethod
145 def select_by_user(user):
146 if user.is_admin:
147 qs = SitePrivilege.objects.all()
148 else:
149 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
150 qs = SitePrivilege.objects.filter(id__in=sp_ids)
151 return qs
152
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500153class Deployment(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500154 #objects = Controllermanager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500155 #deleted_objects = DeploymentDeletionManager()
Tony Mack50e12212015-03-09 13:03:56 -0400156 name = StrippedCharField(max_length=200, unique=True, help_text="Name of the Deployment")
157 #admin_user = StrippedCharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
158 #admin_password = StrippedCharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
159 #admin_tenant = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
160 #auth_url = StrippedCharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
161 #backend_type = StrippedCharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
162 #availability_zone = StrippedCharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Scott Baker5380c522014-06-06 14:49:43 -0700163
164 # smbaker: the default of 'allow all' is intended for evolutions of existing
165 # deployments. When new deployments are created via the GUI, they are
166 # given a default of 'allow site <site_of_creator>'
167 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
168 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
169
170 def get_acl(self):
171 return AccessControlList(self.accessControl)
172
173 def test_acl(self, slice=None, user=None):
174 potential_users=[]
175
176 if user:
177 potential_users.append(user)
178
179 if slice:
180 potential_users.append(slice.creator)
Sapan Bhatiad2d5dd82014-11-11 21:10:07 -0500181 for priv in slice.sliceprivileges.all():
Scott Baker5380c522014-06-06 14:49:43 -0700182 if priv.user not in potential_users:
183 potential_users.append(priv.user)
184
185 acl = self.get_acl()
186 for user in potential_users:
187 if acl.test(user) == "allow":
188 return True
189
190 return False
191
Scott Bakercb95fde2014-06-06 16:09:51 -0700192 @staticmethod
193 def select_by_acl(user):
194 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700195 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700196 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700197 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700198 ids.append(deployment.id)
199
200 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500201
Tony Mackcf29cfa2015-02-05 06:13:04 -0500202 def can_update(self, user):
S.Çağlar Onurb9cf3232015-02-09 14:53:40 -0500203 return user.can_update_deployment(self)
Tony Mack3428e6e2015-02-08 21:38:41 -0500204
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500205 def __unicode__(self): return u'%s' % (self.name)
206
Tony Mack68a1e422014-12-08 16:43:02 -0500207class DeploymentRole(PlCoreBase):
208 #objects = DeploymentLinkManager()
209 #deleted_objects = DeploymentLinkDeletionManager()
210 ROLE_CHOICES = (('admin','Admin'),)
Tony Mack50e12212015-03-09 13:03:56 -0400211 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Tony Mack68a1e422014-12-08 16:43:02 -0500212
213 def __unicode__(self): return u'%s' % (self.role)
214
215class DeploymentPrivilege(PlCoreBase):
216 #objects = DeploymentLinkManager()
217 #deleted_objects = DeploymentLinkDeletionManager()
218
219 user = models.ForeignKey('User', related_name='deploymentprivileges')
220 deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
221 role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
Tony Mack5d93a9e2015-04-11 12:17:59 -0400222 class Meta:
223 unique_together = ('user', 'deployment', 'role')
Tony Mack68a1e422014-12-08 16:43:02 -0500224
225 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
226
227 def can_update(self, user):
S.Çağlar Onurb9cf3232015-02-09 14:53:40 -0500228 return user.can_update_deployment(self)
Tony Mack68a1e422014-12-08 16:43:02 -0500229
230 @staticmethod
231 def select_by_user(user):
232 if user.is_admin:
233 qs = DeploymentPrivilege.objects.all()
234 else:
235 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
236 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
237 return qs
238
Tony Mack51c4a7d2014-11-30 15:33:35 -0500239class ControllerRole(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500240 #objects = ControllerLinkManager()
241 #deleted_objects = ControllerLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500242
243 ROLE_CHOICES = (('admin','Admin'),)
Tony Mack50e12212015-03-09 13:03:56 -0400244 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500245
246 def __unicode__(self): return u'%s' % (self.role)
247
Tony Mackd14d48f2014-12-05 17:13:08 -0500248class Controller(PlCoreBase):
249
Tony Mack51c4a7d2014-11-30 15:33:35 -0500250 objects = ControllerManager()
251 deleted_objects = ControllerDeletionManager()
Sapan Bhatia51ddecd2014-09-22 14:54:56 -0400252
Tony Mack50e12212015-03-09 13:03:56 -0400253 name = StrippedCharField(max_length=200, unique=True, help_text="Name of the Controller")
254 backend_type = StrippedCharField(max_length=200, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
255 version = StrippedCharField(max_length=200, help_text="Controller version")
256 auth_url = StrippedCharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
257 admin_user = StrippedCharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
258 admin_password = StrippedCharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
259 admin_tenant = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
260 domain = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the domain this controller belongs to")
Tony Mack5817cb42015-02-16 19:54:24 -0500261 deployment = models.ForeignKey(Deployment,related_name='controllerdeployments')
Tony Mack78fc1362015-02-18 11:41:36 -0500262
Tony Mack51c4a7d2014-11-30 15:33:35 -0500263
Tony Mack625e10e2014-12-26 12:15:42 -0500264 def __unicode__(self): return u'%s %s %s' % (self.name, self.backend_type, self.version)
Tony Mack51c4a7d2014-11-30 15:33:35 -0500265
Tony Mack78fc1362015-02-18 11:41:36 -0500266 @staticmethod
267 def select_by_user(user):
268
269 if user.is_admin:
270 qs = Controller.objects.all()
271 else:
272 deployments = [dp.deployment for dp in DeploymentPrivilege.objects.filter(user=user, role__role__in=['Admin', 'admin'])]
273 qs = Controller.objects.filter(deployment__in=deployments)
Tony Mackf8e8cd22015-02-18 13:54:58 -0500274 return qs
Tony Mack78fc1362015-02-18 11:41:36 -0500275
Tony Mack3066a952015-01-05 22:48:11 -0500276class SiteDeployment(PlCoreBase):
Tony Mack51c4a7d2014-11-30 15:33:35 -0500277 objects = ControllerLinkManager()
Tony Mackd14d48f2014-12-05 17:13:08 -0500278 deleted_objects = ControllerLinkDeletionManager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500279
Tony Mack30dfcd72015-01-10 23:08:10 -0500280 site = models.ForeignKey(Site,related_name='sitedeployments')
281 deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
282 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='sitedeployments')
Tony Mack50e12212015-03-09 13:03:56 -0400283 availability_zone = StrippedCharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Tony Mackd14d48f2014-12-05 17:13:08 -0500284
Tony Mack5d93a9e2015-04-11 12:17:59 -0400285 class Meta:
286 unique_together = ('site', 'deployment', 'controller')
Tony Mackc8836df2015-03-09 17:13:14 -0400287
Tony Mack81ad09e2015-01-03 17:26:06 -0500288 def __unicode__(self): return u'%s %s' % (self.deployment, self.site)
Tony Mack3066a952015-01-05 22:48:11 -0500289
290class ControllerSite(PlCoreBase):
291
292 site = models.ForeignKey(Site,related_name='controllersite')
293 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='controllersite')
Tony Mack50e12212015-03-09 13:03:56 -0400294 tenant_id = StrippedCharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone tenant id")
Tony Mackc8836df2015-03-09 17:13:14 -0400295
Tony Mack5d93a9e2015-04-11 12:17:59 -0400296 class Meta:
297 unique_together = ('site', 'controller')