1 # sfa should not depend on sfatables
2 # if the sfatables.runtime import fails, just define run_sfatables as identity
5 from sfatables.runtime import SFATablesRules
7 def fetch_context(slice_hrn, user_hrn, contexts):
9 Returns the request context required by sfatables. At some point, this
10 mechanism should be changed to refer to "contexts", which is the
11 information that sfatables is requesting. But for now, we just return
12 the basic information needed in a dict.
15 'sfa': {'user': {'hrn': user_hrn}, 'slice': {'hrn': slice_hrn}}}
18 def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback=None):
20 Run the rspec through sfatables
21 @param chain Name of rule chain
22 @param hrn Object's hrn
23 @param origin_hrn Original caller's hrn
24 @param rspec Incoming rspec
25 @param context_callback Callback used to generate the request context
29 if not context_callback:
30 context_callback = fetch_context
33 rules = SFATablesRules(chain)
34 if rules.sorted_rule_list:
35 contexts = rules.contexts
36 request_context = context_callback(hrn, origin_hrn, contexts)
37 rules.set_context(request_context)
38 newrspec = rules.apply(rspec)
45 from sfa.util.sfalogging import logger
47 def run_sfatables(_, __, ___, rspec, ____=None):
49 "Cannot import sfatables.runtime, please install package sfa-sfatables")