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