blob: f368bbe8133f9b8d7ba597a4c42556f421ccb8e7 [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
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -04004from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
Siobhan Tullyde5450d2013-06-21 11:35:33 -04005from core.models import Tag
6from django.contrib.contenttypes import generic
Siobhan Tully567e3e62013-06-21 18:03:16 -04007from geoposition.fields import GeopositionField
Scott Baker5380c522014-06-06 14:49:43 -07008from core.acl import AccessControlList
Sapan Bhatia1d1b2b12014-09-22 17:25:06 -04009from planetstack.config import Config
10
11config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012
Tony Mack51c4a7d2014-11-30 15:33:35 -050013class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatiac4b98072014-09-19 16:48:36 -040014 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050015 parent=super(ControllerLinkDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040016 try:
17 backend_type = config.observer_backend_type
18 except AttributeError:
19 backend_type = None
20
21 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
22 if (backend_type):
Tony Mack51c4a7d2014-11-30 15:33:35 -050023 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatiac4b98072014-09-19 16:48:36 -040024 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040025 return parent_queryset
Sapan Bhatiac4b98072014-09-19 16:48:36 -040026
27 # deprecated in django 1.7 in favor of get_queryset().
28 def get_query_set(self):
29 return self.get_queryset()
30
31
Tony Mack51c4a7d2014-11-30 15:33:35 -050032class ControllerDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040033 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050034 parent=super(ControllerDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040035
36 try:
37 backend_type = config.observer_backend_type
38 except AttributeError:
39 backend_type = None
40
41 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
42
43 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040044 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040045 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040046 return parent_queryset
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040047
48 # deprecated in django 1.7 in favor of get_queryset().
49 def get_query_set(self):
50 return self.get_queryset()
51
Tony Mack51c4a7d2014-11-30 15:33:35 -050052class ControllerLinkManager(PlCoreBaseManager):
Sapan Bhatia48df09f2014-09-19 16:47:40 -040053 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050054 parent=super(ControllerLinkManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040055
56 try:
57 backend_type = config.observer_backend_type
58 except AttributeError:
59 backend_type = None
60
61 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
62
63 if backend_type:
Tony Mack51c4a7d2014-11-30 15:33:35 -050064 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatia48df09f2014-09-19 16:47:40 -040065 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040066 return parent_queryset
Sapan Bhatia48df09f2014-09-19 16:47:40 -040067
68 # deprecated in django 1.7 in favor of get_queryset().
69 def get_query_set(self):
70 return self.get_queryset()
71
72
Tony Mack51c4a7d2014-11-30 15:33:35 -050073class ControllerManager(PlCoreBaseManager):
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040074 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050075 parent=super(ControllerManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040076
77 try:
78 backend_type = config.observer_backend_type
79 except AttributeError:
80 backend_type = None
81
82 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
83
84 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040085 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040086 else:
Sapan Bhatiaf77d01a2014-09-24 00:34:44 -040087 return parent_queryset
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040088
89 # deprecated in django 1.7 in favor of get_queryset().
90 def get_query_set(self):
91 return self.get_queryset()
92
Siobhan Tully4bc09f22013-04-10 21:15:21 -040093class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050094 """
95 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.
96 """
Siobhan Tully4bc09f22013-04-10 21:15:21 -040097 name = models.CharField(max_length=200, help_text="Name for this Site")
98 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
99 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -0400100 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400101 longitude = models.FloatField(null=True, blank=True)
102 latitude = models.FloatField(null=True, blank=True)
103 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
104 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
105 abbreviated_name = models.CharField(max_length=80)
106
Tony Macke4be32f2014-03-11 20:45:25 -0400107 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Tony Mack3066a952015-01-05 22:48:11 -0500108 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 -0400109 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400110
111 def __unicode__(self): return u'%s' % (self.name)
112
Tony Mack5b061472014-02-04 07:57:10 -0500113 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500114 if user.is_readonly:
115 return False
Tony Mack5b061472014-02-04 07:57:10 -0500116 if user.is_admin:
117 return True
118 site_privs = SitePrivilege.objects.filter(user=user, site=self)
119 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500120 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -0500121 return True
122 return False
123
Tony Mack5b061472014-02-04 07:57:10 -0500124 @staticmethod
125 def select_by_user(user):
126 if user.is_admin:
127 qs = Site.objects.all()
128 else:
129 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
130 site_ids.append(user.site.id)
131 qs = Site.objects.filter(id__in=site_ids)
132 return qs
133
134
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400135class SiteRole(PlCoreBase):
136
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500137 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400138 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
139
140 def __unicode__(self): return u'%s' % (self.role)
141
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400142class SitePrivilege(PlCoreBase):
143
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500144 user = models.ForeignKey('User', related_name='siteprivileges')
145 site = models.ForeignKey('Site', related_name='siteprivileges')
146 role = models.ForeignKey('SiteRole',related_name='siteprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400147
148 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
149
Tony Mack00d361f2013-04-28 10:28:42 -0400150 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400151 super(SitePrivilege, self).save(*args, **kwds)
152
153 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400154 super(SitePrivilege, self).delete(*args, **kwds)
155
Tony Mack5b061472014-02-04 07:57:10 -0500156 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500157 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500158
Tony Mack5b061472014-02-04 07:57:10 -0500159 @staticmethod
160 def select_by_user(user):
161 if user.is_admin:
162 qs = SitePrivilege.objects.all()
163 else:
164 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
165 qs = SitePrivilege.objects.filter(id__in=sp_ids)
166 return qs
167
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500168class Deployment(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500169 #objects = Controllermanager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500170 #deleted_objects = DeploymentDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500171 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Tony Mack51c4a7d2014-11-30 15:33:35 -0500172 #admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
173 #admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
174 #admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
175 #auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
176 #backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
177 #availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Scott Baker5380c522014-06-06 14:49:43 -0700178
179 # smbaker: the default of 'allow all' is intended for evolutions of existing
180 # deployments. When new deployments are created via the GUI, they are
181 # given a default of 'allow site <site_of_creator>'
182 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
183 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
184
185 def get_acl(self):
186 return AccessControlList(self.accessControl)
187
188 def test_acl(self, slice=None, user=None):
189 potential_users=[]
190
191 if user:
192 potential_users.append(user)
193
194 if slice:
195 potential_users.append(slice.creator)
Sapan Bhatiad2d5dd82014-11-11 21:10:07 -0500196 for priv in slice.sliceprivileges.all():
Scott Baker5380c522014-06-06 14:49:43 -0700197 if priv.user not in potential_users:
198 potential_users.append(priv.user)
199
200 acl = self.get_acl()
201 for user in potential_users:
202 if acl.test(user) == "allow":
203 return True
204
205 return False
206
Scott Bakercb95fde2014-06-06 16:09:51 -0700207 @staticmethod
208 def select_by_acl(user):
209 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700210 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700211 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700212 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700213 ids.append(deployment.id)
214
215 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500216
Tony Mackcf29cfa2015-02-05 06:13:04 -0500217 def can_update(self, user):
218 if user.is_readonly:
219 return False
220 if user.is_admin:
221 return True
222
223 if self.deploymentprivileges.filter(user=user, role__role='admin'):
224 return True
225
226 return False
227
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500228 def __unicode__(self): return u'%s' % (self.name)
229
Tony Mack68a1e422014-12-08 16:43:02 -0500230class DeploymentRole(PlCoreBase):
231 #objects = DeploymentLinkManager()
232 #deleted_objects = DeploymentLinkDeletionManager()
233 ROLE_CHOICES = (('admin','Admin'),)
234 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
235
236 def __unicode__(self): return u'%s' % (self.role)
237
238class DeploymentPrivilege(PlCoreBase):
239 #objects = DeploymentLinkManager()
240 #deleted_objects = DeploymentLinkDeletionManager()
241
242 user = models.ForeignKey('User', related_name='deploymentprivileges')
243 deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
244 role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
245
246 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
247
248 def can_update(self, user):
249 if user.is_readonly:
250 return False
251 if user.is_admin:
252 return True
253 dprivs = DeploymentPrivilege.objects.filter(user=user)
254 for dpriv in dprivs:
255 if dpriv.role.role == 'admin':
256 return True
257 return False
258
259 @staticmethod
260 def select_by_user(user):
261 if user.is_admin:
262 qs = DeploymentPrivilege.objects.all()
263 else:
264 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
265 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
266 return qs
267
Tony Mack51c4a7d2014-11-30 15:33:35 -0500268class ControllerRole(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500269 #objects = ControllerLinkManager()
270 #deleted_objects = ControllerLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500271
272 ROLE_CHOICES = (('admin','Admin'),)
273 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
274
275 def __unicode__(self): return u'%s' % (self.role)
276
Tony Mackd14d48f2014-12-05 17:13:08 -0500277class Controller(PlCoreBase):
278
Tony Mack51c4a7d2014-11-30 15:33:35 -0500279 objects = ControllerManager()
280 deleted_objects = ControllerDeletionManager()
Sapan Bhatia51ddecd2014-09-22 14:54:56 -0400281
Tony Mackd14d48f2014-12-05 17:13:08 -0500282 name = models.CharField(max_length=200, unique=True, help_text="Name of the Controller")
Tony Mackad2208f2015-01-19 15:05:42 -0500283 backend_type = models.CharField(max_length=200, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
Tony Mack74364932015-01-06 17:49:25 -0500284 version = models.CharField(max_length=200, help_text="Controller version")
Tony Mack51c4a7d2014-11-30 15:33:35 -0500285 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
286 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
287 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
288 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
289
Tony Mack625e10e2014-12-26 12:15:42 -0500290 def __unicode__(self): return u'%s %s %s' % (self.name, self.backend_type, self.version)
Tony Mack51c4a7d2014-11-30 15:33:35 -0500291
Tony Mack2e897fa2015-01-13 17:33:08 -0500292 def can_update(self, user):
293 if user.is_readonly:
294 return False
295 if user.is_admin:
296 return True
297 return False
298
Tony Mack3066a952015-01-05 22:48:11 -0500299class SiteDeployment(PlCoreBase):
Tony Mack51c4a7d2014-11-30 15:33:35 -0500300 objects = ControllerLinkManager()
Tony Mackd14d48f2014-12-05 17:13:08 -0500301 deleted_objects = ControllerLinkDeletionManager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500302
Tony Mack30dfcd72015-01-10 23:08:10 -0500303 site = models.ForeignKey(Site,related_name='sitedeployments')
304 deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
305 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='sitedeployments')
Tony Mackd14d48f2014-12-05 17:13:08 -0500306 availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Tony Mackd14d48f2014-12-05 17:13:08 -0500307
Tony Mack81ad09e2015-01-03 17:26:06 -0500308 def __unicode__(self): return u'%s %s' % (self.deployment, self.site)
Tony Mack3066a952015-01-05 22:48:11 -0500309
310class ControllerSite(PlCoreBase):
311
312 site = models.ForeignKey(Site,related_name='controllersite')
313 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='controllersite')
314 tenant_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone tenant id")