use a prefix tree to decide which registry to send the request to. The prefix tree...
[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
12 class list(Method):
13     """
14     List the records in an authority. 
15
16     @param cred credential string specifying the rights of the caller
17     @param hrn human readable name of authority to list
18     @return list of record dictionaries         
19     """
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):
31         
32         self.api.auth.check(cred, 'list')
33         records = []
34
35         # load all know registry names into a prefix tree and attempt to find
36         # the longest matching prefix  
37         registries = Registries(self.api)
38         hrns = registries.keys()
39         tree = prefixTree()
40         tree.load(hrns)
41         registry_hrn = tree.best_match(hrn)
42
43         #if there was no match then this record belongs to an unknow registry
44         if not registry_hrn:
45             raise MissingAuthority(hrn)
46         
47         # if the best match (longest matching hrn) is not the local registry,
48         # forward the request
49         if registry_hrn != self.api.hrn:
50             credential = self.api.getCredential()
51             try:
52                 record_list = registries[registry_hrn].list(credential, hrn)
53                 records = [record.as_dict() for record in record_list]
54                 if records:
55                     return records
56             except:
57                 pass
58
59         # if we still havnt found the record yet, try the local registry
60         if not self.api.auth.hierarchy.auth_exists(hrn):
61             raise MissingAuthority(hrn)
62         
63         table = self.api.auth.get_auth_table(hrn)
64         records = table.list()
65           
66         return records