diff --git a/congress/api/library_policy_model.py b/congress/api/library_policy_model.py index 73d819ee2..05060cce8 100644 --- a/congress/api/library_policy_model.py +++ b/congress/api/library_policy_model.py @@ -37,6 +37,7 @@ class LibraryPolicyModel(base.APIModel): Args: params: A dict-like object containing parameters from the request query string and body. + The name parameter filters results by name policy name. context: Key-values providing frame of reference of request Returns: A dict containing at least a 'results' key whose value is @@ -44,10 +45,23 @@ class LibraryPolicyModel(base.APIModel): dict will also be rendered for the user. """ try: - # Note(thread-safety): blocking call - return {"results": self.invoke_rpc(base.LIBRARY_SERVICE_ID, - 'get_policies', - {})} + # Note: name is included as a filtering parameter in get_items + # rather than a key in get_item because the API does not commit to + # library policy name being unique. + if 'name' in params: + # Note(thread-safety): blocking call + try: + policy = self.invoke_rpc( + base.LIBRARY_SERVICE_ID, 'get_policy_by_name', + {'name': params['name'], 'include_rules': True}) + return {"results": [policy]} + except KeyError: # not found + return {"results": []} + else: + # Note(thread-safety): blocking call + return {"results": self.invoke_rpc(base.LIBRARY_SERVICE_ID, + 'get_policies', + {})} except exception.CongressException as e: raise webservice.DataModelException.create(e) diff --git a/congress/db/db_library_policies.py b/congress/db/db_library_policies.py index 4f79375d4..7d49d2ca9 100644 --- a/congress/db/db_library_policies.py +++ b/congress/db/db_library_policies.py @@ -115,6 +115,15 @@ def get_policy(id_, session=None): raise KeyError('No policy found with policy id %s' % id_) +def get_policy_by_name(name, session=None): + session = session or db.get_session() + try: + return session.query(LibraryPolicy).filter( + LibraryPolicy.name == name).one() + except db_exc.NoResultFound: + raise KeyError('No policy found with policy name %s' % name) + + def get_policies(session=None): session = session or db.get_session() return (session.query(LibraryPolicy).all()) diff --git a/congress/library_service/library_service.py b/congress/library_service/library_service.py index 93778805f..50c3a19cd 100644 --- a/congress/library_service/library_service.py +++ b/congress/library_service/library_service.py @@ -76,6 +76,11 @@ class LibraryService (data_service.DataService): policy = db_library_policies.get_policy(id_) return policy.to_dict(include_rules) + def get_policy_by_name(self, name, include_rules=True): + # Note(thread-safety): blocking call + policy = db_library_policies.get_policy_by_name(name) + return policy.to_dict(include_rules) + def delete_all_policies(self): # Note(thread-safety): blocking call db_library_policies.delete_policies() @@ -229,6 +234,9 @@ class DseLibraryServiceEndpoints(object): def get_policy(self, context, id_, include_rules=True): return self.data_service.get_policy(id_, include_rules) + def get_policy_by_name(self, context, name, include_rules=True): + return self.data_service.get_policy_by_name(name, include_rules) + def delete_all_policies(self, context): return self.data_service.delete_all_policies() diff --git a/congress/tests/api/test_library_policy_model.py b/congress/tests/api/test_library_policy_model.py index d5d62296b..2af535f01 100644 --- a/congress/tests/api/test_library_policy_model.py +++ b/congress/tests/api/test_library_policy_model.py @@ -79,6 +79,15 @@ class TestLibraryPolicyModel(base.SqlTestCase): for p in [self.policy, self.policy2])) + def test_get_items_by_name(self): + ret = self.library_policy_model.get_items( + {'name': 'no-such-policy'}) + self.assertEqual(ret['results'], []) + + ret = self.library_policy_model.get_items( + {'name': self.policy['name']}) + self.assertEqual(ret['results'], [self.policy]) + def test_get_item(self): expected_ret = self.policy ret = self.library_policy_model.get_item(self.policy["id"], {}) diff --git a/congress/tests/db/test_db_library_policies.py b/congress/tests/db/test_db_library_policies.py index 571d90dad..a2f7436ae 100644 --- a/congress/tests/db/test_db_library_policies.py +++ b/congress/tests/db/test_db_library_policies.py @@ -68,13 +68,15 @@ class TestDbLibraryPolicies(base.SqlTestCase): self.assertRaises(KeyError, db_library_policies.get_policy, 'nosuchpolicy') + self.assertRaises(KeyError, db_library_policies.get_policy_by_name, + 'nosuchpolicy') + def test_create_get_policy(self): policy1 = db_library_policies.add_policy( {'name': 'policy1', 'abbreviation': 'abbr', 'kind': 'database', 'description': 'descrip', 'rules': [{'rule': 'p(x) :- q(x)', 'comment': 'test comment', 'name': 'testname'}]}) - res = db_library_policies.get_policies() res = db_library_policies.get_policies() self.assertEqual([p.to_dict(include_rules=True) for p in res], @@ -98,9 +100,23 @@ class TestDbLibraryPolicies(base.SqlTestCase): 'comment': 'test comment', 'name': 'testname'}]}) + res = db_library_policies.get_policy_by_name(policy1['name']) + self.assertEqual(res.to_dict(include_rules=True), + {'id': policy1['id'], + 'abbreviation': 'abbr', + 'kind': 'database', + 'name': 'policy1', + 'description': 'descrip', + 'rules': [{'rule': 'p(x) :- q(x)', + 'comment': 'test comment', + 'name': 'testname'}]}) + self.assertRaises(KeyError, db_library_policies.get_policy, 'no_such_policy') + self.assertRaises(KeyError, db_library_policies.get_policy_by_name, + 'no_such_policy') + def test_delete_policy(self): db_library_policies.delete_policy('policy1') diff --git a/congress/tests/library_service/test_library_service.py b/congress/tests/library_service/test_library_service.py index 29ccec617..1c4cb2010 100644 --- a/congress/tests/library_service/test_library_service.py +++ b/congress/tests/library_service/test_library_service.py @@ -86,6 +86,9 @@ class TestLibraryService(base.SqlTestCase): self.assertRaises(KeyError, self.library.get_policy, 'nosuchpolicy') + self.assertRaises(KeyError, self.library.get_policy_by_name, + 'nosuchpolicy') + def test_create_get_policy(self): policy_obj = self.library.create_policy(self.policy1) self.policy1['id'] = policy_obj['id'] @@ -96,14 +99,24 @@ class TestLibraryService(base.SqlTestCase): res = self.library.get_policy(policy_obj['id']) self.assertEqual(res, self.policy1) + res = self.library.get_policy_by_name(policy_obj['name']) + self.assertEqual(res, self.policy1) + res = self.library.get_policies(include_rules=True) self.assertEqual(res, [self.policy1]) res = self.library.get_policy(policy_obj['id'], include_rules=False) self.assertEqual(res, self.policy1_meta) + res = self.library.get_policy_by_name(policy_obj['name'], + include_rules=False) + self.assertEqual(res, self.policy1_meta) + self.assertRaises(KeyError, self.library.get_policy, 'no_such_policy') + self.assertRaises(KeyError, self.library.get_policy_by_name, + 'no_such_policy') + def test_delete_policy(self): self.assertRaises(KeyError, self.library.delete_policy, 'policy1')