dc34f759dd3e28149ebac29f291ac7d57276866c
[sfa.git] / sfa / methods / Resolve.py
1 import types
2
3 from sfa.util.xrn import Xrn, urn_to_hrn
4 from sfa.util.method import Method
5
6 from sfa.trust.credential import Credential
7
8 from sfa.storage.parameter import Parameter, Mixed
9
10 class Resolve(Method):
11     """
12     Resolve a record.
13
14     @param cred credential string authorizing the caller
15     @param hrn human readable name to resolve (hrn or urn) 
16     @return a list of record dictionaries or empty list     
17     """
18
19     interfaces = ['registry']
20     
21     # should we not accept an optional 'details' argument ?
22     accepts = [
23         Mixed(Parameter(str, "Human readable name (hrn or urn)"),
24               Parameter(list, "List of Human readable names ([hrn])")),
25         Mixed(Parameter(str, "Credential string"),
26               Parameter(list, "List of credentials)")),
27         Parameter(dict, "options"),
28         ]
29
30     # xxx used to be [SfaRecord]
31     returns = [Parameter(dict, "registry record")]
32     
33     def call(self, xrns, creds, options=None):
34         if options is None: options={}
35         # use details=False by default, only when explicitly specified do we want 
36         # to mess with the testbed details
37         if 'details' in options: details=options['details']
38         else:                    details=False
39         type = None
40         if not isinstance(xrns, types.ListType):
41             type = Xrn(xrns).get_type()
42             xrns=[xrns]
43         hrns = [urn_to_hrn(xrn)[0] for xrn in xrns]
44         #find valid credentials
45         valid_creds = self.api.auth.checkCredentials(creds, 'resolve')
46
47         #log the call
48         origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn()
49         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrns, self.name))
50  
51         # send the call to the right manager
52         return self.api.manager.Resolve(self.api, xrns, type, details=details)
53