blob: ccd5d392b3236e69a30a9361b7ec2f4991bf2ba5 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatiac6680c12014-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 Bhatiac6680c12014-09-19 16:47:07 -04009class DeploymentManager(PlCoreBaseManager):
10 def get_queryset(self):
11 parent=super(DeploymentManager, self)
12 if hasattr(parent, "get_queryset"):
13 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
14 else:
15 return parent.get_queryset().filter(Q(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
Siobhan Tully4bc09f22013-04-10 21:15:21 -040021class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050022 """
23 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.
24 """
Siobhan Tully4bc09f22013-04-10 21:15:21 -040025 name = models.CharField(max_length=200, help_text="Name for this Site")
26 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
27 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -040028 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040029 longitude = models.FloatField(null=True, blank=True)
30 latitude = models.FloatField(null=True, blank=True)
31 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
32 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
33 abbreviated_name = models.CharField(max_length=80)
34
Tony Macke4be32f2014-03-11 20:45:25 -040035 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Sapan Bhatia378baea2014-06-13 13:37:46 -040036 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 -040037 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040038
39 def __unicode__(self): return u'%s' % (self.name)
40
Tony Mack5b061472014-02-04 07:57:10 -050041 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050042 if user.is_readonly:
43 return False
Tony Mack5b061472014-02-04 07:57:10 -050044 if user.is_admin:
45 return True
46 site_privs = SitePrivilege.objects.filter(user=user, site=self)
47 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050048 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -050049 return True
50 return False
51
Tony Mack5b061472014-02-04 07:57:10 -050052 @staticmethod
53 def select_by_user(user):
54 if user.is_admin:
55 qs = Site.objects.all()
56 else:
57 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
58 site_ids.append(user.site.id)
59 qs = Site.objects.filter(id__in=site_ids)
60 return qs
61
62
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040063class SiteRole(PlCoreBase):
64
Siobhan Tullycf04fb62014-01-11 11:25:57 -050065 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040066 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
67
68 def __unicode__(self): return u'%s' % (self.role)
69
Siobhan Tully4bc09f22013-04-10 21:15:21 -040070class SitePrivilege(PlCoreBase):
71
Siobhan Tully30fd4292013-05-10 08:59:56 -040072 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040073 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040074 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040075
76 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
77
Tony Mack00d361f2013-04-28 10:28:42 -040078 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040079 super(SitePrivilege, self).save(*args, **kwds)
80
81 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -040082 super(SitePrivilege, self).delete(*args, **kwds)
83
Tony Mack5b061472014-02-04 07:57:10 -050084 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050085 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -050086
Tony Mack5b061472014-02-04 07:57:10 -050087 @staticmethod
88 def select_by_user(user):
89 if user.is_admin:
90 qs = SitePrivilege.objects.all()
91 else:
92 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
93 qs = SitePrivilege.objects.filter(id__in=sp_ids)
94 return qs
95
Siobhan Tullycf04fb62014-01-11 11:25:57 -050096class Deployment(PlCoreBase):
97 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Scott Baker57ec5d32014-06-06 14:56:20 -070098 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
99 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
100 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
101 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 -0400102 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 -0700103
104 # smbaker: the default of 'allow all' is intended for evolutions of existing
105 # deployments. When new deployments are created via the GUI, they are
106 # given a default of 'allow site <site_of_creator>'
107 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
108 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
109
110 def get_acl(self):
111 return AccessControlList(self.accessControl)
112
113 def test_acl(self, slice=None, user=None):
114 potential_users=[]
115
116 if user:
117 potential_users.append(user)
118
119 if slice:
120 potential_users.append(slice.creator)
121 for priv in slice.slice_privileges.all():
122 if priv.user not in potential_users:
123 potential_users.append(priv.user)
124
125 acl = self.get_acl()
126 for user in potential_users:
127 if acl.test(user) == "allow":
128 return True
129
130 return False
131
Scott Bakercb95fde2014-06-06 16:09:51 -0700132 @staticmethod
133 def select_by_acl(user):
134 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700135 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700136 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700137 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700138 ids.append(deployment.id)
139
140 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500141
142 def __unicode__(self): return u'%s' % (self.name)
143
Tony Macke4be32f2014-03-11 20:45:25 -0400144 @staticmethod
145 def select_by_user(user):
146 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500147
148class DeploymentRole(PlCoreBase):
149
150 ROLE_CHOICES = (('admin','Admin'),)
151 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
152
153 def __unicode__(self): return u'%s' % (self.role)
154
155class DeploymentPrivilege(PlCoreBase):
156
157 user = models.ForeignKey('User', related_name='deployment_privileges')
158 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
159 role = models.ForeignKey('DeploymentRole')
160
161 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
162
Tony Mack5b061472014-02-04 07:57:10 -0500163 def can_update(self, user):
164 if user.is_readonly:
165 return False
166 if user.is_admin:
167 return True
168 dprivs = DeploymentPrivilege.objects.filter(user=user)
169 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500170 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500171 return True
172 return False
173
Tony Mack5b061472014-02-04 07:57:10 -0500174 @staticmethod
175 def select_by_user(user):
176 if user.is_admin:
177 qs = DeploymentPrivilege.objects.all()
178 else:
179 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
180 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
181 return qs
182
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500183class SiteDeployments(PlCoreBase):
184 site = models.ForeignKey(Site)
185 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400186 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
187
188 @staticmethod
189 def select_by_user(user):
190 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500191
Tony Mack929af702014-02-04 19:36:52 -0500192 #class Meta:
193 # db_table = 'core_site_deployments'
194 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400195