3e99fe46cb77f097529d41522f2131aa9943af91
[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 is just to resolve issues with the dynamic __import__ that comes later.
11 import sfa.rspecs.aggregates.rspec_manager_pl
12 from sfa.trust.credential import Credential
13
14 class get_resources(Method):
15     """
16     Get an resource specification (rspec). The rspec may describe the resources
17     available at an authority or the resources being used by a slice.      
18
19     @param cred credential string specifying the rights of the caller
20     @param hrn human readable name of the slice we are interesed in or None 
21            for an authority.  
22     """
23
24     interfaces = ['aggregate', 'slicemgr']
25     
26     accepts = [
27         Parameter(str, "Credential string"),
28         Mixed(Parameter(str, "Human readable name (hrn)"),
29               Parameter(None, "hrn not specified"))
30         ]
31
32     returns = Parameter(str, "String representatin of an rspec")
33     
34     def call(self, cred, hrn=None, caller_cred=None):
35         sfa_aggregate_type = Config().get_aggregate_rspec_type()
36
37         self.api.auth.check(cred, 'listnodes')
38         if caller_cred==None:
39            caller_cred=cred
40
41         #log the call
42         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))
43
44         # This code needs to be cleaned up so that 'pl' is treated as just another RSpec manager.
45         # The change ought to be straightforward as soon as we define PL's new RSpec.
46
47         if (sfa_aggregate_type == 'pl'):
48             nodes = Nodes(self.api, caller_cred=caller_cred)
49             if hrn:
50                 rspec = nodes.get_rspec(hrn)
51             else:
52                 nodes.refresh()
53                 rspec = nodes['rspec']
54         else:
55             rspec_manager = __import__("sfa.rspecs.aggregates.rspec_manager_"+sfa_aggregate_type, fromlist = ["sfa.rspecs.aggregates"])
56             rspec = rspec_manager.get_rspec(self.api, hrn)
57         
58         return rspec