05bffcbbee8d2bd9801c1361b013830fbf1e03f5
[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
9 class Resolve(Method):
10     """
11     Resolve a record.
12
13     @param cred credential string authorizing the caller
14     @param hrn human readable name to resolve (hrn or urn) 
15     @return a list of record dictionaries or empty list     
16     """
17
18     interfaces = ['registry']
19
20     # should we not accept an optional 'details' argument ?
21     accepts = [
22         Mixed(Parameter(str, "Human readable name (hrn or urn)"),
23               Parameter(list, "List of Human readable names ([hrn])")),
24         Mixed(Parameter(str, "Credential string"),
25               Parameter(list, "List of credentials)")),
26         Parameter(dict, "options"),
27     ]
28
29     # xxx used to be [SfaRecord]
30     returns = [Parameter(dict, "registry record")]
31
32     def call(self, xrns, creds, options=None):
33         if options is None:
34             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:
38             details = options['details']
39         else:
40             details = False
41         type = None
42         if not isinstance(xrns, list):
43             type = Xrn(xrns).get_type()
44             xrns = [xrns]
45         hrns = [urn_to_hrn(xrn)[0] for xrn in xrns]
46         # find valid credentials
47         valid_creds = self.api.auth.checkCredentials(creds, 'resolve')
48
49         # log the call
50         origin_hrn = Credential(
51             string=valid_creds[0]).get_gid_caller().get_hrn()
52         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s" %
53                              (self.api.interface, origin_hrn, hrns, self.name))
54
55         # send the call to the right manager
56         return self.api.manager.Resolve(self.api, xrns, type, details=details)