blob: 6fe40a6987c476dc7a54cc96332d71f34f6d0f52 [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
Sapan Bhatiac4b98072014-09-19 16:48:36 -040013class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
14 def get_queryset(self):
15 parent=super(DeploymentLinkDeletionManager, self)
16 if hasattr(parent, "get_queryset"):
17 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
18 else:
19 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
20
21 # deprecated in django 1.7 in favor of get_queryset().
22 def get_query_set(self):
23 return self.get_queryset()
24
25
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040026class DeploymentDeletionManager(PlCoreBaseDeletionManager):
27 def get_queryset(self):
28 parent=super(DeploymentDeletionManager, self)
29 if hasattr(parent, "get_queryset"):
30 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
31 else:
32 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
33
34 # deprecated in django 1.7 in favor of get_queryset().
35 def get_query_set(self):
36 return self.get_queryset()
37
Sapan Bhatia48df09f2014-09-19 16:47:40 -040038class DeploymentLinkManager(PlCoreBaseManager):
39 def get_queryset(self):
40 parent=super(DeploymentLinkManager, self)
41 if hasattr(parent, "get_queryset"):
42 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
43 else:
44 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
45
46 # deprecated in django 1.7 in favor of get_queryset().
47 def get_query_set(self):
48 return self.get_queryset()
49
50
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040051class DeploymentManager(PlCoreBaseManager):
52 def get_queryset(self):
53 parent=super(DeploymentManager, self)
54 if hasattr(parent, "get_queryset"):
55 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
56 else:
57 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
58
59 # deprecated in django 1.7 in favor of get_queryset().
60 def get_query_set(self):
61 return self.get_queryset()
62
Siobhan Tully4bc09f22013-04-10 21:15:21 -040063class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050064 """
65 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.
66 """
Siobhan Tully4bc09f22013-04-10 21:15:21 -040067 name = models.CharField(max_length=200, help_text="Name for this Site")
68 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
69 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -040070 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040071 longitude = models.FloatField(null=True, blank=True)
72 latitude = models.FloatField(null=True, blank=True)
73 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
74 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
75 abbreviated_name = models.CharField(max_length=80)
76
Tony Macke4be32f2014-03-11 20:45:25 -040077 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Sapan Bhatia378baea2014-06-13 13:37:46 -040078 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 -040079 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040080
81 def __unicode__(self): return u'%s' % (self.name)
82
Tony Mack5b061472014-02-04 07:57:10 -050083 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050084 if user.is_readonly:
85 return False
Tony Mack5b061472014-02-04 07:57:10 -050086 if user.is_admin:
87 return True
88 site_privs = SitePrivilege.objects.filter(user=user, site=self)
89 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050090 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -050091 return True
92 return False
93
Tony Mack5b061472014-02-04 07:57:10 -050094 @staticmethod
95 def select_by_user(user):
96 if user.is_admin:
97 qs = Site.objects.all()
98 else:
99 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
100 site_ids.append(user.site.id)
101 qs = Site.objects.filter(id__in=site_ids)
102 return qs
103
104
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400105class SiteRole(PlCoreBase):
106
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500107 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400108 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
109
110 def __unicode__(self): return u'%s' % (self.role)
111
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400112class SitePrivilege(PlCoreBase):
113
Siobhan Tully30fd4292013-05-10 08:59:56 -0400114 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400115 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400116 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400117
118 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
119
Tony Mack00d361f2013-04-28 10:28:42 -0400120 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400121 super(SitePrivilege, self).save(*args, **kwds)
122
123 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400124 super(SitePrivilege, self).delete(*args, **kwds)
125
Tony Mack5b061472014-02-04 07:57:10 -0500126 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500127 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500128
Tony Mack5b061472014-02-04 07:57:10 -0500129 @staticmethod
130 def select_by_user(user):
131 if user.is_admin:
132 qs = SitePrivilege.objects.all()
133 else:
134 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
135 qs = SitePrivilege.objects.filter(id__in=sp_ids)
136 return qs
137
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500138class Deployment(PlCoreBase):
Sapan Bhatiaabfdb802014-09-22 14:54:10 -0400139 objects = DeploymentManager()
140 deleted_objects = DeploymentDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500141 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Scott Baker57ec5d32014-06-06 14:56:20 -0700142 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
143 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
144 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
145 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 -0400146 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 -0700147
148 # smbaker: the default of 'allow all' is intended for evolutions of existing
149 # deployments. When new deployments are created via the GUI, they are
150 # given a default of 'allow site <site_of_creator>'
151 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
152 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
153
154 def get_acl(self):
155 return AccessControlList(self.accessControl)
156
157 def test_acl(self, slice=None, user=None):
158 potential_users=[]
159
160 if user:
161 potential_users.append(user)
162
163 if slice:
164 potential_users.append(slice.creator)
165 for priv in slice.slice_privileges.all():
166 if priv.user not in potential_users:
167 potential_users.append(priv.user)
168
169 acl = self.get_acl()
170 for user in potential_users:
171 if acl.test(user) == "allow":
172 return True
173
174 return False
175
Scott Bakercb95fde2014-06-06 16:09:51 -0700176 @staticmethod
177 def select_by_acl(user):
178 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700179 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700180 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700181 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700182 ids.append(deployment.id)
183
184 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500185
186 def __unicode__(self): return u'%s' % (self.name)
187
Tony Macke4be32f2014-03-11 20:45:25 -0400188 @staticmethod
189 def select_by_user(user):
190 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500191
192class DeploymentRole(PlCoreBase):
Sapan Bhatia9d21dc72014-09-22 14:54:26 -0400193 objects = DeploymentLinkManager()
194 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500195
196 ROLE_CHOICES = (('admin','Admin'),)
197 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
198
199 def __unicode__(self): return u'%s' % (self.role)
200
201class DeploymentPrivilege(PlCoreBase):
Sapan Bhatia16778cd2014-09-22 14:54:39 -0400202 objects = DeploymentLinkManager()
203 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500204
205 user = models.ForeignKey('User', related_name='deployment_privileges')
206 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
207 role = models.ForeignKey('DeploymentRole')
208
209 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
210
Tony Mack5b061472014-02-04 07:57:10 -0500211 def can_update(self, user):
212 if user.is_readonly:
213 return False
214 if user.is_admin:
215 return True
216 dprivs = DeploymentPrivilege.objects.filter(user=user)
217 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500218 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500219 return True
220 return False
221
Tony Mack5b061472014-02-04 07:57:10 -0500222 @staticmethod
223 def select_by_user(user):
224 if user.is_admin:
225 qs = DeploymentPrivilege.objects.all()
226 else:
227 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
228 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
229 return qs
230
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500231class SiteDeployments(PlCoreBase):
Sapan Bhatia51ddecd2014-09-22 14:54:56 -0400232 objects = DeploymentLinkManager()
233 deleted_objects = DeploymentLinkDeletionManager()
234
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500235 site = models.ForeignKey(Site)
236 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400237 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
238
239 @staticmethod
240 def select_by_user(user):
241 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500242
Tony Mack929af702014-02-04 19:36:52 -0500243 #class Meta:
244 # db_table = 'core_site_deployments'
245 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400246