add request_hash argumet to more method calls
[sfa.git] / sfa / methods / get_resources.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5 from sfa.util.method import Method
6 from sfa.util.parameter import Parameter, Mixed
7 from sfa.trust.auth import Auth
8 from sfa.util.config import Config
9 from sfa.plc.nodes import Nodes
10 # RSpecManager_pl is not used. This line is a check that ensures that everything is in place for the import to work.
11 import sfa.rspecs.aggregates.rspec_manager_pl
12 from sfa.trust.credential import Credential
13 from sfatables.runtime import SFATablesRules
14
15 class get_resources(Method):
16     """
17     Get an resource specification (rspec). The rspec may describe the resources
18     available at an authority or the resources being used by a slice.      
19
20     @param cred credential string specifying the rights of the caller
21     @param hrn human readable name of the slice we are interesed in or None 
22            for an authority.  
23     """
24
25     interfaces = ['aggregate', 'slicemgr']
26     
27     accepts = [
28         Parameter(str, "Credential string"),
29         Mixed(Parameter(str, "Human readable name (hrn)"),
30               Parameter(None, "hrn not specified")),
31         Parameter(str, "Request hash")
32         ]
33
34     returns = Parameter(str, "String representatin of an rspec")
35     
36     def call(self, cred, hrn=None, request_hash = None, caller_cred=None):
37         sfa_aggregate_type = Config().get_aggregate_rspec_type()
38
39         # This cred will be an authority cred, not a user, so we cant use it to 
40         # authenticate the caller's request_hash. Let just get the caller's gid
41         # from the cred and authenticate using that 
42         client_gid = Credential(string=cred).get_gid_caller()
43         client_gid_str = client_gid.save_to_string(save_parents=True)
44         self.api.auth.authenticateGid(client_gid_str, [cred,hrn], request_hash)
45         self.api.auth.check(cred, 'listnodes')
46         if caller_cred==None:
47             caller_cred=cred
48
49         #log the call
50         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, Credential(string=caller_cred).get_gid_caller().get_hrn(), hrn, self.name))
51
52         # This code needs to be cleaned up so that 'pl' is treated as just another RSpec manager.
53         # The change ought to be straightforward as soon as we define PL's new RSpec.
54
55         rspec_manager = __import__("sfa.rspecs.aggregates.rspec_manager_"+sfa_aggregate_type,
56                                    fromlist = ["sfa.rspecs.aggregates"])
57         if (sfa_aggregate_type == 'pl'):
58             nodes = Nodes(self.api, caller_cred=caller_cred)
59             if hrn:
60                 rspec = nodes.get_rspec(hrn)
61             else:
62                 nodes.refresh()
63                 rspec = nodes['rspec']
64         else:
65             rspec = rspec_manager.get_rspec(self.api, hrn)
66
67         # Filter the outgoing rspec using sfatables
68         outgoing_rules = SFATablesRules('OUTGOING')
69
70         request_context = rspec_manager.fetch_context(
71             hrn,
72             Credential(string=caller_cred).get_gid_caller().get_hrn(),
73             outgoing_rules.contexts)
74         outgoing_rules.set_context(request_context)
75         filtered_rspec = outgoing_rules.apply(rspec)
76
77         return filtered_rspec