fix sfatables on slice create
[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     base_context = {'sfa':{'user':{'hrn':user_hrn}, 'slice':{'hrn':slice_hrn}}}
11     return base_context
12
13 def run_sfatables(chain, hrn, origin_hrn, rspec, context_callback = None ):
14     """
15     Run the rspec through sfatables
16     @param chain Name of rule chain
17     @param hrn Object's hrn
18     @param origin_hrn Original caller's hrn
19     @param rspec Incoming rspec
20     @param context_callback Callback used to generate the request context  
21
22     @return rspec
23     """
24     if not context_callback:
25         context_callback = fetch_context
26
27     chain = chain.upper()
28     rules = SFATablesRules(chain)
29     if rules.sorted_rule_list:
30         contexts = rules.contexts
31         request_context = context_callback(hrn, origin_hrn, contexts)
32         rules.set_context(request_context)
33         newrspec = rules.apply(rspec)
34     else:
35         newrspec = rspec
36     return newrspec