From ac9b85e1f58f7686245802d79c7f66f57281ccb5 Mon Sep 17 00:00:00 2001 From: Andrew Pashkin Date: Fri, 22 May 2015 22:42:22 +0300 Subject: [PATCH] Add an option to filter packages by 'id' in API This patch enhances the GET /catalog/packages Murano API call with option to filter packages by id. Partially implements bp assign-category-button Change-Id: I86bbdbde74a7fa086197adef7a91199704811f72 --- .../specification/murano-repository.rst | 4 ++- murano/api/v1/__init__.py | 8 ++--- murano/db/catalog/api.py | 3 ++ murano/tests/unit/api/v1/test_catalog.py | 35 +++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/doc/source/specification/murano-repository.rst b/doc/source/specification/murano-repository.rst index b9ce4120..eb26b7e8 100644 --- a/doc/source/specification/murano-repository.rst +++ b/doc/source/specification/murano-repository.rst @@ -42,7 +42,7 @@ Methods for application package management List packages ------------- -`/v1/catalog/packages?{marker}{limit}{order_by}{type}{category}{fqn}{owned}{catalog}{class_name} [GET]` +`/v1/catalog/packages?{marker}{limit}{order_by}{type}{category}{fqn}{owned}{id}{catalog}{class_name} [GET]` This is the compound request to list and search through application catalog. If there are no search parameters all packages that is_public, enabled and belong to the user's tenant will be listed. @@ -73,6 +73,8 @@ For an admin role all packages are available. +----------------------+-------------+------------------------------------------------------------------------------------------------------------------------------+ | ``owned`` | bool | Search only from packages owned by current tenant | +----------------------+-------------+------------------------------------------------------------------------------------------------------------------------------+ +| ``id`` | string | Allows to point an id for a search | | ++----------------------+-------------+------------------------------------------------------------------------------------------------------------------------------+ | ``include_disabled`` | bool | Include disabled packages in a the result | +----------------------+-------------+------------------------------------------------------------------------------------------------------------------------------+ | ``search`` | string | Gives opportunity to search specified data by all the package parameters | diff --git a/murano/api/v1/__init__.py b/murano/api/v1/__init__.py index 42c5ab30..c037ca1e 100644 --- a/murano/api/v1/__init__.py +++ b/murano/api/v1/__init__.py @@ -17,10 +17,10 @@ from murano.db import session as db_session stats = None -SUPPORTED_PARAMS = ('order_by', 'category', 'marker', 'tag', 'class_name', - 'limit', 'type', 'fqn', 'category', 'owned', 'search', - 'include_disabled', 'sort_dir') -LIST_PARAMS = ('category', 'tag', 'class', 'order_by') +SUPPORTED_PARAMS = ('id', 'order_by', 'category', 'marker', 'tag', + 'class_name', 'limit', 'type', 'fqn', 'category', 'owned', + 'search', 'include_disabled', 'sort_dir') +LIST_PARAMS = ('id', 'category', 'tag', 'class', 'order_by') ORDER_VALUES = ('fqn', 'name', 'created') PKG_PARAMS_MAP = {'display_name': 'name', 'full_name': 'fully_qualified_name', diff --git a/murano/db/catalog/api.py b/murano/db/catalog/api.py index 85153006..5f7857d0 100644 --- a/murano/db/catalog/api.py +++ b/murano/db/catalog/api.py @@ -264,6 +264,7 @@ def package_search(filters, context, manage_public=False, request and then to use the ID of the last package from the response as the marker parameter in a subsequent limited request. """ + # pylint: disable=too-many-branches session = db_session.get_session() pkg = models.Package @@ -293,6 +294,8 @@ def package_search(filters, context, manage_public=False, if 'type' in filters.keys(): query = query.filter(pkg.type == filters['type'].title()) + if 'id' in filters: + query = query.filter(models.Package.id.in_(filters['id'])) if 'category' in filters.keys(): query = query.filter(pkg.categories.any( models.Category.name.in_(filters['category']))) diff --git a/murano/tests/unit/api/v1/test_catalog.py b/murano/tests/unit/api/v1/test_catalog.py index ddac344d..68615a54 100644 --- a/murano/tests/unit/api/v1/test_catalog.py +++ b/murano/tests/unit/api/v1/test_catalog.py @@ -162,6 +162,41 @@ class TestCatalogApi(test_base.ControllerTest, test_base.MuranoApiTestCase): '/v1/catalog/packages/', params={'catalog': 'False'})) self.assertEqual(2, len(result['packages'])) + def test_packages_filter_by_id(self): + """GET /catalog/packages with parameter "id" returns packages + filtered by id. + """ + self._set_policy_rules( + {'get_package': '', + 'manage_public_package': ''} + ) + _, package1_data = self._test_package() + _, package2_data = self._test_package() + + package1_data['fully_qualified_name'] += '_1' + package1_data['name'] += '_1' + package1_data['class_definitions'] = (u'test.mpl.v1.app.Thing1',) + package2_data['fully_qualified_name'] += '_2' + package2_data['name'] += '_2' + package2_data['class_definitions'] = (u'test.mpl.v1.app.Thing2',) + + expected_package = db_catalog_api.package_upload(package1_data, '') + db_catalog_api.package_upload(package2_data, '') + + req = self._get('/catalog/packages', + params={'id': expected_package.id}) + self.expect_policy_check('get_package') + self.expect_policy_check('manage_public_package') + + res = req.get_response(self.api) + self.assertEqual(200, res.status_code) + + self.assertEqual(len(res.json['packages']), 1) + + found_package = res.json['packages'][0] + + self.assertEqual(found_package['id'], expected_package.id) + def test_packages(self): self._set_policy_rules( {'get_package': '',