dont raise the excpetion
[sfa.git] / sfa / methods / resolve.py
1 ### $Id$
2 ### $URL$
3 import traceback
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.debug import log
10 from sfa.server.registry import Registries
11 from sfa.util.prefixTree import prefixTree
12
13 class resolve(Method):
14     """
15     Resolve a record.
16
17     @param cred credential string authorizing the caller
18     @param hrn human readable name to resolve
19     @return a list of record dictionaries or empty list     
20     """
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):
32         
33         self.api.auth.check(cred, 'resolve')
34         good_records = [] 
35
36         # load all know registry names into a prefix tree and attempt to find
37         # the longest matching prefix
38         registries = Registries(self.api)
39         hrns = registries.keys()
40         tree = prefixTree()
41         tree.load(hrns)
42         registry_hrn = tree.best_match(hrn)
43
44         #if there was no match then this record belongs to an unknow registry
45         if not registry_hrn:
46             raise MissingAuthority(hrn)
47
48         # if the best match (longest matching hrn) is not the local registry,
49         # forward the request
50         if registry_hrn != self.api.hrn:
51             credential = self.api.getCredential()
52             try:
53                 records = registries[registry_hrn].resolve(credential, hrn)
54                 good_records = [record.as_dict() for record in records]
55                 if good_records:
56                     return good_records
57             except:
58                 traceback.print_exc()
59
60         # if we still havnt found the record yet, try the local registry
61         auth_hrn = self.api.auth.get_authority(hrn)
62         if not auth_hrn:
63             auth_hrn = hrn
64         table = self.api.auth.get_auth_table(auth_hrn)
65         records = table.resolve('*', hrn)
66         if not records:
67             raise RecordNotFound(hrn) 
68         for record in records:
69             try:
70                 self.api.fill_record_info(record)
71                 good_records.append(dict(record))
72             except PlanetLabRecordDoesNotExist:
73                 # silently drop the ones that are missing in PL
74                 print >> log, "ignoring geni record ", record.get_name(), \
75                               " because pl record does not exist"
76                 table.remove(record)
77
78
79         return good_records    
80