SitePrivilege and SliceMembership linked to tenant roles
diff --git a/plstackapi/core/models.py b/plstackapi/core/models.py
index fd679df..e4e3408 100644
--- a/plstackapi/core/models.py
+++ b/plstackapi/core/models.py
@@ -12,6 +12,12 @@
class Meta:
abstract = True
+ def save(self):
+ if not self.id:
+ self.created = datetime.date.today()
+ self.updated = datetime.datetime.today()
+ super(PlCoreBase, self).save()
+
class Role(PlCoreBase):
ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User'))
@@ -62,17 +68,28 @@
firstname = models.CharField(help_text="person's given name", max_length=200)
lastname = models.CharField(help_text="person's surname", max_length=200)
email = models.EmailField(help_text="e-mail address")
- phone = models.CharField(help_text="phone number contact", max_length=100)
- user_url = models.URLField()
+ phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
+ user_url = models.URLField(null=True, blank=True)
+ is_admin = models.BooleanField(default=False)
site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too")
def __unicode__(self): return u'%s' % (self.email)
- def save(self):
+ def save(self, *args, **kwds):
+ driver = OpenStackDriver()
+ name = self.email[:self.email.find('@')]
+ fields = {'name': name, 'email': self.email, 'password': self.password,
+ 'enabled': self.enabled}
if not self.id:
- self.created = datetime.date.today()
- self.updated = datetime.datetime.today()
- super(User, self).save()
+ user = driver.create_user(**fields)
+ else:
+ driver.update_user(self.user_id, **fields)
+ super(User, self).save(*args, **kwds)
+
+ def delete(self, *args, **kwds):
+ driver = OpenStackDriver()
+ driver.delete_user(self.user_id)
+ super(User, self).delete(*args, **kwds)
class SitePrivilege(PlCoreBase):
@@ -82,11 +99,37 @@
def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
- def save(self):
- if not self.id:
- self.created = datetime.date.today()
- self.updated = datetime.datetime.today()
- super(SitePrivilege, self).save()
+ def save(self, *args, **kwds):
+ driver = OpenStackDriver()
+ driver.add_user_role(user_id=user.user_id,
+ tenant_id=site.tenant_id,
+ role_name=role.name)
+ super(SitePrivilege, self).save(*args, **kwds)
+
+ def delete(self, *args, **kwds):
+ driver = OpenStackDriver()
+ driver.delete_user_role(user_id=user.user_id,
+ tenant_id=site.tenant_id,
+ role_name=role.name)
+ super(SitePrivilege, self).delete(*args, **kwds)
+
+
+class DeploymentNetwork(PlCoreBase):
+ name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
+
+ def __unicode__(self): return u'%s' % (self.name)
+
+class SiteDeploymentNetwork(PlCoreBase):
+ class Meta:
+ unique_together = ['site', 'deploymentNetwork']
+
+ site = models.ForeignKey(Site, related_name='deploymentNetworks')
+ deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
+ name = models.CharField(default="Blah", max_length=100)
+
+
+ def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
+
class Slice(PlCoreBase):
tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
@@ -136,11 +179,19 @@
def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
- def save(self):
- if not self.id:
- self.created = datetime.date.today()
- self.updated = datetime.datetime.today()
- super(SliceMembership, self).save()
+ def save(self, *args, **kwds):
+ driver = OpenStackDriver()
+ driver.add_user_role(user_id=user.user_id,
+ tenant_id=slice.tenant_id,
+ role_name=role.name)
+ super(SliceMembership, self).save(*args, **kwds)
+
+ def delete(self, *args, **kwds):
+ driver = OpenStackDriver()
+ driver.delete_user_role(user_id=user.user_id,
+ tenant_id=slice.tenant_id,
+ role_name=role.name)
+ super(SliceMembership, self).delete(*args, **kwds)
class SubNet(PlCoreBase):
subnet_id = models.CharField(max_length=256, unique=True)
@@ -172,25 +223,6 @@
driver.delete_subnet(self.subnet_id)
super(SubNet, self).delete(*args, **kwargs)
-
-
-class DeploymentNetwork(PlCoreBase):
- name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
-
- def __unicode__(self): return u'%s' % (self.name)
-
-class SiteDeploymentNetwork(PlCoreBase):
- class Meta:
- unique_together = ['site', 'deploymentNetwork']
-
- site = models.ForeignKey(Site, related_name='deploymentNetworks')
- deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
- name = models.CharField(default="Blah", max_length=100)
-
-
- def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
-
-
class Node(PlCoreBase):
name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
diff --git a/plstackapi/openstack/driver.py b/plstackapi/openstack/driver.py
index bdb08a9..adea9f3 100644
--- a/plstackapi/openstack/driver.py
+++ b/plstackapi/openstack/driver.py
@@ -10,27 +10,53 @@
self.config = Config()
self.shell = OpenStackShell()
- def create_tenant(self, **kwds):
+ def create_tenant(self, tenant_name, enabled, description):
"""Create keystone tenant. Suggested fields: name, description, enabled"""
- required_fields = ['tenant_name', 'enabled', 'description']
- for field in required_fields:
- if field not in kwds:
- raise Exception, "Must specify %s" % field
-
- tenants = self.shell.keystone.tenants.findall(name=kwds['tenant_name'])
+ tenants = self.shell.keystone.tenants.findall(name=tenant_name)
if not tenants:
- tenant = self.shell.keystone.tenants.create(**kwds)
+ fields = {'tenant_name': tenant_name, 'enabled': enabled,
+ 'description', description}
+ tenant = self.shell.keystone.tenants.create(**fields)
else:
tenant = tenants[0]
return tenant
def update_tenant(self, id, **kwds):
- return self.shell.keystone.tenants.update(self.id, **kwds)
+ return self.shell.keystone.tenants.update(id, **kwds)
def delete_tenant(self, id):
tenant = self.shell.keystone.tenants.find(id=id)
return self.shell.keystone.tenants.delete(tenant)
+ def create_user(self, name, email, password, enabled):
+ users = self.shell.keystone.users.findall(email=email)
+ if not users:
+ fields = {'name': name, 'email': email, 'password': password,
+ 'enabled': enabled}
+ user = self.shell.keystone.create(**fields)
+ else:
+ user = users[0]
+ return user
+
+ def add_user_role(self, user_id, tenant_id, role_name):
+ user = self.shell.keystone.users.find(id=user_id)
+ tenant = self.shell.keystone.tenants.find(id=tenant_id)
+ role = self.shell.keystone.roles.find(role_name)
+ return tenant.add_user(user, role)
+
+ def delete_user_role(self, user_id, tenant_id, role_name):
+ user = self.shell.keystone.users.find(id=user_id)
+ tenant = self.shell.keystone.tenants.find(id=tenant_id)
+ role = self.shell.keystone.roles.find(role_name)
+ return tenant.delete_user(user, role)
+
+ def update_user(self, id, **kwds):
+ return self.shell.keystone.users.update(id, **kwds)
+
+ def delete_user(self, id):
+ user = self.shell.keystone.users.find(id=id)
+ return self.shell.keystone.users.delete(user)
+
def create_router(self, name):
router = self.shell.quantum.create_router(name=name)
# TODO: add router to external network