blob: af57ed24c87840ca63395b0722030d9a51e69d2d [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
Sapan Bhatia699a0442014-09-22 17:25:06 -04009from planetstack.config import Config
10
11config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012
Sapan Bhatia71058ec2014-09-19 16:48:36 -040013class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
14 def get_queryset(self):
15 parent=super(DeploymentLinkDeletionManager, 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):
23 return parent_queryset.filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
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
Sapan Bhatia005d7612014-09-19 16:48:11 -040032class DeploymentDeletionManager(PlCoreBaseDeletionManager):
33 def get_queryset(self):
34 parent=super(DeploymentDeletionManager, 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:
44 return parent_queryset.filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
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
Sapan Bhatiad8a6eb52014-09-19 16:47:40 -040052class DeploymentLinkManager(PlCoreBaseManager):
53 def get_queryset(self):
54 parent=super(DeploymentLinkManager, 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:
64 return parent_queryset.filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
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
Sapan Bhatiac6680c12014-09-19 16:47:07 -040073class DeploymentManager(PlCoreBaseManager):
74 def get_queryset(self):
75 parent=super(DeploymentManager, 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:
85 return parent_queryset.filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
Sapan Bhatiac6680c12014-09-19 16:47:07 -040086 else:
Sapan Bhatiab8cc1142014-09-23 13:14:31 -040087 return parent_queryset.filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
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')
Sapan Bhatia378baea2014-06-13 13:37:46 -0400108 deployments = models.ManyToManyField('Deployment', through='SiteDeployments', 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
Siobhan Tully30fd4292013-05-10 08:59:56 -0400144 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400145 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400146 role = models.ForeignKey('SiteRole')
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):
Sapan Bhatiac0754e72014-09-22 14:54:10 -0400169 objects = DeploymentManager()
170 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")
Scott Baker57ec5d32014-06-06 14:56:20 -0700172 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")
Sapan Bhatia369ed462014-09-18 00:13:46 -0400176 backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
Scott Baker5380c522014-06-06 14:49:43 -0700177
178 # smbaker: the default of 'allow all' is intended for evolutions of existing
179 # deployments. When new deployments are created via the GUI, they are
180 # given a default of 'allow site <site_of_creator>'
181 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
182 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
183
184 def get_acl(self):
185 return AccessControlList(self.accessControl)
186
187 def test_acl(self, slice=None, user=None):
188 potential_users=[]
189
190 if user:
191 potential_users.append(user)
192
193 if slice:
194 potential_users.append(slice.creator)
195 for priv in slice.slice_privileges.all():
196 if priv.user not in potential_users:
197 potential_users.append(priv.user)
198
199 acl = self.get_acl()
200 for user in potential_users:
201 if acl.test(user) == "allow":
202 return True
203
204 return False
205
Scott Bakercb95fde2014-06-06 16:09:51 -0700206 @staticmethod
207 def select_by_acl(user):
208 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700209 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700210 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700211 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700212 ids.append(deployment.id)
213
214 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500215
216 def __unicode__(self): return u'%s' % (self.name)
217
Tony Macke4be32f2014-03-11 20:45:25 -0400218 @staticmethod
219 def select_by_user(user):
220 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500221
222class DeploymentRole(PlCoreBase):
Sapan Bhatiad2a98542014-09-22 14:54:26 -0400223 objects = DeploymentLinkManager()
224 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500225
226 ROLE_CHOICES = (('admin','Admin'),)
227 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
228
229 def __unicode__(self): return u'%s' % (self.role)
230
231class DeploymentPrivilege(PlCoreBase):
Sapan Bhatiab80b6542014-09-22 14:54:39 -0400232 objects = DeploymentLinkManager()
233 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500234
235 user = models.ForeignKey('User', related_name='deployment_privileges')
236 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
237 role = models.ForeignKey('DeploymentRole')
238
239 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
240
Tony Mack5b061472014-02-04 07:57:10 -0500241 def can_update(self, user):
242 if user.is_readonly:
243 return False
244 if user.is_admin:
245 return True
246 dprivs = DeploymentPrivilege.objects.filter(user=user)
247 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500248 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500249 return True
250 return False
251
Tony Mack5b061472014-02-04 07:57:10 -0500252 @staticmethod
253 def select_by_user(user):
254 if user.is_admin:
255 qs = DeploymentPrivilege.objects.all()
256 else:
257 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
258 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
259 return qs
260
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500261class SiteDeployments(PlCoreBase):
Sapan Bhatia6fc6e2b2014-09-22 14:54:56 -0400262 objects = DeploymentLinkManager()
263 deleted_objects = DeploymentLinkDeletionManager()
264
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500265 site = models.ForeignKey(Site)
266 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400267 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
268
269 @staticmethod
270 def select_by_user(user):
271 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500272
Tony Mack929af702014-02-04 19:36:52 -0500273 #class Meta:
274 # db_table = 'core_site_deployments'
275 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400276