blob: 449a72bbb5b082cb82e80201c8715a82a48659f6 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatiac1f20ef2014-09-22 18:47:53 -04003from django.db.models import Q
Sapan Bhatiac6680c12014-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
Scott Baker76a840e2015-02-11 21:38:09 -08009from xos.config import Config
Sapan Bhatia699a0442014-09-22 17:25:06 -040010
11config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012
Tony Mackf3bbe472014-11-30 15:33:35 -050013class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatia71058ec2014-09-19 16:48:36 -040014 def get_queryset(self):
Tony Mack336e0f92014-11-30 15:53:08 -050015 parent=super(ControllerLinkDeletionManager, self)
Sapan Bhatiab8cc1142014-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 Mackf3bbe472014-11-30 15:33:35 -050023 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatia71058ec2014-09-19 16:48:36 -040024 else:
Sapan Bhatiab8cc1142014-09-23 13:14:31 -040025 return parent_queryset
Sapan Bhatia71058ec2014-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 Mackf3bbe472014-11-30 15:33:35 -050032class ControllerDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatia005d7612014-09-19 16:48:11 -040033 def get_queryset(self):
Tony Mackf3bbe472014-11-30 15:33:35 -050034 parent=super(ControllerDeletionManager, self)
Sapan Bhatiab8cc1142014-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 Bhatiab3c838a2014-10-08 09:38:21 -040044 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatia005d7612014-09-19 16:48:11 -040045 else:
Sapan Bhatiab8cc1142014-09-23 13:14:31 -040046 return parent_queryset
Sapan Bhatia005d7612014-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 Mackf3bbe472014-11-30 15:33:35 -050052class ControllerLinkManager(PlCoreBaseManager):
Sapan Bhatiad8a6eb52014-09-19 16:47:40 -040053 def get_queryset(self):
Tony Mackf3bbe472014-11-30 15:33:35 -050054 parent=super(ControllerLinkManager, self)
Sapan Bhatiab8cc1142014-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 Mackf3bbe472014-11-30 15:33:35 -050064 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatiad8a6eb52014-09-19 16:47:40 -040065 else:
Sapan Bhatiab8cc1142014-09-23 13:14:31 -040066 return parent_queryset
Sapan Bhatiad8a6eb52014-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 Mackf3bbe472014-11-30 15:33:35 -050073class ControllerManager(PlCoreBaseManager):
Sapan Bhatiac6680c12014-09-19 16:47:07 -040074 def get_queryset(self):
Tony Mack336e0f92014-11-30 15:53:08 -050075 parent=super(ControllerManager, self)
Sapan Bhatiab8cc1142014-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 Bhatiab3c838a2014-10-08 09:38:21 -040085 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatiac6680c12014-09-19 16:47:07 -040086 else:
Sapan Bhatia90025d72014-09-24 00:34:44 -040087 return parent_queryset
Sapan Bhatiac6680c12014-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 Macka7dbd422015-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 Mack5ff90fc2015-02-08 21:38:41 -0500114 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500115
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400116class SiteRole(PlCoreBase):
117
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500118 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400119 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
120
121 def __unicode__(self): return u'%s' % (self.role)
122
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400123class SitePrivilege(PlCoreBase):
124
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500125 user = models.ForeignKey('User', related_name='siteprivileges')
126 site = models.ForeignKey('Site', related_name='siteprivileges')
127 role = models.ForeignKey('SiteRole',related_name='siteprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400128
129 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
130
Tony Mack00d361f2013-04-28 10:28:42 -0400131 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400132 super(SitePrivilege, self).save(*args, **kwds)
133
134 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400135 super(SitePrivilege, self).delete(*args, **kwds)
136
Tony Mack5b061472014-02-04 07:57:10 -0500137 def can_update(self, user):
Tony Mack5ff90fc2015-02-08 21:38:41 -0500138 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500139
Tony Mack5b061472014-02-04 07:57:10 -0500140 @staticmethod
141 def select_by_user(user):
142 if user.is_admin:
143 qs = SitePrivilege.objects.all()
144 else:
145 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
146 qs = SitePrivilege.objects.filter(id__in=sp_ids)
147 return qs
148
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500149class Deployment(PlCoreBase):
Tony Mack336e0f92014-11-30 15:53:08 -0500150 #objects = Controllermanager()
Tony Mackf3bbe472014-11-30 15:33:35 -0500151 #deleted_objects = DeploymentDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500152 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Tony Mackf3bbe472014-11-30 15:33:35 -0500153 #admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
154 #admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
155 #admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
156 #auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
157 #backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
158 #availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Scott Baker5380c522014-06-06 14:49:43 -0700159
160 # smbaker: the default of 'allow all' is intended for evolutions of existing
161 # deployments. When new deployments are created via the GUI, they are
162 # given a default of 'allow site <site_of_creator>'
163 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
164 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
165
166 def get_acl(self):
167 return AccessControlList(self.accessControl)
168
169 def test_acl(self, slice=None, user=None):
170 potential_users=[]
171
172 if user:
173 potential_users.append(user)
174
175 if slice:
176 potential_users.append(slice.creator)
Sapan Bhatia900baf92014-11-11 21:10:07 -0500177 for priv in slice.sliceprivileges.all():
Scott Baker5380c522014-06-06 14:49:43 -0700178 if priv.user not in potential_users:
179 potential_users.append(priv.user)
180
181 acl = self.get_acl()
182 for user in potential_users:
183 if acl.test(user) == "allow":
184 return True
185
186 return False
187
Scott Bakercb95fde2014-06-06 16:09:51 -0700188 @staticmethod
189 def select_by_acl(user):
190 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700191 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700192 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700193 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700194 ids.append(deployment.id)
195
196 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500197
Tony Mackd893dfb2015-02-05 06:13:04 -0500198 def can_update(self, user):
S.Çağlar Onurfeb377f2015-02-09 14:53:40 -0500199 return user.can_update_deployment(self)
Tony Mack5ff90fc2015-02-08 21:38:41 -0500200
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500201 def __unicode__(self): return u'%s' % (self.name)
202
Tony Mack93d1b032014-12-08 16:43:02 -0500203class DeploymentRole(PlCoreBase):
204 #objects = DeploymentLinkManager()
205 #deleted_objects = DeploymentLinkDeletionManager()
206 ROLE_CHOICES = (('admin','Admin'),)
207 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
208
209 def __unicode__(self): return u'%s' % (self.role)
210
211class DeploymentPrivilege(PlCoreBase):
212 #objects = DeploymentLinkManager()
213 #deleted_objects = DeploymentLinkDeletionManager()
214
215 user = models.ForeignKey('User', related_name='deploymentprivileges')
216 deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
217 role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
218
219 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
220
221 def can_update(self, user):
S.Çağlar Onurfeb377f2015-02-09 14:53:40 -0500222 return user.can_update_deployment(self)
Tony Mack93d1b032014-12-08 16:43:02 -0500223
224 @staticmethod
225 def select_by_user(user):
226 if user.is_admin:
227 qs = DeploymentPrivilege.objects.all()
228 else:
229 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
230 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
231 return qs
232
Tony Mackf3bbe472014-11-30 15:33:35 -0500233class ControllerRole(PlCoreBase):
Tony Mack336e0f92014-11-30 15:53:08 -0500234 #objects = ControllerLinkManager()
235 #deleted_objects = ControllerLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500236
237 ROLE_CHOICES = (('admin','Admin'),)
238 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
239
240 def __unicode__(self): return u'%s' % (self.role)
241
Tony Mack528d4222014-12-05 17:13:08 -0500242class Controller(PlCoreBase):
243
Tony Mackf3bbe472014-11-30 15:33:35 -0500244 objects = ControllerManager()
245 deleted_objects = ControllerDeletionManager()
Sapan Bhatia6fc6e2b2014-09-22 14:54:56 -0400246
Tony Mack528d4222014-12-05 17:13:08 -0500247 name = models.CharField(max_length=200, unique=True, help_text="Name of the Controller")
Tony Mack78d4c3d2015-01-19 15:05:42 -0500248 backend_type = models.CharField(max_length=200, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
Tony Mack26564362015-01-06 17:49:25 -0500249 version = models.CharField(max_length=200, help_text="Controller version")
Tony Mackf3bbe472014-11-30 15:33:35 -0500250 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
251 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
252 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
253 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
Tony Mack4cea77e2015-02-10 12:43:07 -0500254 domain = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the domain this controller belongs to")
Tony Mack7b6400d2015-02-16 19:54:24 -0500255 deployment = models.ForeignKey(Deployment,related_name='controllerdeployments')
256
Tony Mackf3bbe472014-11-30 15:33:35 -0500257
Tony Mackcd8f8752014-12-26 12:15:42 -0500258 def __unicode__(self): return u'%s %s %s' % (self.name, self.backend_type, self.version)
Tony Mackf3bbe472014-11-30 15:33:35 -0500259
Tony Macka7dbd422015-01-05 22:48:11 -0500260class SiteDeployment(PlCoreBase):
Tony Mackf3bbe472014-11-30 15:33:35 -0500261 objects = ControllerLinkManager()
Tony Mack528d4222014-12-05 17:13:08 -0500262 deleted_objects = ControllerLinkDeletionManager()
Tony Mackf3bbe472014-11-30 15:33:35 -0500263
Tony Mack3c01ff92015-01-10 23:08:10 -0500264 site = models.ForeignKey(Site,related_name='sitedeployments')
265 deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
266 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='sitedeployments')
Tony Mack528d4222014-12-05 17:13:08 -0500267 availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Tony Mack528d4222014-12-05 17:13:08 -0500268
Tony Mack7f542c62015-01-03 17:26:06 -0500269 def __unicode__(self): return u'%s %s' % (self.deployment, self.site)
Tony Macka7dbd422015-01-05 22:48:11 -0500270
271class ControllerSite(PlCoreBase):
272
273 site = models.ForeignKey(Site,related_name='controllersite')
274 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='controllersite')
275 tenant_id = models.CharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone tenant id")