logging and call tracing features
[sfa.git] / sfa / methods / list.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.record import GeniRecord
9 from sfa.server.registry import Registries
10 from sfa.util.prefixTree import prefixTree
11 from sfa.trust.credential import Credential
12
13 class list(Method):
14     """
15     List the records in an authority. 
16
17     @param cred credential string specifying the rights of the caller
18     @param hrn human readable name of authority to list
19     @return list of record dictionaries         
20     """
21     interfaces = ['registry']
22     
23     accepts = [
24         Parameter(str, "Credential string"),
25         Parameter(str, "Human readable name (hrn)")
26         ]
27
28     returns = [GeniRecord]
29     
30     def call(self, cred, hrn, caller_cred=None):
31
32         self.api.auth.check(cred, 'list')
33         if caller_cred==None:
34            caller_cred=cred
35
36         #log the call
37         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))
38         records = []
39
40         # load all know registry names into a prefix tree and attempt to find
41         # the longest matching prefix  
42         registries = Registries(self.api)
43         hrns = registries.keys()
44         tree = prefixTree()
45         tree.load(hrns)
46         registry_hrn = tree.best_match(hrn)
47
48         #if there was no match then this record belongs to an unknow registry
49         if not registry_hrn:
50             raise MissingAuthority(hrn)
51         
52         # if the best match (longest matching hrn) is not the local registry,
53         # forward the request
54         if registry_hrn != self.api.hrn:
55             credential = self.api.getCredential()
56             try:
57                 record_list = registries[registry_hrn].list(credential, hrn, caller_cred=caller_cred)
58                 records = [record.as_dict() for record in record_list]
59                 if records:
60                     return records
61             except:
62                 pass
63
64         # if we still havnt found the record yet, try the local registry
65         if not self.api.auth.hierarchy.auth_exists(hrn):
66             raise MissingAuthority(hrn)
67         
68         table = self.api.auth.get_auth_table(hrn)
69         records = table.list()
70           
71         return records