From 63d62f084e3e54d7831c728ca40de4ce94004315 Mon Sep 17 00:00:00 2001 From: amcrn Date: Fri, 14 Mar 2014 13:22:02 -0700 Subject: [PATCH] Hide Datastores In List With No Active Versions in a datastore-list operation, for non-admin, filters out datastores that either have no versions or have no active versions for non-admin users. admins continue to see all datastores, active or inactive, in a datastore-list operation. Change-Id: Iccaf9ae59469fd0f4cb346ee258d3132eed83660 Co-Authored-By: Andrey Shestakov Closes-Bug: #1290617 --- run_tests.py | 2 +- trove/datastore/models.py | 10 +++++++--- trove/datastore/service.py | 6 +++++- trove/db/__init__.py | 3 +++ trove/db/sqlalchemy/api.py | 4 ++++ trove/tests/api/datastores.py | 18 ++++++++++++++++++ trove/tests/config.py | 2 ++ 7 files changed, 40 insertions(+), 5 deletions(-) diff --git a/run_tests.py b/run_tests.py index 100f0bf228..7ed9d4bdc4 100644 --- a/run_tests.py +++ b/run_tests.py @@ -78,7 +78,7 @@ def datastore_init(): name=CONFIG.dbaas_datastore, default_version_id= CONFIG.dbaas_datastore_version_id) - models.DBDatastore.create(id="e00000e0-00e0-0e00-00e0-000e000000ee", + models.DBDatastore.create(id=CONFIG.dbaas_datastore_id_no_versions, name='Test_Datastore_1', default_version_id=None) diff --git a/trove/datastore/models.py b/trove/datastore/models.py index 5cf9c029fc..93b0cfee5c 100644 --- a/trove/datastore/models.py +++ b/trove/datastore/models.py @@ -81,8 +81,12 @@ class Datastores(object): self.db_info = db_info @classmethod - def load(cls): - return cls(DBDatastore.find_all()) + def load(cls, only_active=True): + datastores = DBDatastore.find_all() + if only_active: + datastores = datastores.join(DBDatastoreVersion).filter( + DBDatastoreVersion.active == 1) + return cls(datastores) def __iter__(self): for item in self.db_info: @@ -137,7 +141,7 @@ class DatastoreVersion(object): @property def active(self): - return self.db_info.active + return (True if self.db_info.active else False) @property def manager(self): diff --git a/trove/datastore/service.py b/trove/datastore/service.py index d2f0c0b314..d9acb1f8fa 100644 --- a/trove/datastore/service.py +++ b/trove/datastore/service.py @@ -28,7 +28,11 @@ class DatastoreController(wsgi.Controller): DatastoreView(datastore, req).data(), 200) def index(self, req, tenant_id): - datastores = models.Datastores.load() + context = req.environ[wsgi.CONTEXT_KEY] + only_active = True + if context.is_admin: + only_active = False + datastores = models.Datastores.load(only_active) return wsgi.Result(views. DatastoresView(datastores, req).data(), 200) diff --git a/trove/db/__init__.py b/trove/db/__init__.py index 0901d90a64..1f3698e1fb 100644 --- a/trove/db/__init__.py +++ b/trove/db/__init__.py @@ -53,6 +53,9 @@ class Query(object): return self.db_api.first(self._query_func, self._model, **self._conditions) + def join(self, *args): + return self.db_api.join(self._query_func, self._model, *args) + def __iter__(self): return iter(self.all()) diff --git a/trove/db/sqlalchemy/api.py b/trove/db/sqlalchemy/api.py index afe03b02ce..4653f9ae20 100644 --- a/trove/db/sqlalchemy/api.py +++ b/trove/db/sqlalchemy/api.py @@ -32,6 +32,10 @@ def first(query, *args, **kwargs): return query(*args, **kwargs).first() +def join(query, model, *args): + return query(model).join(*args) + + def find_all(model, **conditions): return _query_by(model, **conditions) diff --git a/trove/tests/api/datastores.py b/trove/tests/api/datastores.py index 4475d3f233..91209fba8c 100644 --- a/trove/tests/api/datastores.py +++ b/trove/tests/api/datastores.py @@ -20,6 +20,7 @@ from troveclient.compat import exceptions from proboscis import before_class from proboscis import test from proboscis.asserts import assert_raises +from proboscis.asserts import assert_true from trove import tests from trove.tests.util import create_dbaas_client @@ -39,7 +40,10 @@ class Datastores(object): def setUp(self): rd_user = test_config.users.find_user( Requirements(is_admin=False, services=["trove"])) + rd_admin = test_config.users.find_user( + Requirements(is_admin=True, services=["trove"])) self.rd_client = create_dbaas_client(rd_user) + self.rd_admin = create_dbaas_client(rd_admin) @test def test_datastore_list_attrs(self): @@ -152,3 +156,17 @@ class Datastores(object): assert_equal(e.message, "Datastore version '%s' cannot be found." % test_config.dbaas_datastore_version) + + @test + def test_datastore_with_no_active_versions_is_hidden(self): + datastores = self.rd_client.datastores.list() + id_list = [datastore.id for datastore in datastores] + id_no_versions = test_config.dbaas_datastore_id_no_versions + assert_true(id_no_versions not in id_list) + + @test + def test_datastore_with_no_active_versions_is_visible_for_admin(self): + datastores = self.rd_admin.datastores.list() + id_list = [datastore.id for datastore in datastores] + id_no_versions = test_config.dbaas_datastore_id_no_versions + assert_true(id_no_versions in id_list) diff --git a/trove/tests/config.py b/trove/tests/config.py index 9fa4e1b00e..d31e7c2a0f 100644 --- a/trove/tests/config.py +++ b/trove/tests/config.py @@ -72,6 +72,8 @@ class TestConfig(object): 'nova_url': "http://localhost:8774/v1.1", 'dbaas_datastore': "mysql", 'dbaas_datastore_id': "a00000a0-00a0-0a00-00a0-000a000000aa", + 'dbaas_datastore_id_no_versions': "e00000e0-00e0-0e00-00e0-" + "000e000000ee", 'dbaas_datastore_version': "mysql-5.5", 'dbaas_datastore_version_id': "b00000b0-00b0-0b00-00b0-" "000b000000bb",