aboutsummaryrefslogtreecommitdiffstats
path: root/keystone-moon
diff options
context:
space:
mode:
authorasteroide <thomas.duval@orange.com>2015-09-23 22:21:52 +0200
committerasteroide <thomas.duval@orange.com>2015-09-23 22:21:52 +0200
commite35decd4e989773c96a9abb263257291bd51ae1e (patch)
treecd4053e377cd2fee0ecce54dd17011aa2f16a4cd /keystone-moon
parent4c39eb93dee5f48bff4c59aa9ec74cfa4027a297 (diff)
Automatically create tenant in Keystone if tenant name doesn't exist in Keystone.
Change-Id: I773c2508092b3aeb921a9f7a4133331ca04c1858
Diffstat (limited to 'keystone-moon')
-rw-r--r--keystone-moon/keystone/contrib/moon/controllers.py30
1 files changed, 28 insertions, 2 deletions
diff --git a/keystone-moon/keystone/contrib/moon/controllers.py b/keystone-moon/keystone/contrib/moon/controllers.py
index c938278c..c860ed6a 100644
--- a/keystone-moon/keystone/contrib/moon/controllers.py
+++ b/keystone-moon/keystone/contrib/moon/controllers.py
@@ -9,6 +9,7 @@ from keystone import config
from keystone.models import token_model
from keystone.contrib.moon.exception import *
from oslo_log import log
+from uuid import uuid4
CONF = config.CONF
LOG = log.getLogger(__name__)
@@ -59,11 +60,36 @@ class Tenants(controller.V3Controller):
user_id = self._get_user_id_from_token(context.get('token_id'))
return self.tenant_api.get_tenants_dict(user_id)
+ def __get_keystone_tenant_dict(self, tenant_id="", tenant_name="", tenant_description="", domain="default"):
+ tenants = self.resource_api.list_projects()
+ for tenant in tenants:
+ if tenant_id and tenant_id == tenant['id']:
+ return tenant
+ if tenant_name and tenant_name == tenant['name']:
+ return tenant
+ if not tenant_id:
+ tenant_id = uuid4().hex
+ if not tenant_name:
+ tenant_name = tenant_id
+ tenant = {
+ "id": tenant_id,
+ "name": tenant_name,
+ "description": tenant_description,
+ "enabled": True,
+ "domain_id": domain
+ }
+ keystone_tenant = self.resource_api.create_project(tenant["id"], tenant)
+ return keystone_tenant
+
@controller.protected()
def add_tenant(self, context, **kw):
user_id = self._get_user_id_from_token(context.get('token_id'))
- # Next line will raise an error if tenant doesn't exist
- k_tenant_dict = self.resource_api.get_project_by_name(kw.get('tenant_name'), "default")
+ k_tenant_dict = self.__get_keystone_tenant_dict(
+ tenant_name=kw.get('tenant_name'),
+ tenant_description=kw.get('tenant_description', kw.get('tenant_name')),
+ domain=kw.get('tenant_domain', "default"),
+
+ )
tenant_dict = dict()
tenant_dict['id'] = k_tenant_dict['id']
tenant_dict['name'] = kw.get('tenant_name', None)