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