a little nicer wrt pep8
[sfa.git] / sfa / methods / Resolve.py
1 from sfa.util.xrn import Xrn, urn_to_hrn
2 from sfa.util.method import Method
3 from sfa.util.sfalogging import logger
4
5 from sfa.trust.credential import Credential
6
7 from sfa.storage.parameter import Parameter, Mixed
8
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:
35             options = {}
36         # use details=False by default, only when explicitly specified do we want
37         # to mess with the testbed details
38         if 'details' in options:
39             details = options['details']
40         else:
41             details = False
42         type = None
43         if not isinstance(xrns, list):
44             type = Xrn(xrns).get_type()
45             xrns = [xrns]
46         hrns = [urn_to_hrn(xrn)[0] for xrn in xrns]
47         # find valid credentials
48         valid_creds = self.api.auth.checkCredentials(creds, 'resolve')
49
50         # log the call
51         origin_hrn = Credential(
52             string=valid_creds[0]).get_gid_caller().get_hrn()
53         logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s" %
54                     (self.api.interface, origin_hrn, hrns, self.name))
55
56         # send the call to the right manager
57         return self.api.manager.Resolve(self.api, xrns, type, details=details)