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) |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 16 | 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): |
Sapan Bhatia | 40e1813 | 2014-10-08 09:38:21 -0400 | [diff] [blame] | 23 | return parent_queryset.filter(Q(deployment__backend_type=backend_type)) |
Sapan Bhatia | c4b9807 | 2014-09-19 16:48:36 -0400 | [diff] [blame] | 24 | else: |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 25 | return parent_queryset |
Sapan Bhatia | c4b9807 | 2014-09-19 16:48:36 -0400 | [diff] [blame] | 26 | |
| 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 Bhatia | 15fdcdb | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 32 | class DeploymentDeletionManager(PlCoreBaseDeletionManager): |
| 33 | def get_queryset(self): |
| 34 | parent=super(DeploymentDeletionManager, self) |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 35 | |
| 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: |
Sapan Bhatia | 40e1813 | 2014-10-08 09:38:21 -0400 | [diff] [blame] | 44 | return parent_queryset.filter(Q(backend_type=backend_type)) |
Sapan Bhatia | 15fdcdb | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 45 | else: |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 46 | return parent_queryset |
Sapan Bhatia | 15fdcdb | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 47 | |
| 48 | # deprecated in django 1.7 in favor of get_queryset(). |
| 49 | def get_query_set(self): |
| 50 | return self.get_queryset() |
| 51 | |
Sapan Bhatia | 48df09f | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 52 | class DeploymentLinkManager(PlCoreBaseManager): |
| 53 | def get_queryset(self): |
| 54 | parent=super(DeploymentLinkManager, self) |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 55 | |
| 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: |
Sapan Bhatia | 40e1813 | 2014-10-08 09:38:21 -0400 | [diff] [blame] | 64 | return parent_queryset.filter(Q(deployment__backend_type=backend_type)) |
Sapan Bhatia | 48df09f | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 65 | else: |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 66 | return parent_queryset |
Sapan Bhatia | 48df09f | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 67 | |
| 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 Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 73 | class DeploymentManager(PlCoreBaseManager): |
| 74 | def get_queryset(self): |
| 75 | parent=super(DeploymentManager, self) |
Sapan Bhatia | d0a176c | 2014-09-23 13:14:31 -0400 | [diff] [blame] | 76 | |
| 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: |
Sapan Bhatia | 40e1813 | 2014-10-08 09:38:21 -0400 | [diff] [blame] | 85 | return parent_queryset.filter(Q(backend_type=backend_type)) |
Sapan Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 86 | else: |
Sapan Bhatia | f77d01a | 2014-09-24 00:34:44 -0400 | [diff] [blame] | 87 | return parent_queryset |
Sapan Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 88 | |
| 89 | # deprecated in django 1.7 in favor of get_queryset(). |
| 90 | def get_query_set(self): |
| 91 | return self.get_queryset() |
| 92 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 93 | class Site(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 94 | """ |
| 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 97 | 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 Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 100 | location = GeopositionField() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 101 | 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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 107 | #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 108 | 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] | 109 | tags = generic.GenericRelation(Tag) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 110 | |
| 111 | def __unicode__(self): return u'%s' % (self.name) |
| 112 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 113 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 114 | if user.is_readonly: |
| 115 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 116 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 120 | if site_priv.role.role == 'pi': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 121 | return True |
| 122 | return False |
| 123 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 124 | @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 Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 135 | class SiteRole(PlCoreBase): |
| 136 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 137 | ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 138 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 139 | |
| 140 | def __unicode__(self): return u'%s' % (self.role) |
| 141 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 142 | class SitePrivilege(PlCoreBase): |
| 143 | |
Sapan Bhatia | 6bfa2ca | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 144 | user = models.ForeignKey('User', related_name='siteprivileges') |
| 145 | site = models.ForeignKey('Site', related_name='siteprivileges') |
| 146 | role = models.ForeignKey('SiteRole',related_name='siteprivileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 147 | |
| 148 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 149 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 150 | def save(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 151 | super(SitePrivilege, self).save(*args, **kwds) |
| 152 | |
| 153 | def delete(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 154 | super(SitePrivilege, self).delete(*args, **kwds) |
| 155 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 156 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 157 | return self.site.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 158 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 159 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 168 | class Deployment(PlCoreBase): |
Sapan Bhatia | abfdb80 | 2014-09-22 14:54:10 -0400 | [diff] [blame] | 169 | objects = DeploymentManager() |
| 170 | deleted_objects = DeploymentDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 171 | 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] | 172 | 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 Bhatia | ed9cd4f | 2014-09-18 00:13:46 -0400 | [diff] [blame] | 176 | backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version") |
Tony Mack | e75841e | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 177 | availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone") |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 178 | |
| 179 | # smbaker: the default of 'allow all' is intended for evolutions of existing |
| 180 | # deployments. When new deployments are created via the GUI, they are |
| 181 | # given a default of 'allow site <site_of_creator>' |
| 182 | accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all", |
| 183 | help_text="Access control list that specifies which sites/users may use nodes in this deployment") |
| 184 | |
| 185 | def get_acl(self): |
| 186 | return AccessControlList(self.accessControl) |
| 187 | |
| 188 | def test_acl(self, slice=None, user=None): |
| 189 | potential_users=[] |
| 190 | |
| 191 | if user: |
| 192 | potential_users.append(user) |
| 193 | |
| 194 | if slice: |
| 195 | potential_users.append(slice.creator) |
Sapan Bhatia | d2d5dd8 | 2014-11-11 21:10:07 -0500 | [diff] [blame] | 196 | for priv in slice.sliceprivileges.all(): |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 197 | if priv.user not in potential_users: |
| 198 | potential_users.append(priv.user) |
| 199 | |
| 200 | acl = self.get_acl() |
| 201 | for user in potential_users: |
| 202 | if acl.test(user) == "allow": |
| 203 | return True |
| 204 | |
| 205 | return False |
| 206 | |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 207 | @staticmethod |
| 208 | def select_by_acl(user): |
| 209 | ids = [] |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 210 | for deployment in Deployment.objects.all(): |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 211 | acl = deployment.get_acl() |
Scott Baker | 01a4cd0 | 2014-06-09 13:12:40 -0700 | [diff] [blame] | 212 | if acl.test(user) == "allow": |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 213 | ids.append(deployment.id) |
| 214 | |
| 215 | return Deployment.objects.filter(id__in=ids) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 216 | |
| 217 | def __unicode__(self): return u'%s' % (self.name) |
| 218 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 219 | class DeploymentRole(PlCoreBase): |
Sapan Bhatia | 9024076 | 2014-09-23 13:42:40 -0400 | [diff] [blame] | 220 | #objects = DeploymentLinkManager() |
| 221 | #deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 222 | |
| 223 | ROLE_CHOICES = (('admin','Admin'),) |
| 224 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 225 | |
| 226 | def __unicode__(self): return u'%s' % (self.role) |
| 227 | |
| 228 | class DeploymentPrivilege(PlCoreBase): |
Sapan Bhatia | 16778cd | 2014-09-22 14:54:39 -0400 | [diff] [blame] | 229 | objects = DeploymentLinkManager() |
| 230 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 231 | |
Sapan Bhatia | 6bfa2ca | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 232 | user = models.ForeignKey('User', related_name='deploymentprivileges') |
| 233 | deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges') |
| 234 | role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges') |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 235 | |
| 236 | def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role) |
| 237 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 238 | def can_update(self, user): |
| 239 | if user.is_readonly: |
| 240 | return False |
| 241 | if user.is_admin: |
| 242 | return True |
| 243 | dprivs = DeploymentPrivilege.objects.filter(user=user) |
| 244 | for dpriv in dprivs: |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 245 | if dpriv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 246 | return True |
| 247 | return False |
| 248 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 249 | @staticmethod |
| 250 | def select_by_user(user): |
| 251 | if user.is_admin: |
| 252 | qs = DeploymentPrivilege.objects.all() |
| 253 | else: |
| 254 | dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)] |
| 255 | qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids) |
| 256 | return qs |
| 257 | |
Sapan Bhatia | b55f4a3 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 258 | class SiteDeployments(PlCoreBase): |
Sapan Bhatia | 51ddecd | 2014-09-22 14:54:56 -0400 | [diff] [blame] | 259 | objects = DeploymentLinkManager() |
| 260 | deleted_objects = DeploymentLinkDeletionManager() |
| 261 | |
Sapan Bhatia | bfe716a | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 262 | site = models.ForeignKey(Site,related_name='sitedeployments') |
| 263 | deployment = models.ForeignKey(Deployment,related_name='sitedeployments') |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 264 | tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id") |
| 265 | |
Tony Mack | 929af70 | 2014-02-04 19:36:52 -0500 | [diff] [blame] | 266 | #class Meta: |
| 267 | # db_table = 'core_site_deployments' |
| 268 | # #auto_created = Site |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 269 | |