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.
14 base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
17 def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback = None ):
19 Run the rspec through sfatables
20 @param chain Name of rule chain
21 @param hrn Object's hrn
22 @param origin_hrn Original caller's hrn
23 @param rspec Incoming rspec
24 @param context_callback Callback used to generate the request context
28 if not context_callback:
29 context_callback = fetch_context
32 rules = SFATablesRules(chain)
33 if rules.sorted_rule_list:
34 contexts = rules.contexts
35 request_context = context_callback(hrn, origin_hrn, contexts)
36 rules.set_context(request_context)
37 newrspec = rules.apply(rspec)
44 from sfa.util.sfalogging import logger
45 def run_sfatables (_,__,___, rspec, ____=None):
46 logger.warning("Cannot import sfatables.runtime, please install package sfa-sfatables")