summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Cordasco <ian.cordasco@rackspace.com>2015-02-19 16:04:25 -0600
committerIan Cordasco <ian.cordasco@rackspace.com>2015-02-19 18:12:40 -0600
commit49716e0328b9c540d7270b89344546efcf211cdd (patch)
tree6d108ffd66775fd89891df6d86a8da3e17a43e2a
parent41d8bf1c07c84808ebd50313755cf77721c03e58 (diff)
downloadoslo-policy-49716e0328b9c540d7270b89344546efcf211cdd.tar.gz
Add Rules.from_dict classmethod
Many services currently construct a Rules instance from a dictionary in their tests. It would be beneficial to them if they didn't have to import oslo_policy._parser in order to parse a rule before passing it to Rules.__init__. Adding a Rules.from_dict classmethod should satisfy the need to create a Rules instance from a dictionary in tests. Change-Id: I3d73059277c45d4852f842474c6f1d61daa349b8
-rw-r--r--oslo_policy/policy.py9
-rw-r--r--oslo_policy/tests/test_policy.py8
2 files changed, 17 insertions, 0 deletions
diff --git a/oslo_policy/policy.py b/oslo_policy/policy.py
index 0f617f0..e4f4a07 100644
--- a/oslo_policy/policy.py
+++ b/oslo_policy/policy.py
@@ -240,6 +240,15 @@ class Rules(dict):
return cls(rules, default_rule)
+ @classmethod
+ def from_dict(cls, rules_dict, default_rule=None):
+ """Allow loading of rule data from a dictionary."""
+
+ # Parse the rules stored in the dictionary
+ rules = dict((k, _parser.parse_rule(v)) for k, v in rules_dict.items())
+
+ return cls(rules, default_rule)
+
def __init__(self, rules=None, default_rule=None):
"""Initialize the Rules store."""
diff --git a/oslo_policy/tests/test_policy.py b/oslo_policy/tests/test_policy.py
index 3d3fe9e..3401cb5 100644
--- a/oslo_policy/tests/test_policy.py
+++ b/oslo_policy/tests/test_policy.py
@@ -109,6 +109,14 @@ class RulesTestCase(test_base.BaseTestCase):
default=[],
))
+ @mock.patch.object(_parser, 'parse_rule', lambda x: x)
+ def test_from_dict(self):
+ expected = {'admin_or_owner': 'role:admin', 'default': '@'}
+ rules = policy.Rules.from_dict(expected, 'default')
+
+ self.assertEqual('default', rules.default_rule)
+ self.assertEqual(expected, rules)
+
def test_str(self):
exemplar = """{
"admin_or_owner": "role:admin or project_id:%(project_id)s"