again getting away with caller_cred and passing just the hrn of the initial caller
[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         Mixed(Parameter(str, "Request hash"),
28               Parameter(None, "Request hash not specified"))
29         ]
30
31     returns = [GeniRecord]
32     
33     def call(self, cred, hrn, request_hash=None, origin_hrn=None):
34         self.api.auth.authenticateCred(cred, [cred, hrn], request_hash)
35         self.api.auth.check(cred, 'list')
36         if origin_hrn==None:
37             origin_hrn=Credential(string=cred).get_gid_caller().get_hrn()
38
39         #log the call
40         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
41         records = []
42
43         # load all know registry names into a prefix tree and attempt to find
44         # the longest matching prefix  
45         registries = Registries(self.api)
46         hrns = registries.keys()
47         tree = prefixTree()
48         tree.load(hrns)
49         registry_hrn = tree.best_match(hrn)
50
51         #if there was no match then this record belongs to an unknow registry
52         if not registry_hrn:
53             raise MissingAuthority(hrn)
54         
55         # if the best match (longest matching hrn) is not the local registry,
56         # forward the request
57         if registry_hrn != self.api.hrn:
58             credential = self.api.getCredential()
59             try:
60                 request_hash=None
61                 record_list = registries[registry_hrn].list(credential, hrn, request_hash, origin_hrn)
62                 records = [GeniRecord(dict=record).as_dict() for record in record_list]
63             except:
64                 arg_list = [credential, hrn]
65                 request_hash = self.api.key.compute_hash(arg_list)
66                 record_list = registries[registry_hrn].list(credential, hrn, request_hash, origin_hrn)
67                 records = [GeniRecord(dict=record).as_dict() for record in record_list] 
68                 
69         if records:
70             return records
71
72         # if we still havnt found the record yet, try the local registry
73         if not self.api.auth.hierarchy.auth_exists(hrn):
74             raise MissingAuthority(hrn)
75         
76         table = GeniTable()
77         records = table.find({'authority': hrn})
78         
79         return records