Multi-tenancy support has been implemented

All object are now belongs to the specific tenants.

Implements blueprint multi-tenant-support

Change-Id: I0ec0a842ebc1f9b10e251cbad58b6a1c9b375499
This commit is contained in:
Sergey Lukjanov 2013-06-28 19:08:28 +04:00
parent 454a83f04a
commit 15f840bf28
6 changed files with 83 additions and 41 deletions

View File

@ -77,9 +77,14 @@ def set_ctx(new_ctx):
_CTXS._curr_ctxs[corolocal.get_ident()] = new_ctx
def model_query(model, context=None):
def model_query(model, context=None, project_only=None):
context = context or ctx()
return context.session.query(model)
query = context.session.query(model)
if project_only:
query = query.filter_by(tenant_id=context.tenant_id)
return query
def model_save(model, context=None):

View File

@ -17,18 +17,22 @@ from savanna import context as ctx
import savanna.db.models as m
def _model_filter_by(model, **kwargs):
return ctx.model_query(model, project_only=True).filter_by(**kwargs)
## Cluster ops
# TODO(slukjanov): check tenant_id and etc.
def get_clusters(**args):
return ctx.model_query(m.Cluster).filter_by(**args).all()
return _model_filter_by(m.Cluster, **args).all()
def get_cluster(**args):
return ctx.model_query(m.Cluster).filter_by(**args).first()
return _model_filter_by(m.Cluster, **args).first()
def create_cluster(values):
values = values.copy()
session = ctx.current().session
with session.begin():
values['tenant_id'] = ctx.current().tenant_id
@ -66,14 +70,15 @@ def terminate_cluster(cluster):
## ClusterTemplate ops
def get_cluster_templates(**args):
return ctx.model_query(m.ClusterTemplate).filter_by(**args).all()
return _model_filter_by(m.ClusterTemplate, **args).all()
def get_cluster_template(**args):
return ctx.model_query(m.ClusterTemplate).filter_by(**args).first()
return _model_filter_by(m.ClusterTemplate, **args).first()
def create_cluster_template(values):
values = values.copy()
session = ctx.current().session
with session.begin():
values['tenant_id'] = ctx.current().tenant_id
@ -110,14 +115,15 @@ def terminate_cluster_template(**args):
## NodeGroupTemplate ops
def get_node_group_templates(**args):
return ctx.model_query(m.NodeGroupTemplate).filter_by(**args).all()
return _model_filter_by(m.NodeGroupTemplate, **args).all()
def get_node_group_template(**args):
return ctx.model_query(m.NodeGroupTemplate).filter_by(**args).first()
return _model_filter_by(m.NodeGroupTemplate, **args).first()
def create_node_group_template(values):
values = values.copy()
session = ctx.current().session
with session.begin():
values['tenant_id'] = ctx.current().tenant_id

View File

@ -26,9 +26,13 @@ from savanna.openstack.common import uuidutils
class DbTestCase(unittest2.TestCase):
def setUp(self):
def set_tenant(self, tenant_id="tenant_1"):
context.set_ctx(
context.Context('test_user', 'test_tenant', 'test_auth_token', {}))
context.Context('test_user', tenant_id, 'test_auth_token', {}))
def setUp(self):
self.set_tenant()
self.db_fd, self.db_path = tempfile.mkstemp()
session.set_defaults('sqlite:///' + self.db_path, self.db_path)
db_api.configure_db()

View File

@ -0,0 +1,57 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from savanna.db import storage as s
from savanna.openstack.common import uuidutils
from savanna.tests.unit import base
def _create_clusters(name="cluster-1", plugin_name="some_plugin",
hadoop_version="1.2.3", **kwargs):
cluster_dict = {
"name": name,
"plugin_name": plugin_name,
"hadoop_version": hadoop_version,
}
cluster_dict.update(kwargs)
return cluster_dict, s.create_cluster(cluster_dict)
class ClusterStorageTest(base.DbTestCase):
def test_create_cluster_trivial(self):
cluster_dict, cluster = _create_clusters()
self.assertIsNotNone(cluster)
self.assertTrue(uuidutils.is_uuid_like(cluster.id))
self.assertDictContainsSubset(cluster_dict, cluster.dict)
def test_clusters_multi_tenancy(self):
self.assertEqual(0, len(s.get_clusters()))
self.set_tenant("t-1")
self.assertEqual(0, len(s.get_clusters()))
_create_clusters("c-1")
_create_clusters("c-2")
self.assertEqual(2, len(s.get_clusters()))
self.set_tenant("t-2")
self.assertEqual(0, len(s.get_clusters()))
_create_clusters("c-1")
_create_clusters("c-2")
self.assertEqual(2, len(s.get_clusters()))
_create_clusters("c-3")
self.assertEqual(3, len(s.get_clusters()))

View File

@ -1,30 +0,0 @@
# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from savanna.tests.unit import base
class ClusterStorageTest(base.DbTestCase):
def test_get_cluster(self):
pass
def test_get_clusters(self):
pass
def test_create_cluster(self):
pass
def test_terminate_cluster(self):
pass