blob: 04d10737c47305bbf8da4d7a02b7da5defe67007 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -04003from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
Siobhan Tullyde5450d2013-06-21 11:35:33 -04004from core.models import Tag
5from django.contrib.contenttypes import generic
Siobhan Tully567e3e62013-06-21 18:03:16 -04006from geoposition.fields import GeopositionField
Scott Baker5380c522014-06-06 14:49:43 -07007from core.acl import AccessControlList
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008
Sapan Bhatiac4b98072014-09-19 16:48:36 -04009class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
10 def get_queryset(self):
11 parent=super(DeploymentLinkDeletionManager, self)
12 if hasattr(parent, "get_queryset"):
13 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
14 else:
15 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
16
17 # deprecated in django 1.7 in favor of get_queryset().
18 def get_query_set(self):
19 return self.get_queryset()
20
21
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040022class DeploymentDeletionManager(PlCoreBaseDeletionManager):
23 def get_queryset(self):
24 parent=super(DeploymentDeletionManager, self)
25 if hasattr(parent, "get_queryset"):
26 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
27 else:
28 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
29
30 # deprecated in django 1.7 in favor of get_queryset().
31 def get_query_set(self):
32 return self.get_queryset()
33
Sapan Bhatia48df09f2014-09-19 16:47:40 -040034class DeploymentLinkManager(PlCoreBaseManager):
35 def get_queryset(self):
36 parent=super(DeploymentLinkManager, self)
37 if hasattr(parent, "get_queryset"):
38 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
39 else:
40 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
41
42 # deprecated in django 1.7 in favor of get_queryset().
43 def get_query_set(self):
44 return self.get_queryset()
45
46
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040047class DeploymentManager(PlCoreBaseManager):
48 def get_queryset(self):
49 parent=super(DeploymentManager, self)
50 if hasattr(parent, "get_queryset"):
51 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
52 else:
53 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
54
55 # deprecated in django 1.7 in favor of get_queryset().
56 def get_query_set(self):
57 return self.get_queryset()
58
Siobhan Tully4bc09f22013-04-10 21:15:21 -040059class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050060 """
61 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.
62 """
Siobhan Tully4bc09f22013-04-10 21:15:21 -040063 name = models.CharField(max_length=200, help_text="Name for this Site")
64 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
65 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -040066 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040067 longitude = models.FloatField(null=True, blank=True)
68 latitude = models.FloatField(null=True, blank=True)
69 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
70 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
71 abbreviated_name = models.CharField(max_length=80)
72
Tony Macke4be32f2014-03-11 20:45:25 -040073 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Sapan Bhatia378baea2014-06-13 13:37:46 -040074 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 -040075 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040076
77 def __unicode__(self): return u'%s' % (self.name)
78
Tony Mack5b061472014-02-04 07:57:10 -050079 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050080 if user.is_readonly:
81 return False
Tony Mack5b061472014-02-04 07:57:10 -050082 if user.is_admin:
83 return True
84 site_privs = SitePrivilege.objects.filter(user=user, site=self)
85 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050086 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -050087 return True
88 return False
89
Tony Mack5b061472014-02-04 07:57:10 -050090 @staticmethod
91 def select_by_user(user):
92 if user.is_admin:
93 qs = Site.objects.all()
94 else:
95 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
96 site_ids.append(user.site.id)
97 qs = Site.objects.filter(id__in=site_ids)
98 return qs
99
100
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400101class SiteRole(PlCoreBase):
102
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500103 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400104 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
105
106 def __unicode__(self): return u'%s' % (self.role)
107
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400108class SitePrivilege(PlCoreBase):
109
Siobhan Tully30fd4292013-05-10 08:59:56 -0400110 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400111 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400112 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400113
114 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
115
Tony Mack00d361f2013-04-28 10:28:42 -0400116 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400117 super(SitePrivilege, self).save(*args, **kwds)
118
119 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400120 super(SitePrivilege, self).delete(*args, **kwds)
121
Tony Mack5b061472014-02-04 07:57:10 -0500122 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500123 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500124
Tony Mack5b061472014-02-04 07:57:10 -0500125 @staticmethod
126 def select_by_user(user):
127 if user.is_admin:
128 qs = SitePrivilege.objects.all()
129 else:
130 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
131 qs = SitePrivilege.objects.filter(id__in=sp_ids)
132 return qs
133
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500134class Deployment(PlCoreBase):
Sapan Bhatiaabfdb802014-09-22 14:54:10 -0400135 objects = DeploymentManager()
136 deleted_objects = DeploymentDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500137 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Scott Baker57ec5d32014-06-06 14:56:20 -0700138 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
139 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
140 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
141 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
Sapan Bhatiaed9cd4f2014-09-18 00:13:46 -0400142 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 -0700143
144 # smbaker: the default of 'allow all' is intended for evolutions of existing
145 # deployments. When new deployments are created via the GUI, they are
146 # given a default of 'allow site <site_of_creator>'
147 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
148 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
149
150 def get_acl(self):
151 return AccessControlList(self.accessControl)
152
153 def test_acl(self, slice=None, user=None):
154 potential_users=[]
155
156 if user:
157 potential_users.append(user)
158
159 if slice:
160 potential_users.append(slice.creator)
161 for priv in slice.slice_privileges.all():
162 if priv.user not in potential_users:
163 potential_users.append(priv.user)
164
165 acl = self.get_acl()
166 for user in potential_users:
167 if acl.test(user) == "allow":
168 return True
169
170 return False
171
Scott Bakercb95fde2014-06-06 16:09:51 -0700172 @staticmethod
173 def select_by_acl(user):
174 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700175 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700176 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700177 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700178 ids.append(deployment.id)
179
180 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500181
182 def __unicode__(self): return u'%s' % (self.name)
183
Tony Macke4be32f2014-03-11 20:45:25 -0400184 @staticmethod
185 def select_by_user(user):
186 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500187
188class DeploymentRole(PlCoreBase):
Sapan Bhatia9d21dc72014-09-22 14:54:26 -0400189 objects = DeploymentLinkManager()
190 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500191
192 ROLE_CHOICES = (('admin','Admin'),)
193 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
194
195 def __unicode__(self): return u'%s' % (self.role)
196
197class DeploymentPrivilege(PlCoreBase):
Sapan Bhatia16778cd2014-09-22 14:54:39 -0400198 objects = DeploymentLinkManager()
199 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500200
201 user = models.ForeignKey('User', related_name='deployment_privileges')
202 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
203 role = models.ForeignKey('DeploymentRole')
204
205 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
206
Tony Mack5b061472014-02-04 07:57:10 -0500207 def can_update(self, user):
208 if user.is_readonly:
209 return False
210 if user.is_admin:
211 return True
212 dprivs = DeploymentPrivilege.objects.filter(user=user)
213 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500214 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500215 return True
216 return False
217
Tony Mack5b061472014-02-04 07:57:10 -0500218 @staticmethod
219 def select_by_user(user):
220 if user.is_admin:
221 qs = DeploymentPrivilege.objects.all()
222 else:
223 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
224 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
225 return qs
226
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500227class SiteDeployments(PlCoreBase):
Sapan Bhatia51ddecd2014-09-22 14:54:56 -0400228 objects = DeploymentLinkManager()
229 deleted_objects = DeploymentLinkDeletionManager()
230
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500231 site = models.ForeignKey(Site)
232 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400233 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
234
235 @staticmethod
236 def select_by_user(user):
237 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500238
Tony Mack929af702014-02-04 19:36:52 -0500239 #class Meta:
240 # db_table = 'core_site_deployments'
241 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400242