initial checkin of helper sfatables runtime methods. Moved fetch_contect() and __run_...
[sfa.git] / sfa / util / sfatablesRuntime.py
1 from sfatables.runtime import SFATablesRules
2
3 def fetch_context(slice_hrn, user_hrn, contexts):
4     """
5     Returns the request context required by sfatables. At some point, this
6     mechanism should be changed to refer to "contexts", which is the
7     information that sfatables is requesting. But for now, we just return
8     the basic information needed in a dict.
9     """
10     slice_hrn = urn_to_hrn(slice_xrn)[0]
11     user_hrn = urn_to_hrn(user_xrn)[0]
12     base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
13     return base_context
14
15 def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback = None ):
16     """
17     Run the rspec through sfatables
18     @param chain Name of rule chain
19     @param hrn Object's hrn
20     @param origin_hrn Original caller's hrn
21     @param rspec Incoming rspec
22     @param context_callback Callback used to generate the request context  
23
24     @return rspec
25     """
26     if not context_callback:
27         context_callback = fetch_context
28
29     chain = chain.upper()
30     rules = SFATablesRules(chain)
31     if rules.sorted_rule_list:
32         contexts = rules.contexts
33         request_context = context_callback(hrn, origin_hrn, contexts)
34         rules.set_context(request_context)
35         newrspec = rules.apply(rspec)
36     else:
37         newrspec = rspec
38     return newrspec