Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
| 3 | from plstackapi.core.models import PlCoreBase |
| 4 | from plstackapi.core.models import DeploymentNetwork |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 5 | from plstackapi.openstack.driver import OpenStackDriver |
| 6 | |
| 7 | # Create your models here. |
| 8 | |
| 9 | |
| 10 | class Site(PlCoreBase): |
| 11 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 12 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 13 | name = models.CharField(max_length=200, help_text="Name for this Site") |
| 14 | site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page") |
| 15 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 16 | longitude = models.FloatField(null=True, blank=True) |
| 17 | latitude = models.FloatField(null=True, blank=True) |
| 18 | login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site") |
| 19 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 20 | abbreviated_name = models.CharField(max_length=80) |
| 21 | |
| 22 | deployments = models.ManyToManyField(DeploymentNetwork, blank=True, related_name='sites') |
| 23 | |
| 24 | def __unicode__(self): return u'%s' % (self.name) |
| 25 | |
Tony Mack | d9b4607 | 2013-04-14 13:51:19 -0400 | [diff] [blame] | 26 | def save(self, *args, **kwds): |
| 27 | driver = OpenStackDriver() |
| 28 | if not self.tenant_id: |
| 29 | tenant = driver.create_tenant(tenant_name=self.login_base, |
| 30 | description=self.name, |
| 31 | enabled=self.enabled) |
| 32 | self.tenant_id = tenant.id |
| 33 | # update the record |
| 34 | if self.id: |
| 35 | driver.update_tenant(self.tenant_id, |
| 36 | name=self.login_base, |
| 37 | description=self.name, |
| 38 | enabled=self.enabled) |
| 39 | |
| 40 | super(Site, self).save(*args, **kwds) |
| 41 | |
| 42 | |
| 43 | def delete(self, *args, **kwds): |
| 44 | driver = OpenStackDriver() |
| 45 | if self.tenant_id: |
| 46 | driver.delete_tenant(self.tenant_id) |
| 47 | super(Site, self).delete(*args, **kwds) |
| 48 | |
| 49 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 50 | class SitePrivilege(PlCoreBase): |
| 51 | |
| 52 | user = models.ForeignKey('User', related_name='site_privileges') |
| 53 | site = models.ForeignKey('Site', related_name='site_privileges') |
| 54 | role = models.ForeignKey('Role') |
| 55 | |
| 56 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 57 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 58 | |