Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Sapan Bhatia | 4a62a2f | 2014-09-22 18:47:53 -0400 | [diff] [blame^] | 3 | from django.db.models import Q |
Sapan Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 4 | from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 5 | from core.models import Tag |
| 6 | from django.contrib.contenttypes import generic |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 7 | from geoposition.fields import GeopositionField |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 8 | from core.acl import AccessControlList |
Sapan Bhatia | 1d1b2b1 | 2014-09-22 17:25:06 -0400 | [diff] [blame] | 9 | from planetstack.config import Config |
| 10 | |
| 11 | config = Config() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 12 | |
Sapan Bhatia | c4b9807 | 2014-09-19 16:48:36 -0400 | [diff] [blame] | 13 | class 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 Bhatia | 15fdcdb | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 26 | class 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 Bhatia | 48df09f | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 38 | class 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 Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 51 | class 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 63 | class Site(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 64 | """ |
| 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 67 | 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 Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 70 | location = GeopositionField() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 71 | 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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 77 | #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') |
Sapan Bhatia | 378baea | 2014-06-13 13:37:46 -0400 | [diff] [blame] | 78 | 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 Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 79 | tags = generic.GenericRelation(Tag) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 80 | |
| 81 | def __unicode__(self): return u'%s' % (self.name) |
| 82 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 83 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 84 | if user.is_readonly: |
| 85 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 86 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 90 | if site_priv.role.role == 'pi': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 91 | return True |
| 92 | return False |
| 93 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 94 | @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 Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 105 | class SiteRole(PlCoreBase): |
| 106 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 107 | ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 108 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 109 | |
| 110 | def __unicode__(self): return u'%s' % (self.role) |
| 111 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 112 | class SitePrivilege(PlCoreBase): |
| 113 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 114 | user = models.ForeignKey('User', related_name='site_privileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 115 | site = models.ForeignKey('Site', related_name='site_privileges') |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 116 | role = models.ForeignKey('SiteRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 117 | |
| 118 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 119 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 120 | def save(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 121 | super(SitePrivilege, self).save(*args, **kwds) |
| 122 | |
| 123 | def delete(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 124 | super(SitePrivilege, self).delete(*args, **kwds) |
| 125 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 126 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 127 | return self.site.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 128 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 129 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 138 | class Deployment(PlCoreBase): |
Sapan Bhatia | abfdb80 | 2014-09-22 14:54:10 -0400 | [diff] [blame] | 139 | objects = DeploymentManager() |
| 140 | deleted_objects = DeploymentDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 141 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment") |
Scott Baker | 57ec5d3 | 2014-06-06 14:56:20 -0700 | [diff] [blame] | 142 | 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 Bhatia | ed9cd4f | 2014-09-18 00:13:46 -0400 | [diff] [blame] | 146 | backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version") |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 147 | |
| 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 Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 176 | @staticmethod |
| 177 | def select_by_acl(user): |
| 178 | ids = [] |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 179 | for deployment in Deployment.objects.all(): |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 180 | acl = deployment.get_acl() |
Scott Baker | 01a4cd0 | 2014-06-09 13:12:40 -0700 | [diff] [blame] | 181 | if acl.test(user) == "allow": |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 182 | ids.append(deployment.id) |
| 183 | |
| 184 | return Deployment.objects.filter(id__in=ids) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 185 | |
| 186 | def __unicode__(self): return u'%s' % (self.name) |
| 187 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 188 | @staticmethod |
| 189 | def select_by_user(user): |
| 190 | return Deployment.objects.all() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 191 | |
| 192 | class DeploymentRole(PlCoreBase): |
Sapan Bhatia | 9d21dc7 | 2014-09-22 14:54:26 -0400 | [diff] [blame] | 193 | objects = DeploymentLinkManager() |
| 194 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 195 | |
| 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 | |
| 201 | class DeploymentPrivilege(PlCoreBase): |
Sapan Bhatia | 16778cd | 2014-09-22 14:54:39 -0400 | [diff] [blame] | 202 | objects = DeploymentLinkManager() |
| 203 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 204 | |
| 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 211 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 218 | if dpriv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 219 | return True |
| 220 | return False |
| 221 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 222 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 231 | class SiteDeployments(PlCoreBase): |
Sapan Bhatia | 51ddecd | 2014-09-22 14:54:56 -0400 | [diff] [blame] | 232 | objects = DeploymentLinkManager() |
| 233 | deleted_objects = DeploymentLinkDeletionManager() |
| 234 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 235 | site = models.ForeignKey(Site) |
| 236 | deployment = models.ForeignKey(Deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 237 | 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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 242 | |
Tony Mack | 929af70 | 2014-02-04 19:36:52 -0500 | [diff] [blame] | 243 | #class Meta: |
| 244 | # db_table = 'core_site_deployments' |
| 245 | # #auto_created = Site |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 246 | |